public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Willy Tarreau <w@1wt.eu>
Cc: "Thomas Weißschuh" <linux@weissschuh.net>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/3] tools/nolibc: use __builtin_offsetof()
Date: Sat, 4 Apr 2026 16:29:43 +0100	[thread overview]
Message-ID: <20260404162943.4037f824@pumpkin> (raw)
In-Reply-To: <adDNEEmZrqi-IiYp@1wt.eu>

On Sat, 4 Apr 2026 10:34:24 +0200
Willy Tarreau <w@1wt.eu> 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@1wt.eu>
> 
> thanks,
> Willy
> 


  reply	other threads:[~2026-04-04 15:29 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-01 15:07 [PATCH 0/3] tools/nolibc: add support for asprintf() Thomas Weißschuh
2026-04-01 15:07 ` [PATCH 1/3] tools/nolibc: use __builtin_offsetof() Thomas Weißschuh
2026-04-04  8:34   ` Willy Tarreau
2026-04-04 15:29     ` David Laight [this message]
2026-04-01 15:07 ` [PATCH 2/3] selftests/nolibc: test the memory allocator Thomas Weißschuh
2026-04-04  8:51   ` Willy Tarreau
2026-04-01 15:07 ` [PATCH 3/3] tools/nolibc: add support for asprintf() Thomas Weißschuh
2026-04-04  8:53   ` Willy Tarreau
2026-04-04 15:34   ` David Laight
2026-04-05 15:39     ` Thomas Weißschuh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260404162943.4037f824@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox