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 1oWesR-00087s-MQ for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Sep 2022 14:21:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oWesQ-00083V-KK for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Sep 2022 14:21: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 1oWesP-00083L-Fe for pgsql-hackers@lists.postgresql.org; Fri, 09 Sep 2022 14:21:02 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oWesL-0000k9-Op for pgsql-hackers@lists.postgresql.org; Fri, 09 Sep 2022 14:21:01 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 968C23200124; Fri, 9 Sep 2022 10:20:53 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Fri, 09 Sep 2022 10:20:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1662733253; x=1662819653; bh=W C9wBdfRckPAQYDf7A207ngz+LhlldCZh0v/+P/X01U=; b=0NQZV25gDlu6eHjmM TNjmF6rNgfbZgRTQD1alPrAnNNgP8UI6Y2Q7+titfyqZWMBx+NWReOyHLqoOJcts w7qKSPqHuly/uDIsQl/FB4+eLTNcJTIUOG2ePfDbV8SD6Mlii+4o1NEHUyBRHxK9 fpwTS5mwkhClaSf/FTxjVWw5vqFXCTaqXEl9LkULtye0r59NjCbHFskTc3+TDoWS AWaT+ze5eliiIMY12w1kdfiYDBWuEpFpnSlrZhh54jy1Q072Uv3K1B9aBt1w2/8v rJunFQINK2AgFi902ae0vPIrX6NwQCy+EnEweSogSglQ2MkYSZS/Irm254yQON7n fJ2ZQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrfedthedgjeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 9 Sep 2022 10:20:52 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 4306F389; Fri, 9 Sep 2022 16:20:50 +0200 (CEST) Date: Fri, 9 Sep 2022 16:20:50 +0200 From: Alvaro Herrera To: Tom Lane Cc: Junwang Zhao , PostgreSQL Hackers Subject: Re: [PATCH v1] fix potential memory leak in untransformRelOptions Message-ID: <20220909142050.3vv2hjekppk265dd@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <403275.1662041404@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2022-Sep-01, Tom Lane wrote: > Junwang Zhao writes: > > result = lappend(result, makeDefElem(pstrdup(s), val, -1)); > > + pfree(s); > > I wonder why it's pstrdup'ing s in the first place. Yeah, I think both the pstrdups in that function are useless. The DefElems can just point to the correct portion of the (already pstrdup'd by TextDatumGetCString) copy of optiondatums[i]. We modify that copy to install \0 in the place where the = is, and that copy is not freed anywhere. diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c index 609329bb21..0aa4b334ab 100644 --- a/src/backend/access/common/reloptions.c +++ b/src/backend/access/common/reloptions.c @@ -1357,9 +1357,9 @@ untransformRelOptions(Datum options) if (p) { *p++ = '\0'; - val = (Node *) makeString(pstrdup(p)); + val = (Node *) makeString(p); } - result = lappend(result, makeDefElem(pstrdup(s), val, -1)); + result = lappend(result, makeDefElem(s, val, -1)); } return result; I think these pstrdups were already not necessary when the function was added in 265f904d8f25, because textout() was already known to return a palloc'ed copy of its input; but later 220db7ccd8c8 made this contract even more explicit. Keeping 's' and removing the pstrdups better uses memory, because we have a single palloc'ed chunk per option rather than two. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/