Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w9rgO-001pw0-0R for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Apr 2026 21:40:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w9rgM-00CFpd-1t for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Apr 2026 21:40:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w9rgM-00CFo0-0l for pgsql-hackers@lists.postgresql.org; Mon, 06 Apr 2026 21:40:30 +0000 Received: from fout-a6-smtp.messagingengine.com ([103.168.172.149]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w9rgK-00000000yFY-19t0 for pgsql-hackers@lists.postgresql.org; Mon, 06 Apr 2026 21:40:30 +0000 Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailfout.phl.internal (Postfix) with ESMTP id 82C02EC0421; Mon, 6 Apr 2026 17:40:27 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-06.internal (MEProxy); Mon, 06 Apr 2026 17:40:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1775511627; x=1775598027; bh=4aOvSNeEPmz+egIKBkPfTb8icR9+T6h7fZ1YlAqGKK0=; b= ZUqKgAMoESs4Dcn6/5XE5JGhHh53Ekp3TX+y2Ah04e2gy49MwiXgpVu4lDoO5/Ce hdDRYBiE3ncndCwKw5vOlYcRkUakkZXKL0o/cjtE8l3zcL+7go2gEjZRjtqKsm79 cERrE4yWNUBbw/9kpAy2BFAjYBpxrmBASRN0rPGZyQf07o8kBXnlb0ij3AGlNz7v j2USJJmnHgtx2aTfNkZRiwnWcdExzo2bH+RKhd8fmkzy8AVEB73+EsqhEtHHnMm/ 768sspbU8O1shkNmQXTRk2p3+9p80rj+yoJzZGMW/+zuHpVb1MPryaMRBWt/tBAN ELpY48mrS+4Ctun6xOdDZQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1775511627; x= 1775598027; bh=4aOvSNeEPmz+egIKBkPfTb8icR9+T6h7fZ1YlAqGKK0=; b=o /7Hg6IffZ0xJTFb10HOXhBtlzudBnRgTTg0z1UlH/DCvChp+CHryFM64rskvVJzh cdbWV3g2sMvBe5ExIUPCs6dfNc6QwKm+IZRoSElDZRzJSWxLJwZqBJM5H6k/xn1W Ax23v2wQ5QU3lgN2jsY6uzhhZZziUUW+/jNPU+wgQP3htaea8tu444Kxz4NHV2yo jKpneSZh5PqzQxgvH9aHP9mwMx5fYQ8aXzDlMmh9IaXcE98BF+N4DOH0hlTGoA4N 0bRWXj9Hl+mnMInCz+B11Z8xm8yaxdhDCk4Yz+maGN2wrF2vYbsdbww0f/hC8ELE Gzgm9a8eW/OFL4FfBKx+w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgddukeekgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtugfgjgestheksfdttddtjeenucfhrhhomheptehnughrvghs ucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrfgrth htvghrnheptdelledvgfejvdffieeukeefueelfffhgeffhffgffekveeuheeihefhiefg hfdvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprg hnughrvghssegrnhgrrhgriigvlhdruggvpdhnsggprhgtphhtthhopeegpdhmohguvgep shhmthhpohhuthdprhgtphhtthhopehjrggtohgsrdgthhgrmhhpihhonhesvghnthgvrh hprhhishgvuggsrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrsheslhhi shhtshdrphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtoheprghnughrvggrshesph hrohigvghlrdhsvgdprhgtphhtthhopehgrghlkhhinhesrhhuthhokhgvnhdrrhhu X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 6 Apr 2026 17:40:26 -0400 (EDT) Date: Mon, 6 Apr 2026 17:40:26 -0400 From: Andres Freund To: Jacob Champion Cc: Andreas Karlsson , =?utf-8?B?0JPQsNC70LrQuNC9INCh0LXRgNCz0LXQuQ==?= , "pgsql-hackers@lists.postgresql.org" Subject: Re: DEREF_AFTER_NULL: src/common/jsonapi.c:2529 Message-ID: References: <0b32e30f2fb94ae3b7f4ee15bbb072c0@rutoken.ru> <107eb23a-8ebb-42bc-99c0-ca551733e94e@proxel.se> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2026-04-06 14:18:59 -0700, Jacob Champion wrote: > On Mon, Apr 6, 2026 at 11:46 AM Andres Freund wrote: > > I guess getting rid of the stringinfo/pqexpbuffer split is not that easy, but > > at least the common memory allocation stuff seems like it should be doable to > > put through through the same wrappers for both FE/BE, handling whether we want > > to error out or not by passing MCXT_ALLOC_NO_OOM or not. > > We can spell the abstraction layer differently, but how does that help > us hide the complexity of the OOM behavior? IMO the difference in > returning NULLs is the entire reason this code is difficult; the > abstraction layer must necessarily leak [1]. It's not all the complexity, but I think the various indirections do add to making it hard to understand. > > How is the only sane answer here not to avoid ever freeing NULLs? > > Maybe I didn't parse this question correctly, but I don't want to > avoid freeing NULLs. It's entirely reasonable and normal to write code > that frees NULLs. I think it's a bad idea ever call free(), realloc() etc with a NULL. It's imo quite the code smell indicating that code lost of track of whether something was allocated or not. > > Particularly because this code started out as backend code. Yea, yea, we > > probably didn't have NULLs due to erroring out on allocation failure, but > > still. > > > > Kinda seems like the fe_memutils.c pfree() should assert that the argument is > > not null. > > Maybe... but if we want to change this, I hope that we'll instead > consider not naming a function "pfree" when sometimes it is actually > "free"? The whole point of having pfree() in FE code is to make it possible to write common code that doesn't need ifdef around every allocation. I don't see what the gain of this would be. > Or else make pfree() behave like free() [2] so that we don't > have to have that particular papercut at all anymore? -many It's also not a path I want to add any unnecessary instructions to. > It still doesn't help the OOM abstraction leak between libpq and the > backend, as far as I can tell. If the code were to use a JsonLexContext field to decide whether to pass MCXT_ALLOC_NO_OOM to the allocation functions etc it'd at least would make the code more similar between FE/BE due to both having to deal with NULLs. That would require adding some optionally OOM safe functions to stringinfo, but I suspect that would be a good thing anyway (might not be able to do it with the existing functions, some paths that use stringinfo are quite perf sensitive, and it does make some code nontrivially more complicated). Greetings, Andres Freund