News for hackers. Stuff that matters.
Re:Undefined behavior in C is a reading error by a1369209993 on Thursday May 20, 2021 @10:02PM (#27229728)
Actually, there's a even worse version:

  struct foo { ...; bar_t bar[NBAR]; };
  struct foo* p = ...;
  bar_t* q = &p->bar[0]; // add rq, rp, #foo_bar_offs
  // other declarations
  
  if(!p) return NOPE; // optimized out
  // use p and q
Not even any dereferencing, just pointer arithmetic.
Re:Undefined behavior in C is a reading error by tom_mellior on Friday May 21, 2021 @03:49PM (#27239036)
Could you post a complete example please and tell us which compiler optimizes out this check? Clang and GCC don't: https://gcc.godbolt.org/z/dbTadEcra
Re:Undefined behavior in C is a reading error by schlupa on Friday May 21, 2021 @01:48PM (#27237403)
Yes, that one is especially nasty as &p->bar[0] is a constant expression completely solvable at compile time. It's equivalent to the offsetof() macro of stddef.h