Skip to content

Commit da9f9f7

Browse files
committed
Fix possible use after free in expand_partitioned_rtentry()
It's possible that if the only live partition is concurrently dropped and try_table_open() fails, that the bms_del_member() will pfree the live_parts Bitmapset. Since the bms_del_member() call does not assign the result back to the live_parts local variable, the while loop could segfault as that variable would still reference the pfree'd Bitmapset. Backpatch to 15. 52f3de8 was backpatched to 14, but there's no bms_del_member() there due to live_parts not yet existing in RelOptInfo in that version. Technically there's no bug in version 15 as bms_del_member() didn't pfree when the set became empty prior to 00b4146 (from v16). Applied to v15 anyway to keep the code similar and to avoid the bad coding pattern. Author: Bernd Reiß <[email protected]> Reviewed-by: David Rowley <[email protected]> Discussion: https://postgr.es/m/[email protected] Backpatch-through: 15
1 parent f225473 commit da9f9f7

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/backend/optimizer/util/inherit.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
322322
PlanRowMark *top_parentrc, LOCKMODE lockmode)
323323
{
324324
PartitionDesc partdesc;
325-
Bitmapset *live_parts;
326325
int num_live_parts;
327326
int i;
328327

@@ -356,10 +355,10 @@ expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
356355
* that survive pruning. Below, we will initialize child objects for the
357356
* surviving partitions.
358357
*/
359-
relinfo->live_parts = live_parts = prune_append_rel_partitions(relinfo);
358+
relinfo->live_parts = prune_append_rel_partitions(relinfo);
360359

361360
/* Expand simple_rel_array and friends to hold child objects. */
362-
num_live_parts = bms_num_members(live_parts);
361+
num_live_parts = bms_num_members(relinfo->live_parts);
363362
if (num_live_parts > 0)
364363
expand_planner_arrays(root, num_live_parts);
365364

@@ -378,7 +377,7 @@ expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
378377
* table itself, because it's not going to be scanned.
379378
*/
380379
i = -1;
381-
while ((i = bms_next_member(live_parts, i)) >= 0)
380+
while ((i = bms_next_member(relinfo->live_parts, i)) >= 0)
382381
{
383382
Oid childOID = partdesc->oids[i];
384383
Relation childrel;

0 commit comments

Comments
 (0)