Skip to content

Commit 4ed22e8

Browse files
committed
Check get_tle_by_resno() result before deref
When creating a sort to support a group by, we need to look up the target entry in the target list by the resno using get_tle_by_resno(). This particular code-path didn't check the result prior to attempting to dereference it, while all other callers did. While I can't see a way for this usage of get_tle_by_resno() to fail (you can't ask for a column to be sorted on which isn't included in the group by), it's probably best to check that we didn't end up with a NULL somehow anyway than risk the segfault. I'm willing to back-patch this if others feel it's necessary, but my guess is new features are what might tickle this rather than anything existing. Missing check spotted by the Coverity scanner.
1 parent 4403a9d commit 4ed22e8

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/backend/optimizer/plan/createplan.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,6 +4188,9 @@ make_sort_from_groupcols(PlannerInfo *root,
41884188
SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
41894189
TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
41904190

4191+
if (!tle)
4192+
elog(ERROR, "could not retrive tle for sort-from-groupcols");
4193+
41914194
sortColIdx[numsortkeys] = tle->resno;
41924195
sortOperators[numsortkeys] = grpcl->sortop;
41934196
collations[numsortkeys] = exprCollation((Node *) tle->expr);

0 commit comments

Comments
 (0)