Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cfa1b4a

Browse files
committed
Minor pg_dump improvements
Improve pg_dump by checking results on various fgetc() calls which previously were unchecked, ditto for ftello. Also clean up a couple of very minor memory leaks by waiting to allocate structures until after the initial check(s). Issues spotted by Coverity.
1 parent 66c04c9 commit cfa1b4a

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/bin/pg_dump/pg_backup_archiver.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -1898,13 +1898,22 @@ _discoverArchiveFormat(ArchiveHandle *AH)
18981898

18991899
if (strncmp(sig, "PGDMP", 5) == 0)
19001900
{
1901+
int byteread;
1902+
19011903
/*
19021904
* Finish reading (most of) a custom-format header.
19031905
*
19041906
* NB: this code must agree with ReadHead().
19051907
*/
1906-
AH->vmaj = fgetc(fh);
1907-
AH->vmin = fgetc(fh);
1908+
if ((byteread = fgetc(fh)) == EOF)
1909+
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
1910+
1911+
AH->vmaj = byteread;
1912+
1913+
if ((byteread = fgetc(fh)) == EOF)
1914+
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
1915+
1916+
AH->vmin = byteread;
19081917

19091918
/* Save these too... */
19101919
AH->lookahead[AH->lookaheadLen++] = AH->vmaj;
@@ -1913,7 +1922,10 @@ _discoverArchiveFormat(ArchiveHandle *AH)
19131922
/* Check header version; varies from V1.0 */
19141923
if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0))) /* Version > 1.0 */
19151924
{
1916-
AH->vrev = fgetc(fh);
1925+
if ((byteread = fgetc(fh)) == EOF)
1926+
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
1927+
1928+
AH->vrev = byteread;
19171929
AH->lookahead[AH->lookaheadLen++] = AH->vrev;
19181930
}
19191931
else
@@ -1922,18 +1934,21 @@ _discoverArchiveFormat(ArchiveHandle *AH)
19221934
/* Make a convenient integer <maj><min><rev>00 */
19231935
AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
19241936

1925-
AH->intSize = fgetc(fh);
1937+
if ((AH->intSize = fgetc(fh)) == EOF)
1938+
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
19261939
AH->lookahead[AH->lookaheadLen++] = AH->intSize;
19271940

19281941
if (AH->version >= K_VERS_1_7)
19291942
{
1930-
AH->offSize = fgetc(fh);
1943+
if ((AH->offSize = fgetc(fh)) == EOF)
1944+
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
19311945
AH->lookahead[AH->lookaheadLen++] = AH->offSize;
19321946
}
19331947
else
19341948
AH->offSize = AH->intSize;
19351949

1936-
AH->format = fgetc(fh);
1950+
if ((AH->format = fgetc(fh)) == EOF)
1951+
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
19371952
AH->lookahead[AH->lookaheadLen++] = AH->format;
19381953
}
19391954
else

src/bin/pg_dump/pg_backup_custom.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ _CloseArchive(ArchiveHandle *AH)
708708
{
709709
WriteHead(AH);
710710
tpos = ftello(AH->FH);
711+
if (tpos < 0 || errno)
712+
exit_horribly(modulename, "could not determine seek position in archive file: %s\n",
713+
strerror(errno));
711714
WriteToc(AH);
712715
ctx->dataStart = _getFilePos(AH, ctx);
713716
WriteDataChunks(AH, NULL);
@@ -756,7 +759,7 @@ _ReopenArchive(ArchiveHandle *AH)
756759

757760
errno = 0;
758761
tpos = ftello(AH->FH);
759-
if (errno)
762+
if (tpos < 0 || errno)
760763
exit_horribly(modulename, "could not determine seek position in archive file: %s\n",
761764
strerror(errno));
762765

src/bin/pg_dump/pg_dump.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -7145,7 +7145,7 @@ getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
71457145
PGresult *res;
71467146
int ntups;
71477147
int i;
7148-
PQExpBuffer query = createPQExpBuffer();
7148+
PQExpBuffer query;
71497149
FdwInfo *fdwinfo;
71507150
int i_tableoid;
71517151
int i_oid;
@@ -7163,6 +7163,8 @@ getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
71637163
return NULL;
71647164
}
71657165

7166+
query = createPQExpBuffer();
7167+
71667168
/* Make sure we are in proper schema */
71677169
selectSourceSchema(fout, "pg_catalog");
71687170

@@ -7251,7 +7253,7 @@ getForeignServers(Archive *fout, int *numForeignServers)
72517253
PGresult *res;
72527254
int ntups;
72537255
int i;
7254-
PQExpBuffer query = createPQExpBuffer();
7256+
PQExpBuffer query;
72557257
ForeignServerInfo *srvinfo;
72567258
int i_tableoid;
72577259
int i_oid;
@@ -7270,6 +7272,8 @@ getForeignServers(Archive *fout, int *numForeignServers)
72707272
return NULL;
72717273
}
72727274

7275+
query = createPQExpBuffer();
7276+
72737277
/* Make sure we are in proper schema */
72747278
selectSourceSchema(fout, "pg_catalog");
72757279

0 commit comments

Comments
 (0)