Fix small memory leak in get_dbname_oid_list_from_mfile(). master github/master
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 15 Mar 2026 19:24:04 +0000 (15:24 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 15 Mar 2026 19:24:04 +0000 (15:24 -0400)
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

index 9b4b151b318a57a78ca84167e28f645391a372ca..91efe30565094951db78ded7672ed790d58301f2 100644 (file)
@@ -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. */