1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-27 22:56:53 +03:00

Fix SortTocFromFile() to cope with lines that are too long for its buffer.

The original coding supposed that a dump TOC file could never contain lines
longer than 1K.  The folly of that was exposed by a recent report from
Per-Olov Esgard.  We only really need to see the first dozen or two bytes
of each line, since we're just trying to read off the numeric ID at the
start of the line; so there's no need for a particularly huge buffer.
What there is a need for is logic to not process continuation bufferloads.

Back-patch to all supported branches, since it's always been like this.
This commit is contained in:
Tom Lane 2011-04-07 11:40:44 -04:00
parent 93acf043b2
commit 00a9229060

View File

@ -789,11 +789,8 @@ SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
{ {
ArchiveHandle *AH = (ArchiveHandle *) AHX; ArchiveHandle *AH = (ArchiveHandle *) AHX;
FILE *fh; FILE *fh;
char buf[1024]; char buf[100];
char *cmnt; bool incomplete_line;
char *endptr;
DumpId id;
TocEntry *te;
TocEntry *tePrev; TocEntry *tePrev;
/* Allocate space for the 'wanted' array, and init it */ /* Allocate space for the 'wanted' array, and init it */
@ -809,8 +806,30 @@ SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
die_horribly(AH, modulename, "could not open TOC file: %s\n", die_horribly(AH, modulename, "could not open TOC file: %s\n",
strerror(errno)); strerror(errno));
incomplete_line = false;
while (fgets(buf, sizeof(buf), fh) != NULL) while (fgets(buf, sizeof(buf), fh) != NULL)
{ {
bool prev_incomplete_line = incomplete_line;
int buflen;
char *cmnt;
char *endptr;
DumpId id;
TocEntry *te;
/*
* Some lines in the file might be longer than sizeof(buf). This is
* no problem, since we only care about the leading numeric ID which
* can be at most a few characters; but we have to skip continuation
* bufferloads when processing a long line.
*/
buflen = strlen(buf);
if (buflen > 0 && buf[buflen - 1] == '\n')
incomplete_line = false;
else
incomplete_line = true;
if (prev_incomplete_line)
continue;
/* Truncate line at comment, if any */ /* Truncate line at comment, if any */
cmnt = strchr(buf, ';'); cmnt = strchr(buf, ';');
if (cmnt != NULL) if (cmnt != NULL)