]> 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:32 +0000 (01:38 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 6 Aug 2007 01:38:32 +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 1abd3b19912d871bebe9aae533cead367cc7b869..49f5087e6a6a875a4e14b6f22ef1a354344b2c5d 100644 (file)
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.117.2.4 2006/05/24 21:20:24 tgl Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.117.2.5 2007/08/06 01:38:32 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1398,7 +1398,7 @@ ReadStr(ArchiveHandle *AH)
        int                     l;
 
        l = ReadInt(AH);
-       if (l == -1)
+       if (l < 0)
                buf = NULL;
        else
        {
@@ -1406,7 +1406,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';
        }
 
@@ -2547,8 +2549,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 3edbb8f5c0a6c5bb4eb790be02168c12c9903546..bfa4eaf02f40d034c8916b17c43a70e8fb5be28b 100644 (file)
@@ -19,7 +19,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.33 2005/10/15 02:49:38 momjian Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.33.2.1 2007/08/06 01:38:32 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -703,7 +703,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)
@@ -711,9 +711,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 b36a1c8600f2602f27f55e736932255c2161d04f..51fd64fa1919eb6fb3a9e815409356d476cd209d 100644 (file)
@@ -20,7 +20,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.27 2005/10/15 02:49:38 momjian Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.27.2.1 2007/08/06 01:38:32 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -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;
 }
 
index b50919f3f5ab4d7ae4e2b78c61e6c1863cfd3293..506c9a639be9387d23540d5af3f37fd96202dc52 100644 (file)
@@ -16,7 +16,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.49.2.3 2006/11/01 15:59:31 tgl Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.49.2.4 2007/08/06 01:38:32 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -509,7 +509,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 */
@@ -766,12 +766,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;
 }