Skip to content

Commit d25d45e

Browse files
committed
Verify range bounds to bms_add_range when necessary
Now that the bms_add_range boundary protections are gone, some alternative ones are needed in a few places. Author: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp> Discussion: https://postgr.es/m/3437ccf8-a144-55ff-1e2f-fc16b437823b@lab.ntt.co.jp
1 parent 1b68010 commit d25d45e

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/backend/executor/nodeAppend.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
171171
{
172172
/* We'll need to initialize all subplans */
173173
nplans = list_length(node->appendplans);
174+
Assert(nplans > 0);
174175
validsubplans = bms_add_range(NULL, 0, nplans - 1);
175176
}
176177

@@ -179,7 +180,10 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
179180
* immediately, preventing later calls to ExecFindMatchingSubPlans.
180181
*/
181182
if (!prunestate->do_exec_prune)
183+
{
184+
Assert(nplans > 0);
182185
appendstate->as_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
186+
}
183187
}
184188
else
185189
{
@@ -189,6 +193,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
189193
* When run-time partition pruning is not enabled we can just mark all
190194
* subplans as valid; they must also all be initialized.
191195
*/
196+
Assert(nplans > 0);
192197
appendstate->as_valid_subplans = validsubplans =
193198
bms_add_range(NULL, 0, nplans - 1);
194199
appendstate->as_prune_state = NULL;

src/backend/executor/nodeMergeAppend.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
131131
{
132132
/* We'll need to initialize all subplans */
133133
nplans = list_length(node->mergeplans);
134+
Assert(nplans > 0);
134135
validsubplans = bms_add_range(NULL, 0, nplans - 1);
135136
}
136137

@@ -139,7 +140,10 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
139140
* immediately, preventing later calls to ExecFindMatchingSubPlans.
140141
*/
141142
if (!prunestate->do_exec_prune)
143+
{
144+
Assert(nplans > 0);
142145
mergestate->ms_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
146+
}
143147
}
144148
else
145149
{
@@ -149,6 +153,7 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
149153
* When run-time partition pruning is not enabled we can just mark all
150154
* subplans as valid; they must also all be initialized.
151155
*/
156+
Assert(nplans > 0);
152157
mergestate->ms_valid_subplans = validsubplans =
153158
bms_add_range(NULL, 0, nplans - 1);
154159
mergestate->ms_prune_state = NULL;

src/backend/partitioning/partprune.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,10 @@ get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
486486

487487
/* If there are no pruning steps then all partitions match. */
488488
if (num_steps == 0)
489+
{
490+
Assert(context->nparts > 0);
489491
return bms_add_range(NULL, 0, context->nparts - 1);
492+
}
490493

491494
/*
492495
* Allocate space for individual pruning steps to store its result. Each
@@ -2048,8 +2051,12 @@ get_matching_hash_bounds(PartitionPruneContext *context,
20482051
bms_make_singleton(rowHash % greatest_modulus);
20492052
}
20502053
else
2054+
{
2055+
/* Getting here means at least one hash partition exists. */
2056+
Assert(boundinfo->ndatums > 0);
20512057
result->bound_offsets = bms_add_range(NULL, 0,
20522058
boundinfo->ndatums - 1);
2059+
}
20532060

20542061
/*
20552062
* There is neither a special hash null partition or the default hash
@@ -2128,6 +2135,7 @@ get_matching_list_bounds(PartitionPruneContext *context,
21282135
*/
21292136
if (nvalues == 0)
21302137
{
2138+
Assert(boundinfo->ndatums > 0);
21312139
result->bound_offsets = bms_add_range(NULL, 0,
21322140
boundinfo->ndatums - 1);
21332141
result->scan_default = partition_bound_has_default(boundinfo);
@@ -2140,6 +2148,7 @@ get_matching_list_bounds(PartitionPruneContext *context,
21402148
/*
21412149
* First match to all bounds. We'll remove any matching datums below.
21422150
*/
2151+
Assert(boundinfo->ndatums > 0);
21432152
result->bound_offsets = bms_add_range(NULL, 0,
21442153
boundinfo->ndatums - 1);
21452154

@@ -2250,6 +2259,7 @@ get_matching_list_bounds(PartitionPruneContext *context,
22502259
break;
22512260
}
22522261

2262+
Assert(minoff >= 0 && maxoff >= 0);
22532263
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
22542264
return result;
22552265
}
@@ -2327,6 +2337,7 @@ get_matching_range_bounds(PartitionPruneContext *context,
23272337
maxoff--;
23282338

23292339
result->scan_default = partition_bound_has_default(boundinfo);
2340+
Assert(minoff >= 0 && maxoff >= 0);
23302341
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
23312342

23322343
return result;

0 commit comments

Comments
 (0)