]> granicus.if.org Git - postgresql/commitdiff
Fix SortTocFromFile() to cope with lines that are too long for its buffer.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 7 Apr 2011 15:40:23 +0000 (11:40 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 7 Apr 2011 15:40:23 +0000 (11:40 -0400)
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.

src/bin/pg_dump/pg_backup_archiver.c

index ff722db3c67c84cb731e77df82a26c302f833970..7af3db5d64610b524753101966fe3f05b89ba224 100644 (file)
@@ -1003,11 +1003,8 @@ SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
 {
        ArchiveHandle *AH = (ArchiveHandle *) AHX;
        FILE       *fh;
-       char            buf[1024];
-       char       *cmnt;
-       char       *endptr;
-       DumpId          id;
-       TocEntry   *te;
+       char            buf[100];
+       bool            incomplete_line;
 
        /* Allocate space for the 'wanted' array, and init it */
        ropt->idWanted = (bool *) malloc(sizeof(bool) * AH->maxDumpId);
@@ -1019,8 +1016,30 @@ SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
                die_horribly(AH, modulename, "could not open TOC file \"%s\": %s\n",
                                         ropt->tocFile, strerror(errno));
 
+       incomplete_line = false;
        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 */
                cmnt = strchr(buf, ';');
                if (cmnt != NULL)