From 8d8e8d57fbb75b47458eee50d3161a94b4dffe85 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@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

