From: Tom Lane Date: Mon, 6 Aug 2007 01:38:40 +0000 (+0000) Subject: Fix pg_restore to guard against unexpected EOF while reading an archive file. X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=ff5609746ebb723ed16b30118c6adbc343ad1e3e;p=users%2Fbernd%2Fpostgres.git Fix pg_restore to guard against unexpected EOF while reading an archive file. Per report and partial patch from Chad Wagner. --- diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 91c7006b19..fad0e5ab3b 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -1513,7 +1513,7 @@ ReadStr(ArchiveHandle *AH) int l; l = ReadInt(AH); - if (l == -1) + if (l < 0) buf = NULL; else { @@ -1521,7 +1521,9 @@ ReadStr(ArchiveHandle *AH) if (!buf) die_horribly(AH, modulename, "out of memory\n"); - (*AH->ReadBufPtr) (AH, (void *) buf, l); + if ((*AH->ReadBufPtr) (AH, (void *) buf, l) != l) + die_horribly(AH, modulename, "unexpected end of file\n"); + buf[l] = '\0'; } @@ -2661,8 +2663,8 @@ ReadHead(ArchiveHandle *AH) /* If we haven't already read the header... */ if (!AH->readHeader) { - - (*AH->ReadBufPtr) (AH, tmpMag, 5); + if ((*AH->ReadBufPtr) (AH, tmpMag, 5) != 5) + die_horribly(AH, modulename, "unexpected end of file\n"); if (strncmp(tmpMag, "PGDMP", 5) != 0) die_horribly(AH, modulename, "did not find magic string in file header\n"); diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index 5a79d63eef..2806c97d27 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -442,7 +442,7 @@ _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt) while (id != te->dumpId) { - if ((TocIDRequired(AH, id, ropt) & 2) != 0) + if ((TocIDRequired(AH, id, ropt) & REQ_DATA) != 0) die_horribly(AH, modulename, "Dumping a specific TOC data block out of order is not supported" " without ID on this input stream (fseek required)\n"); @@ -712,7 +712,7 @@ _WriteByte(ArchiveHandle *AH, const int i) * * Called by the archiver to read bytes & integers from the archive. * These routines are only used to read & write headers & TOC. - * + * EOF should be treated as a fatal error. */ static int _ReadByte(ArchiveHandle *AH) @@ -720,9 +720,10 @@ _ReadByte(ArchiveHandle *AH) lclContext *ctx = (lclContext *) AH->formatData; int res; - res = fgetc(AH->FH); - if (res != EOF) - ctx->filePos += 1; + res = getc(AH->FH); + if (res == EOF) + die_horribly(AH, modulename, "unexpected end of file\n"); + ctx->filePos += 1; return res; } diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c index ad4bbdbd63..1945a22836 100644 --- a/src/bin/pg_dump/pg_backup_files.c +++ b/src/bin/pg_dump/pg_backup_files.c @@ -397,9 +397,10 @@ _ReadByte(ArchiveHandle *AH) lclContext *ctx = (lclContext *) AH->formatData; int res; - res = fgetc(AH->FH); - if (res != EOF) - ctx->filePos += 1; + res = getc(AH->FH); + if (res == EOF) + die_horribly(AH, modulename, "unexpected end of file\n"); + ctx->filePos += 1; return res; } diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c index 2743fd1774..3f63d5e796 100644 --- a/src/bin/pg_dump/pg_backup_tar.c +++ b/src/bin/pg_dump/pg_backup_tar.c @@ -483,7 +483,7 @@ _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh) used = avail; /* Copy, and adjust buffer pos */ - memcpy(buf, AH->lookahead, used); + memcpy(buf, AH->lookahead + AH->lookaheadPos, used); AH->lookaheadPos += used; /* Adjust required length */ @@ -727,12 +727,13 @@ static int _ReadByte(ArchiveHandle *AH) { lclContext *ctx = (lclContext *) AH->formatData; - int res; - char c = '\0'; + size_t res; + unsigned char c; res = tarRead(&c, 1, ctx->FH); - if (res != EOF) - ctx->filePos += res; + if (res != 1) + die_horribly(AH, modulename, "unexpected end of file\n"); + ctx->filePos += 1; return c; }