summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2007-08-06 01:38:40 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2007-08-06 01:38:40 +0000
commitfbf9179a2758f721ed460f6503e36c56b82eef4c (patch)
treedfdf19fa9ca975e50d57d8e8288861b897338685 /src
parent2e24f4af49ddf6ed4d05446c132b1e33415a26b2 (diff)
Fix pg_restore to guard against unexpected EOF while reading an archive file.
Per report and partial patch from Chad Wagner.
Diffstat (limited to 'src')
-rw-r--r--src/bin/pg_dump/pg_backup_archiver.c12
-rw-r--r--src/bin/pg_dump/pg_backup_custom.c11
-rw-r--r--src/bin/pg_dump/pg_backup_files.c9
-rw-r--r--src/bin/pg_dump/pg_backup_tar.c13
4 files changed, 25 insertions, 20 deletions
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index e9af7d5fd06..392f9f9db52 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.101.4.9 2006/02/05 20:59:06 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.101.4.10 2007/08/06 01:38:40 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -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 b8e8d2c9c46..904424651d6 100644
--- a/src/bin/pg_dump/pg_backup_custom.c
+++ b/src/bin/pg_dump/pg_backup_custom.c
@@ -19,7 +19,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.29.4.1 2005/01/25 22:44:47 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.29.4.2 2007/08/06 01:38:40 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -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 fc096211323..80da43eff28 100644
--- a/src/bin/pg_dump/pg_backup_files.c
+++ b/src/bin/pg_dump/pg_backup_files.c
@@ -20,7 +20,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.25 2004/03/03 21:28:54 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.25.4.1 2007/08/06 01:38:40 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;
}
diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c
index adb5eba895b..f20fddae6aa 100644
--- a/src/bin/pg_dump/pg_backup_tar.c
+++ b/src/bin/pg_dump/pg_backup_tar.c
@@ -16,7 +16,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.46.4.2 2005/06/22 02:02:09 neilc Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.46.4.3 2007/08/06 01:38:40 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -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;
}