Skip to content

Commit 8bf45ea

Browse files
committed
Defend against bad trigger definitions in contrib/lo's lo_manage() trigger.
This function formerly crashed if called as a statement-level trigger, or if a column-name argument wasn't given. In passing, add the trigger name to all error messages from the function. (None of them are expected cases, so this shouldn't pose any compatibility risk.) Marc Cousin, reviewed by Sawada Masahiko
1 parent 7b63528 commit 8bf45ea

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

contrib/lo/lo.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ lo_manage(PG_FUNCTION_ARGS)
4444
HeapTuple trigtuple; /* The original value of tuple */
4545

4646
if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
47-
elog(ERROR, "not fired by trigger manager");
47+
elog(ERROR, "%s: not fired by trigger manager",
48+
trigdata->tg_trigger->tgname);
49+
50+
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) /* internal error */
51+
elog(ERROR, "%s: must be fired for row",
52+
trigdata->tg_trigger->tgname);
4853

4954
/*
5055
* Fetch some values from trigdata
@@ -54,6 +59,10 @@ lo_manage(PG_FUNCTION_ARGS)
5459
tupdesc = trigdata->tg_relation->rd_att;
5560
args = trigdata->tg_trigger->tgargs;
5661

62+
if (args == NULL) /* internal error */
63+
elog(ERROR, "%s: no column name provided in the trigger definition",
64+
trigdata->tg_trigger->tgname);
65+
5766
/* tuple to return to Executor */
5867
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
5968
rettuple = newtuple;
@@ -67,7 +76,8 @@ lo_manage(PG_FUNCTION_ARGS)
6776
attnum = SPI_fnumber(tupdesc, args[0]);
6877

6978
if (attnum <= 0)
70-
elog(ERROR, "column \"%s\" does not exist", args[0]);
79+
elog(ERROR, "%s: column \"%s\" does not exist",
80+
trigdata->tg_trigger->tgname, args[0]);
7181

7282
/*
7383
* Handle updates

0 commit comments

Comments
 (0)