Fix uninitialized memory reference.
authorRobert Haas <rhaas@postgresql.org>
Fri, 1 Dec 2017 15:01:50 +0000 (10:01 -0500)
committerRobert Haas <rhaas@postgresql.org>
Fri, 1 Dec 2017 15:05:00 +0000 (10:05 -0500)
Without this, when partdesc->nparts == 0, we end up calling
ExecBuildSlotPartitionKeyDescription without initializing values
and isnull.

Reported by Coverity via Michael Paquier.  Patch by Michael Paquier,
reviewed and revised by Amit Langote.

Discussion: http://postgr.es/m/CAB7nPqQ3mwkdMoPY-ocgTpPnjd8TKOadMxdTtMLvEzF8480Zfg@mail.gmail.com

src/backend/executor/execPartition.c
src/test/regress/expected/insert.out
src/test/regress/sql/insert.sql

index 59a0ca4597c2f4631750c9bd54015c8820c10639..34875945e81412eca0b74c15b68c4715c638481c 100644 (file)
@@ -206,13 +206,6 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
            slot = myslot;
        }
 
-       /* Quick exit */
-       if (partdesc->nparts == 0)
-       {
-           result = -1;
-           break;
-       }
-
        /*
         * Extract partition key from tuple. Expression evaluation machinery
         * that FormPartitionKeyDatum() invokes expects ecxt_scantuple to
@@ -223,6 +216,17 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
         */
        ecxt->ecxt_scantuple = slot;
        FormPartitionKeyDatum(parent, slot, estate, values, isnull);
+
+       /*
+        * Nothing for get_partition_for_tuple() to do if there are no
+        * partitions to begin with.
+        */
+       if (partdesc->nparts == 0)
+       {
+           result = -1;
+           break;
+       }
+
        cur_index = get_partition_for_tuple(rel, values, isnull);
 
        /*
index b7b37dbc39961757f1448aacf2d27529cd3cd9d0..dcbaad8e2fd1ec98b2b0fec0aca37074d6fa46aa 100644 (file)
@@ -165,6 +165,10 @@ create table range_parted (
    a text,
    b int
 ) partition by range (a, (b+0));
+-- no partitions, so fail
+insert into range_parted values ('a', 11);
+ERROR:  no partition of relation "range_parted" found for row
+DETAIL:  Partition key of the failing row contains (a, (b + 0)) = (a, 11).
 create table part1 partition of range_parted for values from ('a', 1) to ('a', 10);
 create table part2 partition of range_parted for values from ('a', 10) to ('a', 20);
 create table part3 partition of range_parted for values from ('b', 1) to ('b', 10);
index 310b818076c491043b62a94e30727cbafe25e35b..0150b6bb0febb8d0de3ad04bb26971013b50a9f7 100644 (file)
@@ -90,6 +90,10 @@ create table range_parted (
    a text,
    b int
 ) partition by range (a, (b+0));
+
+-- no partitions, so fail
+insert into range_parted values ('a', 11);
+
 create table part1 partition of range_parted for values from ('a', 1) to ('a', 10);
 create table part2 partition of range_parted for values from ('a', 10) to ('a', 20);
 create table part3 partition of range_parted for values from ('b', 1) to ('b', 10);