Skip to content

Commit 9c6fa34

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 9c83398 commit 9c6fa34

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
fprintf(stderr,
122122
_("%s: could not get size of write-ahead log file \"%s\": %s\n"),
123123
progname, fn, stream->walmethod->getlasterror());
124+
pg_free(fn);
124125
return false;
125126
}
126127
if (size == WalSegSz)
@@ -132,6 +133,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
132133
fprintf(stderr,
133134
_("%s: could not open existing write-ahead log file \"%s\": %s\n"),
134135
progname, fn, stream->walmethod->getlasterror());
136+
pg_free(fn);
135137
return false;
136138
}
137139

@@ -141,11 +143,13 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
141143
fprintf(stderr,
142144
_("%s: could not fsync existing write-ahead log file \"%s\": %s\n"),
143145
progname, fn, stream->walmethod->getlasterror());
146+
pg_free(fn);
144147
stream->walmethod->close(f, CLOSE_UNLINK);
145148
return false;
146149
}
147150

148151
walfile = f;
152+
pg_free(fn);
149153
return true;
150154
}
151155
if (size != 0)
@@ -158,6 +162,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
158162
"%s: write-ahead log file \"%s\" has %d bytes, should be 0 or %d\n",
159163
size),
160164
progname, fn, (int) size, WalSegSz);
165+
pg_free(fn);
161166
return false;
162167
}
163168
/* File existed and was empty, so fall through and open */
@@ -172,9 +177,11 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
172177
fprintf(stderr,
173178
_("%s: could not open write-ahead log file \"%s\": %s\n"),
174179
progname, fn, stream->walmethod->getlasterror());
180+
pg_free(fn);
175181
return false;
176182
}
177183

184+
pg_free(fn);
178185
walfile = f;
179186
return true;
180187
}

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, progname);
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)