> I think so as well, IMHO it results from an initial omission that turned
into yet another stupid rule, because we all know that in practice it's
not evaluated, and the proof being that so many programs use that idioů
without failing since the lvalue is not evaluated. And going further,
since at least the end of the 70s, x86 processors have adopted the "LEA"
instruction in addition to the "MOV" instruction to do exactly this:
return the effective address of an expression instead of evaluating it,
which is used everywhere there's a "&p->q", and by extension everywhere
people want to perform two additions at once.

> And the fact that this line is missing also indicates to me that it was
simply overlooked, similar to the fact that it's not listed in the long
list of known UBs in C23. But anyway there"s a trend these days in
compilers to try to find UBs and exploit them to their maximum in order
to bring optimizations that make no sense at all and only result in
breaking long-working programs :-/

Agreed. For this reason, I think getting in touch with some friends on
the ISO committee might be in order.

> Basically he suggests just using a non-zero value as the base in the
pointer calculation to work around the undefined in the offsetof()
macro. I think I'll change the offsetof() definition to rely on that
one when not having __builtin_offsetof(), at least so that the idiom
is not recopied everywhere. I'll just double-check that it's not
affected by __attribute__((packed)).

I'm pretty sure that the suggested workaround doesn't actually fix the
UB. Ultimately, the UB stems from the fact that `((struct whatever
*)NULL)->field` is not a valid object. `((struct whatever
*)128)->field` is *also* not a valid object, so the UB remains. While
this may satisfy UBSan for now, I wouldn't be surprised if a future
UBSan would still complain about this.

> Do you mind if I split your patch into multiple ones ? It addresses
at least two distinct classes, one being the void* and the other being
this issue. In this case I'd edit your commit message to add the
references above.

Of course; feel free to make any changes you like.

Thanks,
Ben


Reply via email to