[PATCH] Fix potential memoryleak in guc.c

Started by Zhang, Jieover 7 years ago2 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t40742
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 28, 2026 at 04:11 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t40742_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t40742_1 && git checkout t40742_1

Patchset v1 (message #1) is on t40742_1

Jump to latest
#1Zhang, Jie
zhangjie2@cn.fujitsu.com

Hi all

In src\backend\utils\misc\guc.c, I found a potential memory leak.

make_absolute_path() return a malloc'd copy, we should free memory before the function return false.
----------------------------------------------------------------------------
SelectConfigFiles(const char *userDoption, const char *progname)
{
......
/* configdir is -D option, or $PGDATA if no -D */
if (userDoption)
configdir = make_absolute_path(userDoption); ★
else
configdir = make_absolute_path(getenv("PGDATA")); ★

if (configdir && stat(configdir, &stat_buf) != 0)
{
write_stderr("%s: could not access directory \"%s\": %s\n",
progname,
configdir,
strerror(errno));
if (errno == ENOENT)
write_stderr("Run initdb or pg_basebackup to initialize a PostgreSQL data directory.\n");
★// Need to free memory of configdir
return false;
}
......
---------------------------------------------------------------------------

Refer to the following files for the implementation of make_absolute_path().

file: src\port\path.c
/*
* make_absolute_path
*
* If the given pathname isn't already absolute, make it so, interpreting
* it relative to the current working directory.
*
* Also canonicalizes the path. The result is always a malloc'd copy.

Attachments:

t40742_1
guc.patchapplication/octet-stream; name=guc.patchDownload+1-0
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Zhang, Jie (#1)
Re: [PATCH] Fix potential memoryleak in guc.c

"Zhang, Jie" <zhangjie2@cn.fujitsu.com> writes:

In src\backend\utils\misc\guc.c, I found a potential memory leak.
make_absolute_path() return a malloc'd copy, we should free memory before the function return false.

If SelectConfigFiles were executed more than once per postmaster
launch, this might be worth adding code for ... but as-is, I'm
dubious. There are a few tens of KB of other one-time leaks
that we don't worry about removing.

Even more to the point, the particular code path you're complaining
about is a failure exit that will lead to immediate process
termination, so there really is no point in adding code there.

regards, tom lane