Skip to content

Commit b0f20c2

Browse files
committed
Be more paranoid in ruleutils.c's get_variable().
We were merely Assert'ing that the Var matched the RTE it's supposedly from. But if the user passes incorrect information to pg_get_expr(), the RTE might in fact not match; this led either to Assert failures or core dumps, as reported by Chris Hanks in bug #14220. To fix, just convert the Asserts to test-and-elog. Adjust an existing test-and-elog elsewhere in the same function to be consistent in wording. (If we really felt these were user-facing errors, we might promote them to ereport's; but I can't convince myself that they're worth translating.) Back-patch to 9.3; the problematic code doesn't exist before that, and a quick check says that 9.2 doesn't crash on such cases. Michael Paquier and Thomas Munro Report: <20160629224349.1407.32667@wrigleys.postgresql.org>
1 parent 17bfef8 commit b0f20c2

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5537,7 +5537,8 @@ get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
55375537

55385538
tle = get_tle_by_resno(dpns->inner_tlist, var->varattno);
55395539
if (!tle)
5540-
elog(ERROR, "bogus varattno for subquery var: %d", var->varattno);
5540+
elog(ERROR, "invalid attnum %d for relation \"%s\"",
5541+
var->varattno, rte->eref->aliasname);
55415542

55425543
Assert(netlevelsup == 0);
55435544
push_child_plan(dpns, dpns->inner_planstate, &save_dpns);
@@ -5598,9 +5599,13 @@ get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
55985599
else if (attnum > 0)
55995600
{
56005601
/* Get column name to use from the colinfo struct */
5601-
Assert(attnum <= colinfo->num_cols);
5602+
if (attnum > colinfo->num_cols)
5603+
elog(ERROR, "invalid attnum %d for relation \"%s\"",
5604+
attnum, rte->eref->aliasname);
56025605
attname = colinfo->colnames[attnum - 1];
5603-
Assert(attname != NULL);
5606+
if (attname == NULL) /* dropped column? */
5607+
elog(ERROR, "invalid attnum %d for relation \"%s\"",
5608+
attnum, rte->eref->aliasname);
56045609
}
56055610
else
56065611
{

0 commit comments

Comments
 (0)