Fix parsePGArray() error checking in pg_dump.
authorThomas Munro <[email protected]>
Mon, 9 Nov 2020 03:01:51 +0000 (16:01 +1300)
committerThomas Munro <[email protected]>
Mon, 9 Nov 2020 03:26:47 +0000 (16:26 +1300)
Coverity complained about a defect in commit 257836a7:

  Calling "parsePGArray" without checking return value (as is
  done elsewhere 11 out of 13 times).

Fix, and also check for empty strings explicitly (NULL as represented by
PQgetvalue()).  That worked correctly before only because parsePGArray()
happens to set *nitems = 0 when it fails on an empty string.  Also
convert a sanity check assertion to an error to be more paranoid, and
pgindent a nearby line.

Reported-by: Michael Paquier <[email protected]>
src/bin/pg_dump/pg_dump.c

index 3c276f2bcb44cfaccedb51727cf2a2111ec9600d..c68db75b97d03bbccad9d78824dea0eba9cd3935 100644 (file)
@@ -18573,13 +18573,25 @@ appendIndexCollationVersion(PQExpBuffer buffer, IndxInfo *indxinfo, int enc,
    }
 
    /* Restore the versions that were recorded by the old cluster (if any). */
-   parsePGArray(inddependcollnames,
-                &inddependcollnamesarray,
-                &ninddependcollnames);
-   parsePGArray(inddependcollversions,
-                &inddependcollversionsarray,
-                &ninddependcollversions);
-   Assert(ninddependcollnames == ninddependcollversions);
+   if (strlen(inddependcollnames) == 0 && strlen(inddependcollversions) == 0)
+   {
+       ninddependcollnames = ninddependcollversions = 0;
+       inddependcollnamesarray = inddependcollversionsarray = NULL;
+   }
+   else
+   {
+       if (!parsePGArray(inddependcollnames,
+                         &inddependcollnamesarray,
+                         &ninddependcollnames))
+           fatal("could not parse index collation name array");
+       if (!parsePGArray(inddependcollversions,
+                         &inddependcollversionsarray,
+                         &ninddependcollversions))
+           fatal("could not parse index collation version array");
+   }
+
+   if (ninddependcollnames != ninddependcollversions)
+       fatal("mismatched number of collation names and versions for index");
 
    if (ninddependcollnames > 0)
        appendPQExpBufferStr(buffer,
@@ -18594,7 +18606,7 @@ appendIndexCollationVersion(PQExpBuffer buffer, IndxInfo *indxinfo, int enc,
                          "UPDATE pg_catalog.pg_depend SET refobjversion = %s WHERE objid = '%u'::pg_catalog.oid AND refclassid = 'pg_catalog.pg_collation'::regclass AND refobjversion IS NOT NULL AND refobjid = ",
                          inddependcollversionsarray[i],
                          indxinfo->dobj.catId.oid);
-       appendStringLiteralAH(buffer,inddependcollnamesarray[i], fout);
+       appendStringLiteralAH(buffer, inddependcollnamesarray[i], fout);
        appendPQExpBuffer(buffer, "::regcollation;\n");
    }