Hi,

I experimented with Valgrind after recent changes committed by Tom [1]
and catched this:

```
25 bytes in 1 blocks are definitely lost in loss record 18 of 49
   at 0x4846828: malloc (in
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x57DE35E: strdup (strdup.c:42)
   by 0x9D9D1D: make_absolute_path (path.c:877)
   by 0x98AD5D: SelectConfigFiles (guc.c:1795)
   by 0x65F104: PostmasterMain (postmaster.c:785)
   by 0x52F2B9: main (main.c:231)
```

I propose to correct this as attached.

[1]: https://postgr.es/m/285483.1746756...@sss.pgh.pa.us
From 8d8e8d57fbb75b47458eee50d3161a94b4dffe85 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksan...@timescale.com>
Date: Wed, 13 Aug 2025 22:26:13 +0300
Subject: [PATCH v1] Fix memory leaks in SelectConfigFiles()

Make sure the memory allocated by make_absolute_path() is freed when
SelectConfigFiles() fails. It's mainly to silence Valgrind. The actual callers
exit() rapidly in such cases, so no memory actually leaks.

Aleksander Alekseev, reviewed by TODO FIXME
Discussion: TODO FIXME
---
 src/backend/utils/misc/guc.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index e404c345e6e..5b60e82bd01 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -1803,6 +1803,7 @@ SelectConfigFiles(const char *userDoption, const char *progname)
 					 configdir);
 		if (errno == ENOENT)
 			write_stderr("Run initdb or pg_basebackup to initialize a PostgreSQL data directory.\n");
+		free(configdir);
 		return false;
 	}
 
@@ -1830,6 +1831,7 @@ SelectConfigFiles(const char *userDoption, const char *progname)
 					 "You must specify the --config-file or -D invocation "
 					 "option or set the PGDATA environment variable.\n",
 					 progname);
+		free(configdir);
 		return false;
 	}
 
@@ -1882,6 +1884,7 @@ SelectConfigFiles(const char *userDoption, const char *progname)
 					 "or by the -D invocation option, or by the "
 					 "PGDATA environment variable.\n",
 					 progname, ConfigFileName);
+		free(configdir);
 		return false;
 	}
 
@@ -1934,6 +1937,7 @@ SelectConfigFiles(const char *userDoption, const char *progname)
 					 "or by the -D invocation option, or by the "
 					 "PGDATA environment variable.\n",
 					 progname, ConfigFileName);
+		free(configdir);
 		return false;
 	}
 	SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
@@ -1965,6 +1969,7 @@ SelectConfigFiles(const char *userDoption, const char *progname)
 					 "or by the -D invocation option, or by the "
 					 "PGDATA environment variable.\n",
 					 progname, ConfigFileName);
+		free(configdir);
 		return false;
 	}
 	SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
-- 
2.43.0

Reply via email to