From bb53b8d359d33f10b6274be743c42f6e8ecfbb84 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 15 Mar 2026 15:24:04 -0400 Subject: [PATCH] Fix small memory leak in get_dbname_oid_list_from_mfile(). Coverity complained that this function leaked the dumpdirpath string, which it did. But we don't need to make a copy at all, because there's not really any point in trimming trailing slashes from the directory name here. If that were needed, the initial file_exists_in_directory() test would have failed, since it doesn't bother with that (and neither does anyplace else in this file). Moreover, if we did want that, reimplementing canonicalize_path() poorly is not the way to proceed. Arguably, all of this code should be reexamined with an eye to using src/port/path.c's facilities, but for today I'll settle for getting rid of the memory leak. --- src/bin/pg_dump/pg_restore.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 9b4b151b318..91efe305650 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -68,7 +68,7 @@ static int restore_all_databases(const char *inputFileSpec, static int get_dbnames_list_to_restore(PGconn *conn, SimplePtrList *dbname_oid_list, SimpleStringList db_exclude_patterns); -static int get_dbname_oid_list_from_mfile(const char *dumpdirpatharg, +static int get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimplePtrList *dbname_oid_list); /* @@ -1051,14 +1051,13 @@ get_dbnames_list_to_restore(PGconn *conn, * Returns, total number of database names in map.dat file. */ static int -get_dbname_oid_list_from_mfile(const char *dumpdirpatharg, SimplePtrList *dbname_oid_list) +get_dbname_oid_list_from_mfile(const char *dumpdirpath, + SimplePtrList *dbname_oid_list) { StringInfoData linebuf; FILE *pfile; char map_file_path[MAXPGPATH]; int count = 0; - int len; - char *dumpdirpath = pstrdup(dumpdirpatharg); /* * If there is no map.dat file in the dump, then return from here as there @@ -1070,15 +1069,6 @@ get_dbname_oid_list_from_mfile(const char *dumpdirpatharg, SimplePtrList *dbname return 0; } - len = strlen(dumpdirpath); - - /* Trim slash from directory name. */ - while (len > 1 && dumpdirpath[len - 1] == '/') - { - dumpdirpath[len - 1] = '\0'; - len--; - } - snprintf(map_file_path, MAXPGPATH, "%s/map.dat", dumpdirpath); /* Open map.dat file. */