Re: Avoid resource leak (contrib/postgres_fdw/connection.c) - Mailing list pgsql-hackers

From Fujii Masao
Subject Re: Avoid resource leak (contrib/postgres_fdw/connection.c)
Date
Msg-id CAHGQGwG2iC2LeizHiCezb-D=G6RE-ACXbFVfeAtzXwjnMTveGA@mail.gmail.com
Whole thread
In response to Avoid resource leak (contrib/postgres_fdw/connection.c)  (Ranier Vilela <ranier.vf@gmail.com>)
Responses Re: Avoid resource leak (contrib/postgres_fdw/connection.c)
List pgsql-hackers
On Mon, Mar 16, 2026 at 8:46 PM Ranier Vilela <ranier.vf@gmail.com> wrote:
>
> Hi.
>
> Per Coverity.
>
> CID 1645716: (#1 of 1): Resource leak (RESOURCE_LEAK)
> 8. leaked_storage: Variable str going out of scope leaks the storage str.data points to.
>
> The function *postgres_fdw_connection* leaks the contents of
> var str.data
> Once that function *cstring_to_text* palloc the contents
> must be necessary to free the var str.data.

It seems that postgres_fdw_connection() is expected to be called by
ForeignServerConnectionString() via OidFunctionCall3(),
and to allocate memory (including str.data) in the FDWConnectionContext
memory context created by ForeignServerConnectionString().

After calling postgres_fdw_connection(), ForeignServerConnectionString()
deletes FDWConnectionContext. So it seems to me that any memory
allocated in that context, including str.data, would not leak. No?

Regards,

--
Fujii Masao



pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: Add starelid, attnum to pg_stats and leverage this in pg_dump
Next
From: Fujii Masao
Date:
Subject: Re: Shutdown indefinitely stuck due to unflushed FPI_FOR_HINT record