Skip to content

Commit 64edc78

Browse files
committed
Don't believe MinMaxExpr is leakproof without checking.
MinMaxExpr invokes the btree comparison function for its input datatype, so it's only leakproof if that function is. Many such functions are indeed leakproof, but others are not, and we should not just assume that they are. Hence, adjust contain_leaked_vars to verify the leakproofness of the referenced function explicitly. I didn't add a regression test because it would need to depend on some particular comparison function being leaky, and that's a moving target, per discussion. This has been wrong all along, so back-patch to supported branches. Discussion: https://postgr.es/m/31042.1546194242@sss.pgh.pa.us
1 parent afd090a commit 64edc78

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,6 @@ contain_leaked_vars_walker(Node *node, void *context)
15291529
case T_CaseExpr:
15301530
case T_CaseTestExpr:
15311531
case T_RowExpr:
1532-
case T_MinMaxExpr:
15331532
case T_SQLValueFunction:
15341533
case T_NullTest:
15351534
case T_BooleanTest:
@@ -1586,6 +1585,36 @@ contain_leaked_vars_walker(Node *node, void *context)
15861585
}
15871586
break;
15881587

1588+
case T_MinMaxExpr:
1589+
{
1590+
/*
1591+
* MinMaxExpr is leakproof if the comparison function it calls
1592+
* is leakproof.
1593+
*/
1594+
MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
1595+
TypeCacheEntry *typentry;
1596+
bool leakproof;
1597+
1598+
/* Look up the btree comparison function for the datatype */
1599+
typentry = lookup_type_cache(minmaxexpr->minmaxtype,
1600+
TYPECACHE_CMP_PROC);
1601+
if (OidIsValid(typentry->cmp_proc))
1602+
leakproof = get_func_leakproof(typentry->cmp_proc);
1603+
else
1604+
{
1605+
/*
1606+
* The executor will throw an error, but here we just
1607+
* treat the missing function as leaky.
1608+
*/
1609+
leakproof = false;
1610+
}
1611+
1612+
if (!leakproof &&
1613+
contain_var_clause((Node *) minmaxexpr->args))
1614+
return true;
1615+
}
1616+
break;
1617+
15891618
case T_CurrentOfExpr:
15901619

15911620
/*

0 commit comments

Comments
 (0)