Hi,

> Having written that, I did test that 'sizeof(((struct
> sockaddr_un*)0)->sun_path)' compiles with gcc, although I'm less
> certain of whether the C standard permits that (or even if that
> permission has changed over time) - the expression argument to sizeof
> is unevaluated, which counters the argument that you can't normally
> evaluate a dereference of a NULL pointer.

According to ISO/IEC 9899, versions 1999 to 2017 at least, the answer seems 
clear from section 6.5.3.4 ("The sizeof operator" or "The sizeof and _Alignof 
operators"), paragraph 2:

"""
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.
"""

Other relevant sections, especially 6.5.2.3 and in particular its paragraphs 2 
and 4, are not in contradiction with the above text.

So in short, yes, IMHO, the standard allows something like:
'sizeof(((struct sockaddr_un*)0)->sun_path)'
where in fact 0 could be replaced by any other invalid pointer.

Regards.

-- 
Olivier Certner


  • [1003.1(2008... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [1003.1... Austin Group Bug Tracker via austin-group-l at The Open Group
      • Re:... Steffen Nurpmeso via austin-group-l at The Open Group
        • ... Eric Blake via austin-group-l at The Open Group
          • ... Eric Blake via austin-group-l at The Open Group
            • ... Olivier Certner via austin-group-l at The Open Group
              • ... Steffen Nurpmeso via austin-group-l at The Open Group
                • ... Steffen Nurpmeso via austin-group-l at The Open Group
                • ... Geoff Clare via austin-group-l at The Open Group
                • ... Steffen Nurpmeso via austin-group-l at The Open Group
                • ... Thorsten Glaser via austin-group-l at The Open Group
            • ... Steffen Nurpmeso via austin-group-l at The Open Group
    • [1003.1... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [1003.1... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [1003.1... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [1003.1... Austin Group Bug Tracker via austin-group-l at The Open Group

Reply via email to