]> granicus.if.org Git - postgresql/commitdiff
Defend against bad trigger definitions in contrib/lo's lo_manage() trigger.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 24 Nov 2013 03:45:46 +0000 (22:45 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 24 Nov 2013 03:46:08 +0000 (22:46 -0500)
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

contrib/lo/lo.c

index 9dbbbcebd23866acecc3257b3b8e6300c7233943..757758fe7f8f439b7422c95a561815658c67938c 100644 (file)
@@ -40,7 +40,12 @@ lo_manage(PG_FUNCTION_ARGS)
        HeapTuple       trigtuple;              /* The original value of tuple  */
 
        if (!CALLED_AS_TRIGGER(fcinfo))         /* internal error */
-               elog(ERROR, "not fired by trigger manager");
+               elog(ERROR, "%s: not fired by trigger manager",
+                        trigdata->tg_trigger->tgname);
+
+       if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))         /* internal error */
+               elog(ERROR, "%s: must be fired for row",
+                        trigdata->tg_trigger->tgname);
 
        /*
         * Fetch some values from trigdata
@@ -50,6 +55,10 @@ lo_manage(PG_FUNCTION_ARGS)
        tupdesc = trigdata->tg_relation->rd_att;
        args = trigdata->tg_trigger->tgargs;
 
+       if (args == NULL)                       /* internal error */
+               elog(ERROR, "%s: no column name provided in the trigger definition",
+                        trigdata->tg_trigger->tgname);
+
        /* tuple to return to Executor */
        if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
                rettuple = newtuple;
@@ -63,7 +72,8 @@ lo_manage(PG_FUNCTION_ARGS)
        attnum = SPI_fnumber(tupdesc, args[0]);
 
        if (attnum <= 0)
-               elog(ERROR, "column \"%s\" does not exist", args[0]);
+               elog(ERROR, "%s: column \"%s\" does not exist",
+                        trigdata->tg_trigger->tgname, args[0]);
 
        /*
         * Handle updates