Skip to content

Commit

Permalink
Check for trigger context before accessing data
Browse files Browse the repository at this point in the history
The ts_hypertable_insert_blocker function was accessing data from the
trigger context before it was tested that a trigger context actually
exists. This led to a crash when the function was called directly.

Fixes: #6819
  • Loading branch information
jnidzwetzki committed Apr 10, 2024
1 parent 8d9b062 commit 6aa014d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/hypertable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1318,12 +1318,12 @@ TS_FUNCTION_INFO_V1(ts_hypertable_insert_blocker);
Datum
ts_hypertable_insert_blocker(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
const char *relname = get_rel_name(trigdata->tg_relation->rd_id);

if (!CALLED_AS_TRIGGER(fcinfo))
elog(ERROR, "insert_blocker: not called by trigger manager");

TriggerData *trigdata = (TriggerData *) fcinfo->context;
const char *relname = get_rel_name(trigdata->tg_relation->rd_id);

if (ts_guc_restoring)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
Expand Down
3 changes: 3 additions & 0 deletions test/expected/triggers.out
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,6 @@ ERROR: trigger with transition tables not supported on hypertables
CREATE TRIGGER t4 AFTER DELETE ON transition_test REFERENCING OLD TABLE AS old_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
ERROR: trigger with transition tables not supported on hypertables
\set ON_ERROR_STOP 1
-- Test insert blocker trigger does not crash when called directly
SELECT _timescaledb_functions.insert_blocker();
ERROR: insert_blocker: not called by trigger manager
2 changes: 2 additions & 0 deletions test/sql/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,6 @@ CREATE TRIGGER t3 AFTER UPDATE ON transition_test REFERENCING NEW TABLE AS new_t
CREATE TRIGGER t4 AFTER DELETE ON transition_test REFERENCING OLD TABLE AS old_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
\set ON_ERROR_STOP 1

-- Test insert blocker trigger does not crash when called directly
SELECT _timescaledb_functions.insert_blocker();

0 comments on commit 6aa014d

Please sign in to comment.