On 22/7/23 12:22 pm, Thorsten Glaser via austin-group-l at The Open Group wrote:
https://www.austingroupbugs.net/view.php?id=561#c6085 (the accepted
text) suggests that…

      versions,  the  size  is  typically in the range 92 to 108. An 
application can
      deduce the size by using <tt>sizeof(((struct sockaddr_un 
*)0)->sun_path)</tt>.

… but I was recently told that attempting that is UB because it
dereferences a nil pointer, even though it’s only within a sizeof,


(Apologies, I meant to reply list; doing so now.)

The text of the standard doesn't support this (6.5.3.4):

"The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
integer constant."

Since the operand isn't evaluated, there is no null pointer dereference. It is the act of dereference (via evaluation) which causes the UB, not its potential presence in the code. Consider that when a potential null pointer exists on a conditional branch that isn't taken, it is universally agreed that there is no UB due to the "potential" dereference. Otherwise null pointer checks in code would be useless.

Regards.


  • probable UB in recen... Thorsten Glaser via austin-group-l at The Open Group
    • Re: probable UB... Steffen Nurpmeso via austin-group-l at The Open Group
    • Re: probable UB... Davin McCall via austin-group-l at The Open Group
      • Re: probabl... Thorsten Glaser via austin-group-l at The Open Group
        • Re: pro... Davin McCall via austin-group-l at The Open Group
        • Re: pro... Harald van Dijk via austin-group-l at The Open Group

Reply via email to