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.
This commit is contained in:
Tom Lane 2026-03-15 15:24:04 -04:00
parent a793677e57
commit bb53b8d359

View file

@ -68,7 +68,7 @@ static int restore_all_databases(const char *inputFileSpec,
static int get_dbnames_list_to_restore(PGconn *conn, static int get_dbnames_list_to_restore(PGconn *conn,
SimplePtrList *dbname_oid_list, SimplePtrList *dbname_oid_list,
SimpleStringList db_exclude_patterns); 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); 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. * Returns, total number of database names in map.dat file.
*/ */
static int 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; StringInfoData linebuf;
FILE *pfile; FILE *pfile;
char map_file_path[MAXPGPATH]; char map_file_path[MAXPGPATH];
int count = 0; 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 * 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; 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); snprintf(map_file_path, MAXPGPATH, "%s/map.dat", dumpdirpath);
/* Open map.dat file. */ /* Open map.dat file. */