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 1nYAvL-0001Yw-Nf for pgsql-hackers@arkaria.postgresql.org; Sat, 26 Mar 2022 18:14:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nYAvK-0001LE-Ln for pgsql-hackers@arkaria.postgresql.org; Sat, 26 Mar 2022 18:14:02 +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 1nYAvK-0001L5-CN for pgsql-hackers@lists.postgresql.org; Sat, 26 Mar 2022 18:14:02 +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 1nYAvI-0002Oi-Fc for pgsql-hackers@lists.postgresql.org; Sat, 26 Mar 2022 18:14:02 +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 22QIDuXc103099; Sat, 26 Mar 2022 14:13:56 -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: <20220326174953.gdeasrl5wzal42ic@alap3.anarazel.de> References: <48826.1648310694@sss.pgh.pa.us> <49360.1648311192@sss.pgh.pa.us> <20220326163441.qzpcioycozbzarsf@alap3.anarazel.de> <51678.1648312664@sss.pgh.pa.us> <55128.1648315414@sss.pgh.pa.us> <20220326174953.gdeasrl5wzal42ic@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Sat, 26 Mar 2022 10:49:53 -0700" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <103039.1648318377.0@sss.pgh.pa.us> Date: Sat, 26 Mar 2022 14:13:56 -0400 Message-ID: <103098.1648318436@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <103039.1648318377.1@sss.pgh.pa.us> Andres Freund writes: > Wonder if we should try to get rid of the problem by also fixing the double > evaluation of val? I think something like Good idea. The attached also silences the warning, and getting rid of the double-eval hazard seems like a net win. > Might also be worth adding an assertion that base < val. Did that too. On the whole I like this better. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="silence-null-subtraction-warning-2.patch"; charset="us-ascii" Content-ID: <103039.1648318377.2@sss.pgh.pa.us> Content-Description: silence-null-subtraction-warning-2.patch diff --git a/src/include/utils/relptr.h b/src/include/utils/relptr.h index fdc2124d2c..cc80a7200d 100644 --- a/src/include/utils/relptr.h +++ b/src/include/utils/relptr.h @@ -56,11 +56,24 @@ #define relptr_is_null(rp) \ ((rp).relptr_off == 0) +/* We use this inline to avoid double eval of "val" in relptr_store */ +static inline Size +relptr_store_eval(char *base, char *val) +{ + if (val == NULL) + return 0; + else + { + Assert(val > base); + return val - base; + } +} + #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P #define relptr_store(base, rp, val) \ (AssertVariableIsOfTypeMacro(base, char *), \ AssertVariableIsOfTypeMacro(val, __typeof__((rp).relptr_type)), \ - (rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base))) + (rp).relptr_off = relptr_store_eval(base, (char *) (val))) #else /* * If we don't have __builtin_types_compatible_p, assume we might not have @@ -68,7 +81,7 @@ */ #define relptr_store(base, rp, val) \ (AssertVariableIsOfTypeMacro(base, char *), \ - (rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base))) + (rp).relptr_off = relptr_store_eval(base, (char *) (val))) #endif #define relptr_copy(rp1, rp2) \ ------- =_aaaaaaaaaa0--