Is the indirection of a null pointer to an array type UB?

Consider this code:

#include <stdio.h>

int *f(int (*p)[2])
{
    return *p; //Possible UB here?
}

int main()
{
    printf("%p", f(NULL));
}

Is the fact that we apply an indirect call to a null pointer, create a UB?

Perhaps this is not so, because the lvalue of the array type is converted back to a pointer, and no object value is actually executed. Which of these is true in the case?

EDIT: I know exactly what UB is. I just want, as a proof or some explanation, using a standard article, why or why there is no UB code above.

+4
source share
3 answers

, . NULL undefined.

, , undefined , , .

, , , , undefined. , - .

undefined, .

+3

NULL - , (ny) ( ) UB.

, , NULL.

p , p == NULL, *p . , undefined .

FWIW, NULL , NULL.

0

, , .

: , UB, , , , , . undefined .

, , , . , , , , . null, .

return *p;

, :

return (int *) p; // no UB here even if p is null!

, : .


. , undefined . , , n1256 C99 n1570 C11, 6.5.3.2 ( ):

Β§4 * ... '' , type ''. , * undefined.

, :

* ...


, , lvalue :

  • ()

  • postfix [] lvalue

*p[i], , UB, , resul. ,

(return *p;) , , . ( ) :

, & * E E (, E )...

p , . 6.5.2.1 , :

Β§ 3 . E - n- (n Β³ 2) 'j'., 'K, E ( lvalue) (n - 1) - j '., 'K. * , (n - 1) -

IMHO , *p (int *) p, f , .

, , , *, , UB. , , . , , UB, UB , , .

NOTE. I know that comments are not normative, but they are here to help understand the standard. Therefore, when one comment explicitly says that it is &*Eequivalent E(even if it Eis a null pointer), this actually means that, provided that the result is still used for its address, applying the * operator to the null pointer is not necessarily UB.

0
source

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


All Articles