Does const invoice match UB when pointer argument is received by va_arg?

I noticed some potential problem with a macro va_argthat is used to get an unnamed parameter from a function variable. Consider the following simplified example:

#include <stdio.h>
#include <stdarg.h>

void foo(int n, ...)
{
    va_list ap;  
    const char *s;

    va_start(ap, n);
    for (s = va_arg(ap, const char *); *s != '\0'; s++)
        putchar(*s);
    va_end(ap);
}

int main(void)
{
    char str[] = "xyz";
    foo(1, str);
    return 0;
}

The link to va_argindicates that (my selection):

If va_arg is called when there are no more arguments in ap, or if the type of the next argument in ap (after promotions) is incompatible with T, the behavior is undefined (...)

, const char * char * . char foo, char ( ), , const- :

s = va_arg(ap, const char *)

"" UB. , arr const, char *, , . int * const int *.

+4
3

6.2.7 C11 , 6.7.6.1 6.7. 3 . ( 10) :

, , ; .

, 6.7.6.1 :

, , , .

, , const char* char* ( const char char ). va_arg 7.16.1.1, ( , ), , undefined .

, , , char* const char* --- . , va_arg .

+3

n1570/6.2.5p28, , , :

... ...

char , char , const char. , .

, char, char, UB n1570/6.7.3p6:

, , const, lvalue , undefined.


, , n1570/6.7.6.1p2:

, , , .

, , , const char char .

, ... nickie. , , UB.

+3

, va_arg const char *, char *, , - undefined.

1.

, , , 2.


(Quoted from ISO / IEC 9899: 201x)

1 (6.2.7 Compatible type and composite type 1)
Two types have a compatible type if their types are the same

2 (6.7.3. Type classifiers 10)
For two compatible compatible types, both must have the same version of the compatible type; the order of type classifiers in the list of qualifiers or classifiers does not affect the specified type.

+1
source

Source: https://habr.com/ru/post/1670467/


All Articles