Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3cac2c8

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'sright: 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 anygzclose() call where we care about the error status. (There aresome calls where we don't, because we already failed at some previousstep.) This ensures that we don't get a misleadingly irrelevanterror 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 casesare basically can't-happen if we're not misusing zlib, so it'snot worth the extra notational cruft that would be required.Also, fix several places that simply failed to check for close-timeerrors 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 betweenthe frontend logging API changes that happened in v12 and the factthat frontend code can't rely on %m before that, the patch would needsubstantial revision to work in older branches. It doesn't quiteseem 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 parenta8d8445 commit3cac2c8

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

‎src/bin/pg_basebackup/bbstreamer_file.c

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

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

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

356356
#ifdefHAVE_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
#ifdefHAVE_LIBLZ4

‎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
elseif (AH->OF!=stdout)
@@ -1567,6 +1568,7 @@ RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
15671568
{
15681569
intres;
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

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
intlen;
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)

[8]ページ先頭

©2009-2025 Movatter.jp