]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix a couple of memory leaks in src/bin/pg_basebackup/
authorMichael Paquier <michael@paquier.xyz>
Mon, 26 Jul 2021 02:14:17 +0000 (11:14 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 26 Jul 2021 02:14:17 +0000 (11:14 +0900)
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

src/bin/pg_basebackup/receivelog.c
src/bin/pg_basebackup/walmethods.c
src/bin/pg_basebackup/walmethods.h

index a61c4a8d8c48aad8fc110b480fd3c228e2c3e34a..bb07d4a79555779a0f3d525559888f2e84a5dab3 100644 (file)
@@ -121,6 +121,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
                        fprintf(stderr,
                                        _("%s: could not get size of write-ahead log file \"%s\": %s\n"),
                                        progname, fn, stream->walmethod->getlasterror());
+                       pg_free(fn);
                        return false;
                }
                if (size == WalSegSz)
@@ -132,6 +133,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
                                fprintf(stderr,
                                                _("%s: could not open existing write-ahead log file \"%s\": %s\n"),
                                                progname, fn, stream->walmethod->getlasterror());
+                               pg_free(fn);
                                return false;
                        }
 
@@ -141,11 +143,13 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
                                fprintf(stderr,
                                                _("%s: could not fsync existing write-ahead log file \"%s\": %s\n"),
                                                progname, fn, stream->walmethod->getlasterror());
+                               pg_free(fn);
                                stream->walmethod->close(f, CLOSE_UNLINK);
                                return false;
                        }
 
                        walfile = f;
+                       pg_free(fn);
                        return true;
                }
                if (size != 0)
@@ -158,6 +162,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
                                                         "%s: write-ahead log file \"%s\" has %d bytes, should be 0 or %d\n",
                                                         size),
                                        progname, fn, (int) size, WalSegSz);
+                       pg_free(fn);
                        return false;
                }
                /* File existed and was empty, so fall through and open */
@@ -172,9 +177,11 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
                fprintf(stderr,
                                _("%s: could not open write-ahead log file \"%s\": %s\n"),
                                progname, fn, stream->walmethod->getlasterror());
+               pg_free(fn);
                return false;
        }
 
+       pg_free(fn);
        walfile = f;
        return true;
 }
index e6b845065f5a212047a9d9cbcc8b2da3059f451f..1dc8a5a72a69d0e9a3274124ddee2cf592909465 100644 (file)
@@ -95,6 +95,7 @@ dir_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
        filename = dir_get_file_name(pathname, temp_suffix);
        snprintf(tmppath, sizeof(tmppath), "%s/%s",
                         dir_data->basedir, filename);
+       pg_free(filename);
 
        /*
         * Open a file for non-compressed as well as compressed files. Tracking
@@ -255,11 +256,13 @@ dir_close(Walfile f, WalCloseMethod method)
                        filename = dir_get_file_name(df->pathname, df->temp_suffix);
                        snprintf(tmppath, sizeof(tmppath), "%s/%s",
                                         dir_data->basedir, filename);
+                       pg_free(filename);
 
                        /* permanent name, so no need for the prefix */
                        filename2 = dir_get_file_name(df->pathname, NULL);
                        snprintf(tmppath2, sizeof(tmppath2), "%s/%s",
                                         dir_data->basedir, filename2);
+                       pg_free(filename2);
                        r = durable_rename(tmppath, tmppath2, progname);
                }
                else if (method == CLOSE_UNLINK)
@@ -270,6 +273,7 @@ dir_close(Walfile f, WalCloseMethod method)
                        filename = dir_get_file_name(df->pathname, df->temp_suffix);
                        snprintf(tmppath, sizeof(tmppath), "%s/%s",
                                         dir_data->basedir, filename);
+                       pg_free(filename);
                        r = unlink(tmppath);
                }
                else
@@ -626,11 +630,14 @@ tar_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
        if (tarCreateHeader(tar_data->currentfile->header, tmppath, NULL, 0, S_IRUSR | S_IWUSR, 0, 0, time(NULL)) != TAR_OK)
        {
                pg_free(tar_data->currentfile);
+               pg_free(tmppath);
                tar_data->currentfile = NULL;
                tar_set_error("could not create tar header");
                return NULL;
        }
 
+       pg_free(tmppath);
+
 #ifdef HAVE_LIBZ
        if (tar_data->compression)
        {
index 92be83963457bc52a8a88c6a9f3a6f4296d2ecac..2873e2f7bb543e8908b12e30754beb5605bac3ab 100644 (file)
@@ -53,8 +53,8 @@ struct WalWriteMethod
        ssize_t         (*get_file_size) (const char *pathname);
 
        /*
-        * Return the name of the current file to work on, without the base
-        * directory.  This is useful for logging.
+        * Return the name of the current file to work on in pg_malloc()'d string,
+        * without the base directory.  This is useful for logging.
         */
        char       *(*get_file_name) (const char *pathname, const char *temp_suffix);