Skip to content

Commit bbda88c

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 df9e65b commit bbda88c

File tree

6 files changed

+24
-7
lines changed

6 files changed

+24
-7
lines changed

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,10 +1262,11 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
12621262
#ifdef HAVE_LIBZ
12631263
if (state.ztarfile != NULL)
12641264
{
1265+
errno = 0; /* in case gzclose() doesn't set it */
12651266
if (gzclose(state.ztarfile) != 0)
12661267
{
1267-
pg_log_error("could not close compressed file \"%s\": %s",
1268-
state.filename, get_gz_error(state.ztarfile));
1268+
pg_log_error("could not close compressed file \"%s\": %m",
1269+
state.filename);
12691270
exit(1);
12701271
}
12711272
}

src/bin/pg_basebackup/receivelog.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ mark_file_as_archived(StreamCtl *stream, const char *fname)
7171
return false;
7272
}
7373

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

7681
return true;
7782
}

src/bin/pg_basebackup/walmethods.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ dir_close(Walfile f, WalCloseMethod method)
235235

236236
#ifdef HAVE_LIBZ
237237
if (dir_data->compression > 0)
238+
{
239+
errno = 0; /* in case gzclose() doesn't set it */
238240
r = gzclose(df->gzfp);
241+
}
239242
else
240243
#endif
241244
r = close(df->fd);

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 2 additions & 0 deletions
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)
@@ -1578,6 +1579,7 @@ RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
15781579
{
15791580
int res;
15801581

1582+
errno = 0; /* in case gzclose() doesn't set it */
15811583
if (AH->gzOut)
15821584
res = GZCLOSE(AH->OF);
15831585
else

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 6 additions & 3 deletions
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

Lines changed: 4 additions & 1 deletion
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)