From: Pavan Deolasee Date: Mon, 11 Jul 2016 08:29:50 +0000 (+0530) Subject: Change several asserts checking for aggregate context to errors X-Git-Tag: XL9_5_R1_2~28 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=ffe87df927ab6469c8878a08010d119ad475f2e1;p=postgres-xl.git Change several asserts checking for aggregate context to errors This should address the crash in assert-enabled build reported by Pallavi Sontakke. SQLSmith tries to call various functions from system catalogs and functions should be prepared to handle such calls. --- diff --git a/src/backend/utils/adt/array_userfuncs.c b/src/backend/utils/adt/array_userfuncs.c index d0e0dfdae9..b3004849c7 100644 --- a/src/backend/utils/adt/array_userfuncs.c +++ b/src/backend/utils/adt/array_userfuncs.c @@ -568,9 +568,11 @@ array_agg_finalfn(PG_FUNCTION_ARGS) ArrayBuildState *state; int dims[1]; int lbs[1]; + MemoryContext aggcontext; /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + elog(ERROR, "aggregate function called in non-aggregate context"); state = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0); @@ -645,9 +647,11 @@ array_agg_array_finalfn(PG_FUNCTION_ARGS) { Datum result; ArrayBuildStateArr *state; + MemoryContext aggcontext; /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + elog(ERROR, "aggregate function called in non-aggregate context"); state = PG_ARGISNULL(0) ? NULL : (ArrayBuildStateArr *) PG_GETARG_POINTER(0); diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index 5c410c9e91..b89e8a183c 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -2004,10 +2004,6 @@ json_agg_collectfn(PG_FUNCTION_ARGS) elog(ERROR, "json_agg_collectfn called in non-aggregate context"); } - - /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); - if (PG_ARGISNULL(0)) { /* @@ -2057,9 +2053,11 @@ Datum json_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; + MemoryContext aggcontext; /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + elog(ERROR, "aggregate function called in non-aggregate context"); state = PG_ARGISNULL(0) ? NULL : @@ -2173,9 +2171,11 @@ Datum json_object_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; + MemoryContext aggcontext; /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + elog(ERROR, "aggregate function called in non-aggregate context"); state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0); diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c index 14ce57e252..1166b9454d 100644 --- a/src/backend/utils/adt/jsonb.c +++ b/src/backend/utils/adt/jsonb.c @@ -1682,9 +1682,11 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS) JsonbAggState *arg; JsonbInState result; Jsonb *out; + MemoryContext aggcontext; /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + elog(ERROR, "jsonb_agg_finalfn called in non-aggregate context"); if (PG_ARGISNULL(0)) PG_RETURN_NULL(); /* returns null iff no input values */ @@ -1913,9 +1915,11 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS) JsonbAggState *arg; JsonbInState result; Jsonb *out; + MemoryContext aggcontext; /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + elog(ERROR, "jsonb_object_agg_finalfn called in non-aggregate context"); if (PG_ARGISNULL(0)) PG_RETURN_NULL(); /* returns null iff no input values */ diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4971a26564..2e0d4f74cd 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -473,9 +473,14 @@ Datum bytea_string_agg_finalfn(PG_FUNCTION_ARGS) { StringInfo state; + MemoryContext aggcontext; /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + { + /* cannot be called directly because of internal-type argument */ + elog(ERROR, "bytea_string_agg_finalfn called in non-aggregate context"); + } state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0); @@ -4348,9 +4353,13 @@ Datum string_agg_finalfn(PG_FUNCTION_ARGS) { StringInfo state; + MemoryContext aggcontext; - /* cannot be called directly because of internal-type argument */ - Assert(AggCheckCallContext(fcinfo, NULL)); + if (!AggCheckCallContext(fcinfo, &aggcontext)) + { + /* cannot be called directly because of internal-type argument */ + elog(ERROR, "string_agg_finalfn called in non-aggregate context"); + } state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0); diff --git a/src/include/utils/json.h b/src/include/utils/json.h index 11960bbf2c..6743beb219 100644 --- a/src/include/utils/json.h +++ b/src/include/utils/json.h @@ -33,6 +33,9 @@ extern Datum row_to_json_pretty(PG_FUNCTION_ARGS); extern Datum to_json(PG_FUNCTION_ARGS); extern Datum json_agg_transfn(PG_FUNCTION_ARGS); +#ifdef XCP +extern Datum json_agg_collectfn(PG_FUNCTION_ARGS); +#endif extern Datum json_agg_finalfn(PG_FUNCTION_ARGS); extern Datum json_object_agg_finalfn(PG_FUNCTION_ARGS);