Skip to content

Commit 3cac2c8

Browse files
committed
Handle close() failures more robustly in pg_dump and pg_basebackup.
Coverity complained that applying get_gz_error after a failed gzclose, as we did in one place in pg_basebackup, is unsafe. I think it's right: it's entirely likely that the call is touching freed memory. Change that to inspect errno, as we do for other gzclose calls. Also, be careful to initialize errno to zero immediately before any gzclose() call where we care about the error status. (There are some calls where we don't, because we already failed at some previous step.) This ensures that we don't get a misleadingly irrelevant error code if gzclose() fails in a way that doesn't set errno. We could work harder at that, but it looks to me like all such cases are basically can't-happen if we're not misusing zlib, so it's not worth the extra notational cruft that would be required. Also, fix several places that simply failed to check for close-time errors at all, mostly at some remove from the close or gzclose itself; and one place that did check but didn't bother to report the errno. Back-patch to v12. These mistakes are older than that, but between the frontend logging API changes that happened in v12 and the fact that frontend code can't rely on %m before that, the patch would need substantial revision to work in older branches. It doesn't quite seem worth the trouble given the lack of related field complaints. Patch by me; thanks to Michael Paquier for review. Discussion: https://postgr.es/m/1343113.1636489231@sss.pgh.pa.us
1 parent a8d8445 commit 3cac2c8

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

src/bin/pg_basebackup/bbstreamer_file.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ bbstreamer_gzip_writer_finalize(bbstreamer *streamer)
303303

304304
mystreamer = (bbstreamer_gzip_writer *) streamer;
305305

306+
errno = 0; /* in case gzclose() doesn't set it */
306307
if (gzclose(mystreamer->gzfile) != 0)
307308
{
308-
pg_log_error("could not close compressed file \"%s\": %s",
309-
mystreamer->pathname,
310-
get_gz_error(mystreamer->gzfile));
309+
pg_log_error("could not close compressed file \"%s\": %m",
310+
mystreamer->pathname);
311311
exit(1);
312312
}
313313

src/bin/pg_basebackup/receivelog.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ mark_file_as_archived(StreamCtl *stream, const char *fname)
7070
return false;
7171
}
7272

73-
stream->walmethod->close(f, CLOSE_NORMAL);
73+
if (stream->walmethod->close(f, CLOSE_NORMAL) != 0)
74+
{
75+
pg_log_error("could not close archive status file \"%s\": %s",
76+
tmppath, stream->walmethod->getlasterror());
77+
return false;
78+
}
7479

7580
return true;
7681
}

src/bin/pg_basebackup/walmethods.c

+3
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,10 @@ dir_close(Walfile f, WalCloseMethod method)
355355

356356
#ifdef HAVE_LIBZ
357357
if (dir_data->compression_method == COMPRESSION_GZIP)
358+
{
359+
errno = 0; /* in case gzclose() doesn't set it */
358360
r = gzclose(df->gzfp);
361+
}
359362
else
360363
#endif
361364
#ifdef HAVE_LIBLZ4

src/bin/pg_dump/pg_backup_archiver.c

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ CloseArchive(Archive *AHX)
268268
AH->ClosePtr(AH);
269269

270270
/* Close the output */
271+
errno = 0; /* in case gzclose() doesn't set it */
271272
if (AH->gzOut)
272273
res = GZCLOSE(AH->OF);
273274
else if (AH->OF != stdout)
@@ -1567,6 +1568,7 @@ RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
15671568
{
15681569
int res;
15691570

1571+
errno = 0; /* in case gzclose() doesn't set it */
15701572
if (AH->gzOut)
15711573
res = GZCLOSE(AH->OF);
15721574
else

src/bin/pg_dump/pg_backup_directory.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ _EndData(ArchiveHandle *AH, TocEntry *te)
369369
lclContext *ctx = (lclContext *) AH->formatData;
370370

371371
/* Close the file */
372-
cfclose(ctx->dataFH);
372+
if (cfclose(ctx->dataFH) != 0)
373+
fatal("could not close data file: %m");
373374

374375
ctx->dataFH = NULL;
375376
}
@@ -680,7 +681,8 @@ _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
680681
int len;
681682

682683
/* Close the BLOB data file itself */
683-
cfclose(ctx->dataFH);
684+
if (cfclose(ctx->dataFH) != 0)
685+
fatal("could not close blob data file: %m");
684686
ctx->dataFH = NULL;
685687

686688
/* register the blob in blobs.toc */
@@ -699,7 +701,8 @@ _EndBlobs(ArchiveHandle *AH, TocEntry *te)
699701
{
700702
lclContext *ctx = (lclContext *) AH->formatData;
701703

702-
cfclose(ctx->blobsTocFH);
704+
if (cfclose(ctx->blobsTocFH) != 0)
705+
fatal("could not close blobs TOC file: %m");
703706
ctx->blobsTocFH = NULL;
704707
}
705708

src/bin/pg_dump/pg_backup_tar.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,11 @@ tarClose(ArchiveHandle *AH, TAR_MEMBER *th)
438438
* Close the GZ file since we dup'd. This will flush the buffers.
439439
*/
440440
if (AH->compression != 0)
441+
{
442+
errno = 0; /* in case gzclose() doesn't set it */
441443
if (GZCLOSE(th->zFH) != 0)
442-
fatal("could not close tar member");
444+
fatal("could not close tar member: %m");
445+
}
443446

444447
if (th->mode == 'w')
445448
_tarAddFile(AH, th); /* This will close the temp file */

0 commit comments

Comments
 (0)