Skip to content

Commit d6b37cd

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 dcb04c4 commit d6b37cd

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,6 @@ contain_leaky_functions_walker(Node *node, void *context)
14261426
case T_CaseExpr:
14271427
case T_CaseTestExpr:
14281428
case T_RowExpr:
1429-
case T_MinMaxExpr:
14301429
case T_NullTest:
14311430
case T_BooleanTest:
14321431
case T_List:
@@ -1518,6 +1517,35 @@ contain_leaky_functions_walker(Node *node, void *context)
15181517
}
15191518
break;
15201519

1520+
case T_MinMaxExpr:
1521+
{
1522+
/*
1523+
* MinMaxExpr is leakproof if the comparison function it calls
1524+
* is leakproof.
1525+
*/
1526+
MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
1527+
TypeCacheEntry *typentry;
1528+
bool leakproof;
1529+
1530+
/* Look up the btree comparison function for the datatype */
1531+
typentry = lookup_type_cache(minmaxexpr->minmaxtype,
1532+
TYPECACHE_CMP_PROC);
1533+
if (OidIsValid(typentry->cmp_proc))
1534+
leakproof = get_func_leakproof(typentry->cmp_proc);
1535+
else
1536+
{
1537+
/*
1538+
* The executor will throw an error, but here we just
1539+
* treat the missing function as leaky.
1540+
*/
1541+
leakproof = false;
1542+
}
1543+
1544+
if (!leakproof)
1545+
return true;
1546+
}
1547+
break;
1548+
15211549
default:
15221550

15231551
/*

0 commit comments

Comments
 (0)