]> granicus.if.org Git - postgresql/commitdiff
Fix pg_restore to guard against unexpected EOF while reading an archive file.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Aug 2007 01:38:49 +0000 (01:38 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Aug 2007 01:38:49 +0000 (01:38 +0000)
Per report and partial patch from Chad Wagner.

src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_custom.c
src/bin/pg_dump/pg_backup_files.c
src/bin/pg_dump/pg_backup_tar.c

index 64a837bbb66f510a43afc2748e5d1e0729c5a6e9..a42fff3031935cb5d80f5a817a81f27027dfb558 100644 (file)
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.79.2.6 2005/08/25 00:11:36 tgl Exp $
+ *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.79.2.7 2007/08/06 01:38:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1526,7 +1526,7 @@ ReadStr(ArchiveHandle *AH)
        int                     l;
 
        l = ReadInt(AH);
-       if (l == -1)
+       if (l < 0)
                buf = NULL;
        else
        {
@@ -1534,7 +1534,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';
        }
 
@@ -2323,8 +2325,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");
index 8f207a0331f1c68941f5ee037144b70fd97813dc..8fd627163346953ef7234d7112c6fdc867e3424b 100644 (file)
@@ -19,7 +19,7 @@
  *
  *
  * IDENTIFICATION
- *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.26 2003/10/08 03:52:32 momjian Exp $
+ *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.26.2.1 2007/08/06 01:38:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -713,7 +713,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)
@@ -721,9 +721,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;
 }
 
index b686bc34043dabff02fb038739489bad042b90b8..7505043033374b0becfe9e607ee219b1de0d112f 100644 (file)
@@ -20,7 +20,7 @@
  *
  *
  * IDENTIFICATION
- *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.22 2003/10/08 03:52:32 momjian Exp $
+ *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.22.2.1 2007/08/06 01:38:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -396,9 +396,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;
 }
 
index 0124481d9c709d4553a81dc9de5996889b87c2f7..75fef857f764114e94eda2762eefa3c1a16c0222 100644 (file)
@@ -16,7 +16,7 @@
  *
  *
  * IDENTIFICATION
- *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.38.2.1 2005/06/22 02:12:19 neilc Exp $
+ *             $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.38.2.2 2007/08/06 01:38:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -485,7 +485,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 */
@@ -729,12 +729,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;
 }