On 2025-03-12 13:29, Collin Funk wrote:
> This is reported in Gnulib here [1].
> 
> It is not a bug, in the Gnulib documentation there is a warning about
> this [2]:

Cough, cough.

I cannot find anywhere in the GCC or Clang documentation that displacement
of null pointers is allowed (i.e. that it constitutes a documented
extension).

Therefore, an expression like ((char *) 0) + 1 means "invoke undefined
behavior here", allowing an optimizer to assume that the code is
unreachable, with whatever consequences that further entails.

The offsetof macro has widely been implemented using null pointer arithmetic.
Compilers which come with <stddef.h> whose offsetof is implemented
in that traditional way are de facto defining null pointer arithmetic
as a documented extension. This is because the include file they provide
is human readable, and is processed in the ordinary way like any other
file: whatever the file depends on must be defined for the sake of
that file.

Note, though, it looks like Clang does not use the traditional trick
for defining offsetof; it uses __builtin_offsetof:

https://clang.llvm.org/doxygen/____stddef__offsetof_8h_source.html

Therefore in the case of Clang, we cannot look to its offsetof
definition to hypothesize that it has a null pointer arithmetic
extension.


Reply via email to