Re: Pointer subtraction with a null pointer

From: Andres Freund <andres(at)anarazel(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, Robert Haas <robertmhaas(at)gmail(dot)com>, Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
Subject: Re: Pointer subtraction with a null pointer
Date: 2022-03-26 18:08:59
Message-ID: 20220326180859.iqm6qwywnvtee7o6@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2022-03-26 10:49:53 -0700, Andres Freund wrote:
> > It's hard to see how that isn't a flat-out compiler bug.
>
> It only happens if the NULL is directly passed as an argument to the macro,
> not if there's an intermediary variable. Argh.
>
>
> #include <stddef.h>
>
> #define relptr_store(base, rp, val) \
> ((rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
>
> typedef union { struct foo *relptr_type; size_t relptr_off; } relptr;
>
> void
> problem_not_present(relptr *rp, char *base)
> {
> struct foo *val = NULL;
>
> relptr_store(base, *rp, val);
> }
>
> void
> problem_present(relptr *rp, char *base)
> {
> relptr_store(base, *rp, NULL);
> }
>
>
> Looks like that warning is uttered whenever there's a subtraction from a
> pointer with NULL, even if the code isn't reachable. Which I guess makes
> *some* sense, outside of macros it's not something that'd ever be reasonable.

Reported as https://github.com/llvm/llvm-project/issues/54570

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2022-03-26 18:13:56 Re: Pointer subtraction with a null pointer
Previous Message Tom Lane 2022-03-26 17:57:39 Re: [PATCH] Expose port->authn_id to extensions and triggers