Re: Pointer subtraction with a null pointer
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@lists.postgresql.org, Robert Haas <robertmhaas@gmail.com>, Thomas Munro <thomas.munro@gmail.com>
Date: 2022-03-26T18:08:59Z
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
Commits
-
Suppress compiler warning in relptr_store().
- e07d4ddc55fd 15.0 landed
- 403a8c76293b 10.21 landed
- a2a1aa830a1c 11.16 landed
- cb8586d0514b 12.11 landed
- 4fbbea2c1934 13.7 landed
- 0144c9c7e73d 14.3 landed