Skip to content

Commit 62cb742

Browse files
committed
Avoid dangling-pointer usage in pg_basebackup progress reports.
Ill-considered refactoring in 23a1c65 led to progress_filename sometimes pointing to data that had gone out of scope. The most bulletproof fix is to hang onto a copy of whatever's passed in. Compared to the work spent elsewhere per file, that's not very expensive, plus we can skip it except in verbose logging mode. Per buildfarm. Discussion: https://postgr.es/m/20220212211316.GK31460@telsasoft.com
1 parent 138c51b commit 62cb742

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static bool found_tablespace_dirs = false;
164164
static uint64 totalsize_kb;
165165
static uint64 totaldone;
166166
static int tablespacecount;
167-
static const char *progress_filename;
167+
static char *progress_filename = NULL;
168168

169169
/* Pipe to communicate with background wal receiver process */
170170
#ifndef WIN32
@@ -775,11 +775,22 @@ verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found)
775775

776776
/*
777777
* Callback to update our notion of the current filename.
778+
*
779+
* No other code should modify progress_filename!
778780
*/
779781
static void
780782
progress_update_filename(const char *filename)
781783
{
782-
progress_filename = filename;
784+
/* We needn't maintain this variable if not doing verbose reports. */
785+
if (showprogress && verbose)
786+
{
787+
if (progress_filename)
788+
free(progress_filename);
789+
if (filename)
790+
progress_filename = pg_strdup(filename);
791+
else
792+
progress_filename = NULL;
793+
}
783794
}
784795

785796
/*
@@ -1258,7 +1269,7 @@ CreateBackupStreamer(char *archive_name, char *spclocation,
12581269
*/
12591270
if (must_parse_archive)
12601271
streamer = bbstreamer_tar_archiver_new(streamer);
1261-
progress_filename = archive_filename;
1272+
progress_update_filename(archive_filename);
12621273
}
12631274

12641275
/*
@@ -1662,7 +1673,7 @@ ReceiveTarFile(PGconn *conn, char *archive_name, char *spclocation,
16621673
expect_unterminated_tarfile);
16631674
state.tablespacenum = tablespacenum;
16641675
ReceiveCopyData(conn, ReceiveTarCopyChunk, &state);
1665-
progress_filename = NULL;
1676+
progress_update_filename(NULL);
16661677

16671678
/*
16681679
* The decision as to whether we need to inject the backup manifest into
@@ -2161,7 +2172,7 @@ BaseBackup(void)
21612172

21622173
if (showprogress)
21632174
{
2164-
progress_filename = NULL;
2175+
progress_update_filename(NULL);
21652176
progress_report(PQntuples(res), true, true);
21662177
}
21672178

0 commit comments

Comments
 (0)