Skip to content

Commit c1132aa

Browse files
committed
Check the size in COPY_POINTER_FIELD
instead of making each caller do it. Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent 18fea73 commit c1132aa

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

src/backend/nodes/copyfuncs.c

+21-33
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@
5757
#define COPY_POINTER_FIELD(fldname, sz) \
5858
do { \
5959
Size _size = (sz); \
60-
newnode->fldname = palloc(_size); \
61-
memcpy(newnode->fldname, from->fldname, _size); \
60+
if (_size > 0) \
61+
{ \
62+
newnode->fldname = palloc(_size); \
63+
memcpy(newnode->fldname, from->fldname, _size); \
64+
} \
6265
} while (0)
6366

6467
/* Copy a parse location field (for Copy, this is same as scalar case) */
@@ -296,12 +299,9 @@ _copyRecursiveUnion(const RecursiveUnion *from)
296299
*/
297300
COPY_SCALAR_FIELD(wtParam);
298301
COPY_SCALAR_FIELD(numCols);
299-
if (from->numCols > 0)
300-
{
301-
COPY_POINTER_FIELD(dupColIdx, from->numCols * sizeof(AttrNumber));
302-
COPY_POINTER_FIELD(dupOperators, from->numCols * sizeof(Oid));
303-
COPY_POINTER_FIELD(dupCollations, from->numCols * sizeof(Oid));
304-
}
302+
COPY_POINTER_FIELD(dupColIdx, from->numCols * sizeof(AttrNumber));
303+
COPY_POINTER_FIELD(dupOperators, from->numCols * sizeof(Oid));
304+
COPY_POINTER_FIELD(dupCollations, from->numCols * sizeof(Oid));
305305
COPY_SCALAR_FIELD(numGroups);
306306

307307
return newnode;
@@ -896,13 +896,10 @@ _copyMergeJoin(const MergeJoin *from)
896896
COPY_SCALAR_FIELD(skip_mark_restore);
897897
COPY_NODE_FIELD(mergeclauses);
898898
numCols = list_length(from->mergeclauses);
899-
if (numCols > 0)
900-
{
901-
COPY_POINTER_FIELD(mergeFamilies, numCols * sizeof(Oid));
902-
COPY_POINTER_FIELD(mergeCollations, numCols * sizeof(Oid));
903-
COPY_POINTER_FIELD(mergeStrategies, numCols * sizeof(int));
904-
COPY_POINTER_FIELD(mergeNullsFirst, numCols * sizeof(bool));
905-
}
899+
COPY_POINTER_FIELD(mergeFamilies, numCols * sizeof(Oid));
900+
COPY_POINTER_FIELD(mergeCollations, numCols * sizeof(Oid));
901+
COPY_POINTER_FIELD(mergeStrategies, numCols * sizeof(int));
902+
COPY_POINTER_FIELD(mergeNullsFirst, numCols * sizeof(bool));
906903

907904
return newnode;
908905
}
@@ -1064,12 +1061,9 @@ _copyAgg(const Agg *from)
10641061
COPY_SCALAR_FIELD(aggstrategy);
10651062
COPY_SCALAR_FIELD(aggsplit);
10661063
COPY_SCALAR_FIELD(numCols);
1067-
if (from->numCols > 0)
1068-
{
1069-
COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
1070-
COPY_POINTER_FIELD(grpOperators, from->numCols * sizeof(Oid));
1071-
COPY_POINTER_FIELD(grpCollations, from->numCols * sizeof(Oid));
1072-
}
1064+
COPY_POINTER_FIELD(grpColIdx, from->numCols * sizeof(AttrNumber));
1065+
COPY_POINTER_FIELD(grpOperators, from->numCols * sizeof(Oid));
1066+
COPY_POINTER_FIELD(grpCollations, from->numCols * sizeof(Oid));
10731067
COPY_SCALAR_FIELD(numGroups);
10741068
COPY_SCALAR_FIELD(transitionSpace);
10751069
COPY_BITMAPSET_FIELD(aggParams);
@@ -1091,19 +1085,13 @@ _copyWindowAgg(const WindowAgg *from)
10911085

10921086
COPY_SCALAR_FIELD(winref);
10931087
COPY_SCALAR_FIELD(partNumCols);
1094-
if (from->partNumCols > 0)
1095-
{
1096-
COPY_POINTER_FIELD(partColIdx, from->partNumCols * sizeof(AttrNumber));
1097-
COPY_POINTER_FIELD(partOperators, from->partNumCols * sizeof(Oid));
1098-
COPY_POINTER_FIELD(partCollations, from->partNumCols * sizeof(Oid));
1099-
}
1088+
COPY_POINTER_FIELD(partColIdx, from->partNumCols * sizeof(AttrNumber));
1089+
COPY_POINTER_FIELD(partOperators, from->partNumCols * sizeof(Oid));
1090+
COPY_POINTER_FIELD(partCollations, from->partNumCols * sizeof(Oid));
11001091
COPY_SCALAR_FIELD(ordNumCols);
1101-
if (from->ordNumCols > 0)
1102-
{
1103-
COPY_POINTER_FIELD(ordColIdx, from->ordNumCols * sizeof(AttrNumber));
1104-
COPY_POINTER_FIELD(ordOperators, from->ordNumCols * sizeof(Oid));
1105-
COPY_POINTER_FIELD(ordCollations, from->ordNumCols * sizeof(Oid));
1106-
}
1092+
COPY_POINTER_FIELD(ordColIdx, from->ordNumCols * sizeof(AttrNumber));
1093+
COPY_POINTER_FIELD(ordOperators, from->ordNumCols * sizeof(Oid));
1094+
COPY_POINTER_FIELD(ordCollations, from->ordNumCols * sizeof(Oid));
11071095
COPY_SCALAR_FIELD(frameOptions);
11081096
COPY_NODE_FIELD(startOffset);
11091097
COPY_NODE_FIELD(endOffset);

0 commit comments

Comments
 (0)