haproxy/include/import
Ben Kallus 1e48ec7f6c CLEANUP: include: replace hand-rolled offsetof to avoid UB
The C standard specifies that it's undefined behavior to dereference
NULL (even if you use & right after). The hand-rolled offsetof idiom
&(((s*)NULL)->f) is thus technically undefined. This clutters the
output of UBSan and is simple to fix: just use the real offsetof when
it's available.

Note that there's no clear statement about this point in the spec,
only several points which together converge to this:

- From N3220, 6.5.3.4:
  A postfix expression followed by the -> operator and an identifier
  designates a member of a structure or union object. The value is
  that of the named member of the object to which the first expression
  points, and is an lvalue.

- From N3220, 6.3.2.1:
  An lvalue is an expression (with an object type other than void) that
  potentially designates an object; if an lvalue does not designate an
  object when it is evaluated, the behavior is undefined.

- From N3220, 6.5.4.4 p3:
  The unary & operator yields the address of its operand. If the
  operand has type "type", the result has type "pointer to type". If
  the operand is the result of a unary * operator, neither that operator
  nor the & operator is evaluated and the result is as if both were
  omitted, except that the constraints on the operators still apply and
  the result is not an lvalue. Similarly, if the operand is the result
  of a [] operator, neither the & operator nor the unary * that is
  implied by the [] is evaluated and the result is as if the & operator
  were removed and the [] operator were changed to a + operator.

=> In short, this is saying that C guarantees these identities:
    1. &(*p) is equivalent to p
    2. &(p[n]) is equivalent to p + n

As a consequence, &(*p) doesn't result in the evaluation of *p, only
the evaluation of p (and similar for []). There is no corresponding
special carve-out for ->.

See also: https://pvs-studio.com/en/blog/posts/cpp/0306/

After this patch, HAProxy can run without crashing after building w/
clang-19 -fsanitize=undefined -fno-sanitize=function,alignment
2025-07-25 17:54:32 +02:00
..
atomic-ops.h MAJOR: threads/plock: update the embedded library again 2023-08-11 19:03:35 +02:00
cebtree-prv.h CLEANUP: assorted typo fixes in the code and comments 2025-04-02 11:12:20 +02:00
cebtree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebu32_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebu64_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebua_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebub_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebuib_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebuis_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebul_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
cebus_tree.h IMPORT: import cebtree (compact elastic binary trees) 2024-09-15 23:44:59 +02:00
eb32sctree.h REORG: ebtree: split structures into their own file ebtree-t.h 2021-10-07 01:41:14 +02:00
eb32tree.h REORG: ebtree: split structures into their own file ebtree-t.h 2021-10-07 01:41:14 +02:00
eb64tree.h REORG: ebtree: split structures into their own file ebtree-t.h 2021-10-07 01:41:14 +02:00
ebimtree.h BUG/MEDIUM: ebtree: use a byte-per-byte memcmp() to compare memory blocks 2020-06-16 11:30:33 +02:00
ebistree.h CLEANUP: assorted typo fixes in the code and comments 2024-09-03 17:49:21 +02:00
ebmbtree.h OPTIM: ebtree: make ebmb_insert_prefix() keep a copy the new node's key 2022-11-15 09:37:09 +01:00
ebpttree.h REORG: ebtree: split structures into their own file ebtree-t.h 2021-10-07 01:41:14 +02:00
ebsttree.h CLEANUP: assorted typo fixes in the code and comments 2024-09-03 17:49:21 +02:00
ebtree-t.h REORG: ebtree: split structures into their own file ebtree-t.h 2021-10-07 01:41:14 +02:00
ebtree.h IMPORT: ebtree: make string_equal_bits() return an unsigned 2024-01-06 13:35:42 +01:00
ist.h MINOR: ist: define iststrip() new function 2024-04-26 11:29:25 +02:00
lru.h REORG: ebtree: move the include files from ebtree to include/import/ 2020-06-11 09:31:11 +02:00
mjson.h MINOR: sample: converter: Add mjson library. 2021-04-15 17:05:38 +02:00
mt_list.h CLEANUP: include: replace hand-rolled offsetof to avoid UB 2025-07-25 17:54:32 +02:00
plock.h Revert "IMPORT: plock: export the uninlined version of the lock wait function" 2025-02-07 19:51:15 +01:00
sha1.h IMPORT: sha1: import SHA1 functions 2017-10-25 04:45:48 +02:00
slz-tables.h BUILD: tree-wide: fix a few missing includes in a few files 2024-03-05 11:50:34 +01:00
slz.h IMPORT: slz: use a better hash for machines with a fast multiply 2025-05-16 16:43:53 +02:00
xxhash.h BUILD: xxhash: silence a build warning on Solaris + gcc-5.5 2024-04-17 09:43:32 +02:00