Skip to content

Commit c4ef3b8

Browse files
committed
Fix a couple of memory leaks in src/bin/pg_basebackup/
These have been introduced by 7fbe0c8, and could happen for pg_basebackup and pg_receivewal. Per report from Coverity for the ones in walmethods.c, I have spotted the ones in receivelog.c after more review. Backpatch-through: 10
1 parent 1bcfda3 commit c4ef3b8

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/bin/pg_basebackup/receivelog.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
121121
{
122122
pg_log_error("could not get size of write-ahead log file \"%s\": %s",
123123
fn, stream->walmethod->getlasterror());
124+
pg_free(fn);
124125
return false;
125126
}
126127
if (size == WalSegSz)
@@ -131,6 +132,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
131132
{
132133
pg_log_error("could not open existing write-ahead log file \"%s\": %s",
133134
fn, stream->walmethod->getlasterror());
135+
pg_free(fn);
134136
return false;
135137
}
136138

@@ -139,11 +141,13 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
139141
{
140142
pg_log_error("could not fsync existing write-ahead log file \"%s\": %s",
141143
fn, stream->walmethod->getlasterror());
144+
pg_free(fn);
142145
stream->walmethod->close(f, CLOSE_UNLINK);
143146
return false;
144147
}
145148

146149
walfile = f;
150+
pg_free(fn);
147151
return true;
148152
}
149153
if (size != 0)
@@ -155,6 +159,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
155159
"write-ahead log file \"%s\" has %d bytes, should be 0 or %d",
156160
size),
157161
fn, (int) size, WalSegSz);
162+
pg_free(fn);
158163
return false;
159164
}
160165
/* File existed and was empty, so fall through and open */
@@ -168,9 +173,11 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
168173
{
169174
pg_log_error("could not open write-ahead log file \"%s\": %s",
170175
fn, stream->walmethod->getlasterror());
176+
pg_free(fn);
171177
return false;
172178
}
173179

180+
pg_free(fn);
174181
walfile = f;
175182
return true;
176183
}

src/bin/pg_basebackup/walmethods.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ dir_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
9595
filename = dir_get_file_name(pathname, temp_suffix);
9696
snprintf(tmppath, sizeof(tmppath), "%s/%s",
9797
dir_data->basedir, filename);
98+
pg_free(filename);
9899

99100
/*
100101
* Open a file for non-compressed as well as compressed files. Tracking
@@ -255,11 +256,13 @@ dir_close(Walfile f, WalCloseMethod method)
255256
filename = dir_get_file_name(df->pathname, df->temp_suffix);
256257
snprintf(tmppath, sizeof(tmppath), "%s/%s",
257258
dir_data->basedir, filename);
259+
pg_free(filename);
258260

259261
/* permanent name, so no need for the prefix */
260262
filename2 = dir_get_file_name(df->pathname, NULL);
261263
snprintf(tmppath2, sizeof(tmppath2), "%s/%s",
262264
dir_data->basedir, filename2);
265+
pg_free(filename2);
263266
r = durable_rename(tmppath, tmppath2);
264267
}
265268
else if (method == CLOSE_UNLINK)
@@ -270,6 +273,7 @@ dir_close(Walfile f, WalCloseMethod method)
270273
filename = dir_get_file_name(df->pathname, df->temp_suffix);
271274
snprintf(tmppath, sizeof(tmppath), "%s/%s",
272275
dir_data->basedir, filename);
276+
pg_free(filename);
273277
r = unlink(tmppath);
274278
}
275279
else
@@ -626,11 +630,14 @@ tar_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
626630
if (tarCreateHeader(tar_data->currentfile->header, tmppath, NULL, 0, S_IRUSR | S_IWUSR, 0, 0, time(NULL)) != TAR_OK)
627631
{
628632
pg_free(tar_data->currentfile);
633+
pg_free(tmppath);
629634
tar_data->currentfile = NULL;
630635
tar_set_error("could not create tar header");
631636
return NULL;
632637
}
633638

639+
pg_free(tmppath);
640+
634641
#ifdef HAVE_LIBZ
635642
if (tar_data->compression)
636643
{

src/bin/pg_basebackup/walmethods.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ struct WalWriteMethod
5353
ssize_t (*get_file_size) (const char *pathname);
5454

5555
/*
56-
* Return the name of the current file to work on, without the base
57-
* directory. This is useful for logging.
56+
* Return the name of the current file to work on in pg_malloc()'d string,
57+
* without the base directory. This is useful for logging.
5858
*/
5959
char *(*get_file_name) (const char *pathname, const char *temp_suffix);
6060

0 commit comments

Comments
 (0)