From e5b6c78585e404a48f061576432ea57a69e0adf8 Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Fri, 6 Oct 2023 16:12:10 +0200 Subject: [PATCH] Improve arg handling in continuous_agg_trigfn When continuous_agg_invalidation_trigger is called without any argument, fcinfo->context in continuous_agg_trigfn is NULL and it should not be dereferenced. --- tsl/src/continuous_aggs/insert.c | 3 ++- tsl/test/expected/cagg_usage.out | 3 +++ tsl/test/sql/cagg_usage.sql | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tsl/src/continuous_aggs/insert.c b/tsl/src/continuous_aggs/insert.c index 3087f0b0411..e96c47d4336 100644 --- a/tsl/src/continuous_aggs/insert.c +++ b/tsl/src/continuous_aggs/insert.c @@ -218,7 +218,8 @@ continuous_agg_trigfn(PG_FUNCTION_ARGS) char *hypertable_id_str, *parent_hypertable_id_str; int32 hypertable_id, parent_hypertable_id = 0; bool is_distributed_hypertable_trigger = false; - if (trigdata->tg_trigger->tgnargs < 0) + + if (trigdata == NULL || trigdata->tg_trigger == NULL || trigdata->tg_trigger->tgnargs < 0) elog(ERROR, "must supply hypertable id"); hypertable_id_str = trigdata->tg_trigger->tgargs[0]; diff --git a/tsl/test/expected/cagg_usage.out b/tsl/test/expected/cagg_usage.out index fd124b2acdd..4d67ff9a2ef 100644 --- a/tsl/test/expected/cagg_usage.out +++ b/tsl/test/expected/cagg_usage.out @@ -409,6 +409,9 @@ SELECT relname FROM pg_class WHERE oid = :mat_table; ---------------------------------------------------------------- -- Cleanup DROP TABLE whatever; +-- Check that continuous_agg_invalidation_trigger() handles no arguments properly +SELECT _timescaledb_functions.continuous_agg_invalidation_trigger(); +ERROR: must supply hypertable id -- END OF BASIC USAGE TESTS -- CREATE TABLE metrics(time timestamptz, device TEXT, value float); SELECT table_name FROM create_hypertable('metrics','time'); diff --git a/tsl/test/sql/cagg_usage.sql b/tsl/test/sql/cagg_usage.sql index 6b77da2767c..29ae3e9a567 100644 --- a/tsl/test/sql/cagg_usage.sql +++ b/tsl/test/sql/cagg_usage.sql @@ -267,6 +267,9 @@ SELECT relname FROM pg_class WHERE oid = :mat_table; -- Cleanup DROP TABLE whatever; +-- Check that continuous_agg_invalidation_trigger() handles no arguments properly +SELECT _timescaledb_functions.continuous_agg_invalidation_trigger(); + -- END OF BASIC USAGE TESTS -- CREATE TABLE metrics(time timestamptz, device TEXT, value float);