Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d40d827

Browse files
committed
Robustify find_base_rel and find_base_rel_ignore_join
Improve find_base_rel() and find_base_rel_ignore_join() so that they raise an ERROR if they ever receive a negative relid value in non-cassert builds. If either of these functions had ever received a negative relid then they'd have attempted to access memory that does not belong to simple_rel_array. Because no evidence has been presented of actual cases where bugs have caused this to happen, here we take a lightweight approach to checking for negative values and simply cast both values to uint32 before performing the comparison. This will cause any negative relids to be seen as greater than simple_rel_array_size which will ERROR rather than attempt to access a negative simple_rel_array element. Obviously, the run-time error is better than a crash, so it makes sense to protect against this, especially when it can be done without adding any additional run-time overhead. There is a slight change here if the functions are ever called with a relid of 0. This will pass the bounds check, but that array entry should be NULL (along with the corresponding simple_rte_array entry), so won't pass the "if (rel)" condition and still fall through and raise an ERROR. Author: Ranier Vilela Reviewed-by: Ashutosh Bapat, David Rowley Discussion: https://postgr.es/m/CAEudQArQSghBu2gLojg4o_tnHj_x2HcS%3D%2BwewL3NJS8z0VnK%2Bg%40mail.gmail.com
1 parent 3ef18a9 commit d40d827

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/backend/optimizer/util/relnode.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,8 @@ find_base_rel(PlannerInfo *root, int relid)
406406
{
407407
RelOptInfo *rel;
408408

409-
Assert(relid > 0);
410-
411-
if (relid < root->simple_rel_array_size)
409+
/* use an unsigned comparison to prevent negative array element access */
410+
if ((uint32) relid < (uint32) root->simple_rel_array_size)
412411
{
413412
rel = root->simple_rel_array[relid];
414413
if (rel)
@@ -432,9 +431,8 @@ find_base_rel(PlannerInfo *root, int relid)
432431
RelOptInfo *
433432
find_base_rel_ignore_join(PlannerInfo *root, int relid)
434433
{
435-
Assert(relid > 0);
436-
437-
if (relid < root->simple_rel_array_size)
434+
/* use an unsigned comparison to prevent negative array element access */
435+
if ((uint32) relid < (uint32) root->simple_rel_array_size)
438436
{
439437
RelOptInfo *rel;
440438
RangeTblEntry *rte;

0 commit comments

Comments
 (0)