Skip to content

Commit fcb9535

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 df9ea6a commit fcb9535

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

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-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.144 2007/03/26 16:58:39 tgl Exp $
18+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.145 2007/08/06 01:38:14 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1472,15 +1472,17 @@ ReadStr(ArchiveHandle *AH)
14721472
int l;
14731473

14741474
l = ReadInt(AH);
1475-
if (l == -1)
1475+
if (l < 0)
14761476
buf = NULL;
14771477
else
14781478
{
14791479
buf = (char *) malloc(l + 1);
14801480
if (!buf)
14811481
die_horribly(AH, modulename, "out of memory\n");
14821482

1483-
(*AH->ReadBufPtr) (AH, (void *) buf, l);
1483+
if ((*AH->ReadBufPtr) (AH, (void *) buf, l) != l)
1484+
die_horribly(AH, modulename, "unexpected end of file\n");
1485+
14841486
buf[l] = '\0';
14851487
}
14861488

@@ -2675,8 +2677,8 @@ ReadHead(ArchiveHandle *AH)
26752677
/* If we haven't already read the header... */
26762678
if (!AH->readHeader)
26772679
{
2678-
2679-
(*AH->ReadBufPtr) (AH, tmpMag, 5);
2680+
if ((*AH->ReadBufPtr) (AH, tmpMag, 5) != 5)
2681+
die_horribly(AH, modulename, "unexpected end of file\n");
26802682

26812683
if (strncmp(tmpMag, "PGDMP", 5) != 0)
26822684
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-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.38 2007/03/18 16:50:44 neilc Exp $
22+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.39 2007/08/06 01:38:14 tgl Exp $
2323
*
2424
*-------------------------------------------------------------------------
2525
*/
@@ -712,17 +712,18 @@ _WriteByte(ArchiveHandle *AH, const int i)
712712
*
713713
* Called by the archiver to read bytes & integers from the archive.
714714
* These routines are only used to read & write headers & TOC.
715-
*
715+
* EOF should be treated as a fatal error.
716716
*/
717717
static int
718718
_ReadByte(ArchiveHandle *AH)
719719
{
720720
lclContext *ctx = (lclContext *) AH->formatData;
721721
int res;
722722

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

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-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.32 2007/03/18 16:50:44 neilc Exp $
23+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.33 2007/08/06 01:38:15 tgl Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -395,9 +395,10 @@ _ReadByte(ArchiveHandle *AH)
395395
lclContext *ctx = (lclContext *) AH->formatData;
396396
int res;
397397

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

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-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.58 2007/03/18 16:50:44 neilc Exp $
19+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.59 2007/08/06 01:38:15 tgl Exp $
2020
*
2121
*-------------------------------------------------------------------------
2222
*/
@@ -510,7 +510,7 @@ _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
510510
used = avail;
511511

512512
/* Copy, and adjust buffer pos */
513-
memcpy(buf, AH->lookahead, used);
513+
memcpy(buf, AH->lookahead + AH->lookaheadPos, used);
514514
AH->lookaheadPos += used;
515515

516516
/* Adjust required length */
@@ -766,12 +766,13 @@ static int
766766
_ReadByte(ArchiveHandle *AH)
767767
{
768768
lclContext *ctx = (lclContext *) AH->formatData;
769-
int res;
770-
char c = '\0';
769+
size_t res;
770+
unsigned char c;
771771

772772
res = tarRead(&c, 1, ctx->FH);
773-
if (res != EOF)
774-
ctx->filePos += res;
773+
if (res != 1)
774+
die_horribly(AH, modulename, "unexpected end of file\n");
775+
ctx->filePos += 1;
775776
return c;
776777
}
777778

0 commit comments

Comments
 (0)