Re: [PATCH 1/3] tools/nolibc: use __builtin_offsetof()

From: David Laight

Date: Sat Apr 04 2026 - 11:29:50 EST


On Sat, 4 Apr 2026 10:34:24 +0200
Willy Tarreau <w@xxxxxx> wrote:

> Hi Thomas,
>
> On Wed, Apr 01, 2026 at 05:07:27PM +0200, Thomas Weißschuh wrote:
> > The current custom implementation of offsetof() fails UBSAN:
> > runtime error: member access within null pointer of type 'struct ...'
> > This means that all its users, including container_of(), free() and
> > realloc(), fail.
> >
> > Use __builtin_offsetof() instead which does not have this issue and
> > has been available since GCC 4 and clang 4.
>
> Yeah, that's a place where I find the standard ambiguous and ridiculously
> absurd (since there's no dereference, only an address calculations), but I
> had to do the same in haproxy recently for the same reasons, and I didn't
> remember that we had it in nolibc as well. So that's an obvious ack!

My guess it is all because the C standard allows NULL to have any
bit-pattern. If it isn't the 'all zero' pattern then the arithmetic
on the NULL pointer gives the wrong answer.

Trouble is pretty much all C assumes that NULL is 0.
Even the C for ICL's VME-B used 0 for NULL although the 'native' value
would have been ~0. Too much code would have been broken.

So it is technically not portable, but will always generate the
correct answer.

David

>
> Acked-by: Willy Tarreau <w@xxxxxx>
>
> thanks,
> Willy
>