From ba7cf152c9841a9664df735d28e0bbe0f25bcc51 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Mon, 16 Nov 2026 14:11:08 +0300 Subject: [PATCH] Fix backend crashes on NULL arguments in stddev final functions and age_vle age_float8_stddev_samp_aggfinalfn() and age_float8_stddev_pop_aggfinalfn() are not STRICT, but they pass their argument straight to PostgreSQL's float8_stddev_samp() / float8_stddev_pop(), which do not check for NULL. A direct call with NULL crashes the backend with SIGSEGV: SELECT ag_catalog.age_float8_stddev_samp_aggfinalfn(NULL); SELECT ag_catalog.age_float8_stddev_pop_aggfinalfn(NULL); The stDev() / stDevP() aggregates never pass a NULL state (initcond is {0,0,0} and the transition function is STRICT), so only direct calls are affected. Return NULL for a NULL state, as a STRICT function would. The 8 argument form of age_vle() reads its last argument, the VLE grammar node id, without a NULL check, so passing NULL there crashes the backend the same way. Add it to the existing check for required arguments, which raises "age_vle: invalid NULL argument passed". A SIGSEGV in one backend makes the postmaster reinitialize and drops every other session on the instance. Found by calling every non-STRICT function of the extension with NULL arguments (all NULL, and one NULL at a time). Besides these three, only age_create_barbell_graph() with a NULL node label crashed, and that is already fixed on master (#2521). Reproduced on 1.7.0, 1.8.0 and master with PostgreSQL 18. --- regress/expected/cypher_vle.out | 3 +++ regress/expected/expr.out | 13 +++++++++++++ regress/sql/cypher_vle.sql | 3 +++ regress/sql/expr.sql | 3 +++ src/backend/utils/adt/age_vle.c | 3 ++- src/backend/utils/adt/agtype.c | 16 ++++++++++++++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/regress/expected/cypher_vle.out b/regress/expected/cypher_vle.out index fac61e5f3..11c5607c2 100644 --- a/regress/expected/cypher_vle.out +++ b/regress/expected/cypher_vle.out @@ -129,6 +129,9 @@ SELECT count(edges) FROM start_and_end_points, age_vle( '"cypher_vle"'::agtype, 1 (1 row) +-- Test the 8 argument form with a NULL VLE grammar node id - should error +SELECT count(edges) FROM start_and_end_points, age_vle( '"cypher_vle"'::agtype, start_vertex, end_vertex, '{"id": 1111111111111111, "label": "", "end_id": 2222222222222222, "start_id": 333333333333333, "properties": {}}::edge'::agtype, '1'::agtype, 'null'::agtype, '1'::agtype, NULL); +ERROR: age_vle: invalid NULL argument passed -- Test the VLE match integration -- Each should find 400 SELECT * FROM cypher('cypher_vle', $$MATCH (u:begin)-[*]->(v:end) RETURN count(*) $$) AS (e agtype); diff --git a/regress/expected/expr.out b/regress/expected/expr.out index 806a6f65c..51b863551 100644 --- a/regress/expected/expr.out +++ b/regress/expected/expr.out @@ -6698,6 +6698,19 @@ ERROR: function ag_catalog.age_stdevp() does not exist LINE 1: SELECT * FROM cypher('UCSC', $$ RETURN stDevP() $$) AS (stDe... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. +-- direct calls of the final functions with a NULL state - should return NULL +SELECT age_float8_stddev_samp_aggfinalfn(NULL); + age_float8_stddev_samp_aggfinalfn +----------------------------------- + +(1 row) + +SELECT age_float8_stddev_pop_aggfinalfn(NULL); + age_float8_stddev_pop_aggfinalfn +---------------------------------- + +(1 row) + -- -- aggregate functions percentileCont() & percentileDisc() -- diff --git a/regress/sql/cypher_vle.sql b/regress/sql/cypher_vle.sql index 12359f4de..4b35b5be5 100644 --- a/regress/sql/cypher_vle.sql +++ b/regress/sql/cypher_vle.sql @@ -73,6 +73,9 @@ SELECT count(edges) FROM start_and_end_points, age_vle( '"cypher_vle"'::agtype, -- Test object property matching - should match 1 SELECT count(edges) FROM start_and_end_points, age_vle( '"cypher_vle"'::agtype, start_vertex, end_vertex, '{"id": 1111111111111111, "label": "", "end_id": 2222222222222222, "start_id": 333333333333333, "properties": {"dangerous": {"type": "poisons", "level": "all"}}}::edge'::agtype, '1'::agtype, 'null'::agtype, '0'::agtype); +-- Test the 8 argument form with a NULL VLE grammar node id - should error +SELECT count(edges) FROM start_and_end_points, age_vle( '"cypher_vle"'::agtype, start_vertex, end_vertex, '{"id": 1111111111111111, "label": "", "end_id": 2222222222222222, "start_id": 333333333333333, "properties": {}}::edge'::agtype, '1'::agtype, 'null'::agtype, '1'::agtype, NULL); + -- Test the VLE match integration -- Each should find 400 SELECT * FROM cypher('cypher_vle', $$MATCH (u:begin)-[*]->(v:end) RETURN count(*) $$) AS (e agtype); diff --git a/regress/sql/expr.sql b/regress/sql/expr.sql index d4d900a1c..0cade79af 100644 --- a/regress/sql/expr.sql +++ b/regress/sql/expr.sql @@ -2764,6 +2764,9 @@ SELECT * FROM cypher('UCSC', $$ RETURN stDevP(NULL) $$) AS (stDevP agtype); -- should fail SELECT * FROM cypher('UCSC', $$ RETURN stDev() $$) AS (stDev agtype); SELECT * FROM cypher('UCSC', $$ RETURN stDevP() $$) AS (stDevP agtype); +-- direct calls of the final functions with a NULL state - should return NULL +SELECT age_float8_stddev_samp_aggfinalfn(NULL); +SELECT age_float8_stddev_pop_aggfinalfn(NULL); -- -- aggregate functions percentileCont() & percentileDisc() diff --git a/src/backend/utils/adt/age_vle.c b/src/backend/utils/adt/age_vle.c index c4dc7efdb..136971919 100644 --- a/src/backend/utils/adt/age_vle.c +++ b/src/backend/utils/adt/age_vle.c @@ -1972,7 +1972,8 @@ Datum age_vle(PG_FUNCTION_ARGS) /* all of these arguments need to be non NULL */ if (PG_ARGISNULL(0) || /* graph name */ PG_ARGISNULL(3) || /* edge prototype */ - PG_ARGISNULL(6)) /* direction */ + PG_ARGISNULL(6) || /* direction */ + (PG_NARGS() == 8 && PG_ARGISNULL(7))) /* VLE grammar node id */ { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c index cc9cc7717..90b6fbda1 100644 --- a/src/backend/utils/adt/agtype.c +++ b/src/backend/utils/adt/agtype.c @@ -11525,6 +11525,16 @@ Datum age_float8_stddev_samp_aggfinalfn(PG_FUNCTION_ARGS) PGFunction func; agtype_value agtv_float; + /* + * The function is not STRICT, so it can be called directly with a NULL + * state. float8_stddev_samp does not check for that and would dereference + * it, so return NULL here, as a STRICT function would. + */ + if (PG_ARGISNULL(0)) + { + PG_RETURN_NULL(); + } + /* we can't use DirectFunctionCall1 as it errors for NULL values */ func = float8_stddev_samp; result = (*func) (fcinfo); @@ -11557,6 +11567,12 @@ Datum age_float8_stddev_pop_aggfinalfn(PG_FUNCTION_ARGS) PGFunction func; agtype_value agtv_float; + /* same as above, float8_stddev_pop does not check for a NULL state */ + if (PG_ARGISNULL(0)) + { + PG_RETURN_NULL(); + } + /* we can't use DirectFunctionCall1 as it errors for NULL values */ func = float8_stddev_pop; result = (*func) (fcinfo);