Always pfree strings returned by GetDatabasePath
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 25 Apr 2022 08:32:13 +0000 (10:32 +0200)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 25 Apr 2022 08:32:13 +0000 (10:32 +0200)
Several places didn't do it, and in many cases it didn't matter because
it would be a small allocation in a short-lived context; but other
places may accumulate a few (for example, in CreateDatabaseUsingFileCopy,
one per tablespace).  In most databases this is highly unlikely to be
very serious either, but it seems better to make the code consistent in
case there's future copy-and-paste.

The only case of actual concern seems to be the aforementioned routine,
which is new with commit 9c08aea6a309, so there's no need to backpatch.

As pointed out by Coverity.

src/backend/commands/dbcommands.c
src/backend/utils/init/postinit.c

index 1bbecfeddf6d904c12f3be1ec35fad043aa04dcd..9d0f83cde318eddfffa97759b2bf116c734286ea 100644 (file)
@@ -217,6 +217,8 @@ CreateDatabaseUsingWalLog(Oid src_dboid, Oid dst_dboid,
        UnlockRelationId(&dstrelid, AccessShareLock);
    }
 
+   pfree(srcpath);
+   pfree(dstpath);
    list_free_deep(rnodelist);
 }
 
@@ -628,6 +630,8 @@ CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dst_dboid, Oid src_tsid,
            (void) XLogInsert(RM_DBASE_ID,
                              XLOG_DBASE_CREATE_FILE_COPY | XLR_SPECIAL_REL_UPDATE);
        }
+       pfree(srcpath);
+       pfree(dstpath);
    }
    table_endscan(scan);
    table_close(rel, AccessShareLock);
@@ -2128,6 +2132,9 @@ movedb(const char *dbname, const char *tblspcname)
    /* Now it's safe to release the database lock */
    UnlockSharedObjectForSession(DatabaseRelationId, db_id, 0,
                                 AccessExclusiveLock);
+
+   pfree(src_dbpath);
+   pfree(dst_dbpath);
 }
 
 /* Error cleanup callback for movedb */
@@ -2141,6 +2148,8 @@ movedb_failure_callback(int code, Datum arg)
    dstpath = GetDatabasePath(fparms->dest_dboid, fparms->dest_tsoid);
 
    (void) rmtree(dstpath, true);
+
+   pfree(dstpath);
 }
 
 /*
@@ -3051,6 +3060,9 @@ dbase_redo(XLogReaderState *record)
         * We don't need to copy subdirectories
         */
        copydir(src_path, dst_path, false);
+
+       pfree(src_path);
+       pfree(dst_path);
    }
    else if (info == XLOG_DBASE_CREATE_WAL_LOG)
    {
@@ -3063,6 +3075,7 @@ dbase_redo(XLogReaderState *record)
        /* Create the database directory with the version file. */
        CreateDirAndVersionFile(dbpath, xlrec->db_id, xlrec->tablespace_id,
                                true);
+       pfree(dbpath);
    }
    else if (info == XLOG_DBASE_DROP)
    {
index 5dbc7379e36864ea06f4383edf29fd2ffd8599a5..38e5b54a15a25ad9132b6ab3fc4811776e7be7a4 100644 (file)
@@ -1057,6 +1057,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
    }
 
    SetDatabasePath(fullpath);
+   pfree(fullpath);
 
    /*
     * It's now possible to do real access to the system catalogs.