]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
IMPORT: cebtree: Replace offset calculation with offsetof to avoid UB
authorBen Kallus <benjamin.p.kallus.gr@dartmouth.edu>
Wed, 29 Oct 2025 12:38:51 +0000 (08:38 -0400)
committerWilly Tarreau <w@1wt.eu>
Fri, 7 Nov 2025 06:32:58 +0000 (07:32 +0100)
This is the same as the equivalent fix in ebtree:

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.

This is cebtree commit 2d08958858c2b8a1da880061aed941324e20e748.

include/import/cebtree-prv.h

index 221968af59eac76eae52914d61e09c191b5c7510..e20bff0ca6b53daf841c621eed040c404cbab586 100644 (file)
@@ -555,7 +555,7 @@ struct ceb_node *_ceb_descend(struct ceb_root **root,
        /* the parent will be the (possibly virtual) node so that
         * &lparent->l == root, i.e. container_of(root, struct ceb_node, b[0]).
         */
-       lparent = (struct ceb_node *)((char *)root - (long)&((struct ceb_node *)0)->b[0]);
+       lparent = (struct ceb_node *)((char *)root - offsetof(struct ceb_node, b));
        gparent = lparent;
        if (ret_nparent)
                *ret_nparent = NULL;