Check the size in COPY_POINTER_FIELD
authorPeter Eisentraut
Sun, 8 Aug 2021 14:55:51 +0000 (16:55 +0200)
committerPeter Eisentraut
Sun, 8 Aug 2021 16:46:34 +0000 (18:46 +0200)
instead of making each caller do it.

Discussion: https://www.postgresql.org/message-id/flat/c1097590-a6a4-486a-64b1-e1f9cc0533ce@enterprisedb.com

src/backend/nodes/copyfuncs.c

index 29020c908e87aa531abc7edcfd799d0cdd87f101..38251c2b8e2b756d0399adeceadacd2593afca5e 100644 (file)
 #define COPY_POINTER_FIELD(fldname, sz) \
    do { \
        Size    _size = (sz); \
-       newnode->fldname = palloc(_size); \
-       memcpy(newnode->fldname, from->fldname, _size); \
+       if (_size > 0) \
+       { \
+           newnode->fldname = palloc(_size); \
+           memcpy(newnode->fldname, from->fldname, _size); \
+       } \
    } while (0)
 
 /* Copy a parse location field (for Copy, this is same as scalar case) */
@@ -296,12 +299,9 @@ _copyRecursiveUnion(const RecursiveUnion *from)
     */
    COPY_SCALAR_FIELD(wtParam);
    COPY_SCALAR_FIELD(numCols);
-   if (from->numCols > 0)
-   {
-       COPY_POINTER_FIELD(dupColIdx, from->numCols * sizeof(AttrNumber));
-       COPY_POINTER_FIELD(dupOperators, from->numCols * sizeof(Oid));
-       COPY_POINTER_FIELD(dupCollations, from->numCols * sizeof(Oid));
-   }
+   COPY_POINTER_FIELD(dupColIdx, from->numCols * sizeof(AttrNumber));
+   COPY_POINTER_FIELD(dupOperators, from->numCols * sizeof(Oid));
+   COPY_POINTER_FIELD(dupCollations, from->numCols * sizeof(Oid));
    COPY_SCALAR_FIELD(numGroups);
 
    return newnode;
@@ -896,13 +896,10 @@ _copyMergeJoin(const MergeJoin *from)
    COPY_SCALAR_FIELD(skip_mark_restore);
    COPY_NODE_FIELD(mergeclauses);
    numCols = list_length(from->mergeclauses);
-   if (numCols > 0)
-   {
-       COPY_POINTER_FIELD(mergeFamilies, numCols * sizeof(Oid));
-       COPY_POINTER_FIELD(mergeCollations, numCols * sizeof(Oid));
-       COPY_POINTER_FIELD(mergeStrategies, numCols * sizeof(int));
-       COPY_POINTER_FIELD(mergeNullsFirst, numCols * sizeof(bool));
-   }
+   COPY_POINTER_FIELD(mergeFamilies, numCols * sizeof(Oid));
+   COPY_POINTER_FIELD(mergeCollations, numCols * sizeof(Oid));
+   COPY_POINTER_FIELD(mergeStrategies, numCols * sizeof(int));
+   COPY_POINTER_FIELD(mergeNullsFirst, numCols * sizeof(bool));
 
    return newnode;
 }
@@ -1064,12 +1061,9 @@ _copyAgg(const Agg *from)
    COPY_SCALAR_FIELD(aggstrategy);
    COPY_SCALAR_FIELD(aggsplit);
    COPY_SCALAR_FIELD(numCols);
-   if (from->numCols > 0)
-   {
-       COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
-       COPY_POINTER_FIELD(grpOperators, from->numCols * sizeof(Oid));
-       COPY_POINTER_FIELD(grpCollations, from->numCols * sizeof(Oid));
-   }
+   COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
+   COPY_POINTER_FIELD(grpOperators, from->numCols * sizeof(Oid));
+   COPY_POINTER_FIELD(grpCollations, from->numCols * sizeof(Oid));
    COPY_SCALAR_FIELD(numGroups);
    COPY_SCALAR_FIELD(transitionSpace);
    COPY_BITMAPSET_FIELD(aggParams);
@@ -1091,19 +1085,13 @@ _copyWindowAgg(const WindowAgg *from)
 
    COPY_SCALAR_FIELD(winref);
    COPY_SCALAR_FIELD(partNumCols);
-   if (from->partNumCols > 0)
-   {
-       COPY_POINTER_FIELD(partColIdx, from->partNumCols * sizeof(AttrNumber));
-       COPY_POINTER_FIELD(partOperators, from->partNumCols * sizeof(Oid));
-       COPY_POINTER_FIELD(partCollations, from->partNumCols * sizeof(Oid));
-   }
+   COPY_POINTER_FIELD(partColIdx, from->partNumCols * sizeof(AttrNumber));
+   COPY_POINTER_FIELD(partOperators, from->partNumCols * sizeof(Oid));
+   COPY_POINTER_FIELD(partCollations, from->partNumCols * sizeof(Oid));
    COPY_SCALAR_FIELD(ordNumCols);
-   if (from->ordNumCols > 0)
-   {
-       COPY_POINTER_FIELD(ordColIdx, from->ordNumCols * sizeof(AttrNumber));
-       COPY_POINTER_FIELD(ordOperators, from->ordNumCols * sizeof(Oid));
-       COPY_POINTER_FIELD(ordCollations, from->ordNumCols * sizeof(Oid));
-   }
+   COPY_POINTER_FIELD(ordColIdx, from->ordNumCols * sizeof(AttrNumber));
+   COPY_POINTER_FIELD(ordOperators, from->ordNumCols * sizeof(Oid));
+   COPY_POINTER_FIELD(ordCollations, from->ordNumCols * sizeof(Oid));
    COPY_SCALAR_FIELD(frameOptions);
    COPY_NODE_FIELD(startOffset);
    COPY_NODE_FIELD(endOffset);