Fix fuzzy error handling in pg_basebackup when opening gzFile
authorMichael Paquier <michael@paquier.xyz>
Tue, 4 Feb 2020 04:56:04 +0000 (13:56 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 4 Feb 2020 04:56:04 +0000 (13:56 +0900)
First, this code did not bother checking for a failure when calling
dup().  Then, per zlib, gzerror() returns NULL for a NULL input, which
can happen if passing down to gzdopen() an invalid file descriptor or if
there was an allocation failure.

No back-patch is done as this would unlikely be a problem in the field.

Per Coverity.

Reported-by: Tom Lane
src/bin/pg_basebackup/pg_basebackup.c

index 556a0af91607c3bb0cef5815e6bffc6d19d22e01..4e12cdb44672d0d1facf173cd9b80433727633cb 100644 (file)
@@ -1022,7 +1022,20 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
 #ifdef HAVE_LIBZ
            if (compresslevel != 0)
            {
-               state.ztarfile = gzdopen(dup(fileno(stdout)), "wb");
+               int     fd = dup(fileno(stdout));
+               if (fd < 0)
+               {
+                   pg_log_error("could not duplicate stdout: %m");
+                   exit(1);
+               }
+
+               state.ztarfile = gzdopen(fd, "wb");
+               if (state.ztarfile == NULL)
+               {
+                   pg_log_error("could not open output file: %m");
+                   exit(1);
+               }
+
                if (gzsetparams(state.ztarfile, compresslevel,
                                Z_DEFAULT_STRATEGY) != Z_OK)
                {