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 11, 2024
1 parent 8d9b062 commit de734fb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .unreleased/fix_6820
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes: #6820 Fix a crash when the ts_hypertable_insert_blocker was called directly
Thanks: @brasic for reporting a crash when the ts_hypertable_insert_blocker was called directly
9 changes: 6 additions & 3 deletions src/hypertable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1318,12 +1318,15 @@ 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;
Ensure(trigdata != NULL, "trigdata has to be set");
Ensure(trigdata->tg_relation != NULL, "tg_relation has to be set");

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 de734fb

Please sign in to comment.