pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andres Freund <andres@anarazel.de>
Cc: pgsql-hackers@lists.postgresql.org, Robert Haas <robertmhaas@gmail.com>
Cc: Thomas Munro <thomas.munro@gmail.com>
Subject: Re: Pointer subtraction with a null pointer
Date: Sat, 26 Mar 2022 13:23:34 -0400
Message-ID: <55128.1648315414@sss.pgh.pa.us> (raw)
In-Reply-To: <51678.1648312664@sss.pgh.pa.us>
References: <48826.1648310694@sss.pgh.pa.us>
	<49360.1648311192@sss.pgh.pa.us>
	<20220326163441.qzpcioycozbzarsf@alap3.anarazel.de>
	<51678.1648312664@sss.pgh.pa.us>

I wrote:
> Andres Freund <andres@anarazel.de> writes:
>> On 2022-03-26 12:13:12 -0400, Tom Lane wrote:
>>> This code is old, but mylodon wasn't doing that a week ago, so
>>> Andres must've updated the compiler and/or changed its options.

>> Yep, updated it to clang 13. It's a warning present in 13, but not in 12.

> OK, that answers that.

... Actually, after looking closer, I misread what our code is doing.
These call sites are trying to set the relptr value to "null" (zero),
and AFAICS it should be allowed:

freepage.c:188:2: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
        relptr_store(base, fpm->btree_root, (FreePageBtree *) NULL);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../../src/include/utils/relptr.h:63:59: note: expanded from macro 'relptr_store'
         (rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
                                                ~~~~~~~~~~~~~~~~ ^

clang is complaining about the subtraction despite it being inside
a conditional arm that cannot be reached when val is null.  It's hard
to see how that isn't a flat-out compiler bug.

However, granting that it isn't going to get fixed right away,
we could replace these call sites with "relptr_store_null()",
and maybe get rid of the conditional in relptr_store().

			regards, tom lane





view thread (14+ messages)  latest in thread

Message-ID: <55128.1648315414@sss.pgh.pa.us>
Permalink:  ../55128.1648315414@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/55128.1648315414@sss.pgh.pa.us

 · 

reply

Reply instructions:

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

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, andres@anarazel.de, robertmhaas@gmail.com, thomas.munro@gmail.com
  Subject: Re: Pointer subtraction with a null pointer
  In-Reply-To: <55128.1648315414@sss.pgh.pa.us>

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

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox