Re: [PATCH] ACPI: avoid NULL pointer arithmetic

From: Arnd Bergmann
Date: Thu Sep 30 2021 - 16:37:21 EST


On Thu, Sep 30, 2021 at 8:52 PM Rafael J. Wysocki <rafael@xxxxxxxxxx> wrote:
>
> On Wed, Sep 29, 2021 at 8:47 PM Rafael J. Wysocki <rafael@xxxxxxxxxx> wrote:
> >
> > On Mon, Sep 27, 2021 at 2:13 PM Arnd Bergmann <arnd@xxxxxxxxxx> wrote:
> > >
> > > #define ACPI_TO_POINTER(i) ACPI_CAST_PTR (void, (acpi_size) (i))
> > > -#define ACPI_TO_INTEGER(p) ACPI_PTR_DIFF (p, (void *) 0)
> > > -#define ACPI_OFFSET(d, f) ACPI_PTR_DIFF (&(((d *) 0)->f), (void *) 0)
> > > +#define ACPI_TO_INTEGER(p) ((uintptr_t)(p))
> > > +#define ACPI_OFFSET(d, f) offsetof(d, f)
> > > #define ACPI_PHYSADDR_TO_PTR(i) ACPI_TO_POINTER(i)
> > > #define ACPI_PTR_TO_PHYSADDR(i) ACPI_TO_INTEGER(i)
> > >
> > > --
> >
> > Queued up as 5.16 material, converted into an upstream ACPICA pull
> > request and submitted, thanks!
>
> And reverted from there, because it introduced build issues.
>
> Can we use alternative definitions that don't depend on uintptr_t and
> offsetof()?

It's a bit tricky, as both were introduced to avoid portability issues.

For uintptr_t, we could use 'unsigned long', which works on everything
that Linux can run on, but wouldn't work if this code can be compiled
for 64-bit Windows. 'size_t' probably works, but likely has the same problem
as 'uintptr_t' because they require and additional #include. I see
that some code uses acpi_uintptr_t, which looks like it is meant to
replace uintptr_t, this is defined as 'void *' in include/acpi/actypes.h,
so that probably wouldn't avoid the warning.

For offsetof(), we could use __builtin_offsetof(), which would work with
any gcc-compatible compiler, if the goal is to avoid including <stddef.h>.
If it has to work on other compilers, there is no portable way that doesn't
rely on standard headers. The best idea I'd have would be to use
"#ifdef offsetof" to choose between the trivial implementation I had
and the old one that works for non-standard C but may invoke
undefined behavior.

Arnd