Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYA8d-0006pN-6a for pgsql-hackers@arkaria.postgresql.org; Sat, 26 Mar 2022 17:23:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nYA8c-0004ac-3z for pgsql-hackers@arkaria.postgresql.org; Sat, 26 Mar 2022 17:23:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYA8b-0004aT-RH for pgsql-hackers@lists.postgresql.org; Sat, 26 Mar 2022 17:23:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nYA8Z-0001y9-Ib for pgsql-hackers@lists.postgresql.org; Sat, 26 Mar 2022 17:23:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 22QHNYOm055129; Sat, 26 Mar 2022 13:23:34 -0400 From: Tom Lane To: Andres Freund cc: pgsql-hackers@lists.postgresql.org, Robert Haas , Thomas Munro Subject: Re: Pointer subtraction with a null pointer 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> Comments: In-reply-to Tom Lane message dated "Sat, 26 Mar 2022 12:37:44 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <55127.1648315414.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 26 Mar 2022 13:23:34 -0400 Message-ID: <55128.1648315414@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I wrote: > Andres Freund 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 1= 2. > 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 poin= ter 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 'r= elptr_store' (rp).relptr_off =3D ((val) =3D=3D 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