Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ieKoy-0003bK-2x for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Dec 2019 15:19:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ieKow-0003LB-Dz for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Dec 2019 15:19:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ieKow-0003Ky-1b for pgsql-bugs@lists.postgresql.org; Mon, 09 Dec 2019 15:19:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ieKot-0006ey-GT for pgsql-bugs@lists.postgresql.org; Mon, 09 Dec 2019 15:19:32 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id xB9FJRAO011416; Mon, 9 Dec 2019 10:19:28 -0500 From: Tom Lane To: cilizili@protonmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16157: handling of pg_malloc(0) In-reply-to: <16157-3a757ffed31f8579@postgresql.org> References: <16157-3a757ffed31f8579@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Mon, 09 Dec 2019 12:44:01 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11414.1575904767.1@sss.pgh.pa.us> Date: Mon, 09 Dec 2019 10:19:27 -0500 Message-ID: <11415.1575904767@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > While checking code, I found a potential bug. To avoid unportable behavior > of malloc(0), pg_malloc family functions in fe_memutils.c replace the size 0 > with 1. I think 1 is poor, any size of chunk enought for structure or > pointer may be required. I don't see your point. The reason we're doing anything special here is that the C/POSIX standard for malloc() says If the size of the space requested is 0, the behavior is implementation-defined: either a null pointer shall be returned, or the behavior shall be as if the size were some non-zero value, except that the behavior is undefined if the returned pointer is used to access an object. We don't want the first behavior, so we're forcing it to be the second one. But the behavior is still undefined if you try to actually store anything in the returned space, so there's no point in making it any bigger than we have to. Indeed, doing so would make it easier for buggy code that tries to store something there to escape detection. > For example, in initdb.c:L508, if nlines equals to 2^32-1, > result = (char **) pg_malloc((nlines + 1) * sizeof(char *)); Well, arguably it's a bug that this code isn't being careful about integer overflow in the size request, but no amount of hacking of pg_malloc() will fix that; the bug is in the caller. (In practice, since initdb is only run with known and very finite inputs, there is zero point in complicating this particular logic with overflow worries. Elsewhere, we'd more likely want to fix it.) regards, tom lane