8000 Fix pg_restore to guard against unexpected EOF while reading an archi… · danielcode/postgres@99fa5f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99fa5f4

Browse files
committed
Fix pg_restore to guard against unexpected EOF while reading an archive file.
Per report and partial patch from Chad Wagner.
1 parent 681690f commit 99fa5f4

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed< 8000 /span>

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.62.2.7 2005/05/17 17:31:15 tgl Exp $
18+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.62.2.8 2007/08/06 01:38:57 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1545,15 +1545,17 @@ ReadStr(ArchiveHandle *AH)
15451545
int l;
15461546

15471547
l = ReadInt(AH);
1548-
if (l == -1)
1548+
if (l < 0)
15491549
buf = NULL;
15501550
else
15511551
{
15521552
buf = (char *) malloc(l + 1);
15531553
if (!buf)
15541554
die_horribly(AH, modulename, "out of memory\n");
15551555

1556-
(*AH->ReadBufPtr) (AH, (void *) buf, l);
1556+
if ((*AH->ReadBufPtr) (AH, (void *) buf, l) != l)
1557+
die_horribly(AH, modulename, "unexpected end of file\n");
1558+
15571559
buf[l] = '\0';
15581560
}
15591561

@@ -2277,8 +2279,8 @@ ReadHead(ArchiveHandle *AH)
22772279
/* If we haven't already read the header... */
22782280
if (!AH->readHeader)
22792281
{
2280-
2281-
(*AH->ReadBufPtr) (AH, tmpMag, 5);
2282+
if ((*AH->ReadBufPtr) (AH, tmpMag, 5) != 5)
2283+
die_horribly(AH, modulename, "unexpected end of file\n");
22822284

22832285
if (strncmp(tmpMag, "PGDMP", 5) != 0)
22842286
die_horribly(AH, modulename, "did not find magic string in file header\n");

src/bin/pg_dump/pg_backup_custom.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*
2121
* IDENTIFICATION
22-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.23 2002/10/25 01:33:17 momjian Exp $
22+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.23.2.1 2007/08/06 01:38:57 tgl Exp $
2323
*
2424
*-------------------------------------------------------------------------
2525
*/
@@ -713,17 +713,18 @@ _WriteByte(ArchiveHandle *AH, const int i)
713713
*
714714
* Called by the archiver to read bytes & integers from the archive.
715715
* These routines are only used to read & write headers & TOC.
716-
*
716+
* EOF should be treated as a fatal error.
717717
*/
718718
static int
719719
_ReadByte(ArchiveHandle *AH)
720720
{
721721
lclContext *ctx = (lclContext *) AH->formatData;
722722
int res;
723723

724-
res = fgetc(AH->FH);
725-
if (res != EOF)
726-
ctx->filePos += 1;
724+
res = getc(AH->FH);
725+
if (res == EOF)
726+
die_horribly(AH, modulename, "unexpected end of file\n");
727+
ctx->filePos += 1;
727728
return res;
728729
}
729730

src/bin/pg_dump/pg_backup_files.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
*
2222
* IDENTIFICATION
23-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.21 2002/10/25 01:33:17 momjian Exp $
23+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.21.2.1 2007/08/06 01:38:57 tgl Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -396,9 +396,10 @@ _ReadByte(ArchiveHandle *AH)
396396
lclContext *ctx = (lclContext *) AH->formatData;
397397
int res;
398398

399-
res = fgetc(AH->FH);
400-
if (res != EOF)
401-
ctx->filePos += 1;
399+
res = getc(AH->FH);
400+
if (res == EOF)
401+
die_horribly(AH, modulename, "unexpected end of file\n");
402+
ctx->filePos += 1;
402403
return res;
403404
}
404405

src/bin/pg_dump/pg_backup_tar.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*
1818
* IDENTIFICATION
19-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.32.2.2 2003/02/01 19:29:26 tgl Exp $
19+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.32.2.3 2007/08/06 01:38:57 tgl Exp $
2020
*
2121
*-------------------------------------------------------------------------
2222
*/
@@ -484,7 +484,7 @@ _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
484484
used = avail;
485485

486486
/* Copy, and adjust buffer pos */
487-
memcpy(buf, AH->lookahead, used);
487+
memcpy(buf, AH->lookahead + AH->lookaheadPos, used);
488488
AH->lookaheadPos += used;
489489

490490
/* Adjust required length */
@@ -728,12 +728,13 @@ static int
728728
_ReadByte(ArchiveHandle *AH)
729729
{
730730
lclContext *ctx = (lclContext *) AH->formatData;
731-
int res;
732-
char c = '\0';
731+
size_t res;
732+
unsigned char c;
733733

734734
res = tarRead(&c, 1, ctx->FH);
735-
if (res != EOF)
736-
ctx->filePos += res;
735+
if (res != 1)
736+
die_horribly(AH, modulename, "unexpected end of file\n");
737+
ctx->filePos += 1;
737738
return c;
738739
}
739740

0 commit comments

Comments
 (0)
0