From 0bd56172b2871e94c0d7115ffbf430308317ac49 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 25 Apr 2022 10:32:13 +0200 Subject: [PATCH] Always pfree strings returned by GetDatabasePath 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 | 13 +++++++++++++ src/backend/utils/init/postinit.c | 1 + 2 files changed, 14 insertions(+) diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 1bbecfeddf6..9d0f83cde31 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -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) { diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 5dbc7379e36..38e5b54a15a 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -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. -- 2.39.5