From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Junwang Zhao <zhjwpku@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: [PATCH v1] fix potential memory leak in untransformRelOptions
Date: Fri, 9 Sep 2022 16:20:50 +0200
Message-ID: <20220909142050.3vv2hjekppk265dd@alvherre.pgsql> (raw)
In-Reply-To: <403275.1662041404@sss.pgh.pa.us>
On 2022-Sep-01, Tom Lane wrote:
> Junwang Zhao <zhjwpku@gmail.com> 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.
inline patch 1: src/backend/access/common/reloptions.c (+2/-2 lines) download inline-1.patchdiff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.cindex 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/
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: alvherre@alvh.no-ip.org, tgl@sss.pgh.pa.us, zhjwpku@gmail.com
Subject: Re: [PATCH v1] fix potential memory leak in untransformRelOptions
In-Reply-To: <20220909142050.3vv2hjekppk265dd@alvherre.pgsql>
References: <403275.1662041404@sss.pgh.pa.us> <20220909142050.3vv2hjekppk265dd@alvherre.pgsql>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox