Change several asserts checking for aggregate context to errors
authorPavan Deolasee <[email protected]>
Mon, 11 Jul 2016 08:29:50 +0000 (13:59 +0530)
committerPavan Deolasee <[email protected]>
Mon, 11 Jul 2016 08:29:50 +0000 (13:59 +0530)
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.

src/backend/utils/adt/array_userfuncs.c
src/backend/utils/adt/json.c
src/backend/utils/adt/jsonb.c
src/backend/utils/adt/varlena.c
src/include/utils/json.h

index d0e0dfdae9ea0fe9124132e5d9d9dd211beebbda..b3004849c7759d88be351ca1b5ebf60707ab58a0 100644 (file)
@@ -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);
 
index 5c410c9e91f190ad9e5358003dae3f4fcc24264b..b89e8a183cbb9e4936c112c2800b7e2649122f60 100644 (file)
@@ -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);
 
index 14ce57e252d1a1ce2329972f6be7fa638ed5c66b..1166b9454d7b37a3f69c167d5f615f0d7ed30520 100644 (file)
@@ -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 */
index 4971a26564e6e49e0e5ac478451b48cd25838c8b..2e0d4f74cdb550c6bc9affc7f6d7e7bba51a0a98 100644 (file)
@@ -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);
 
index 11960bbf2c6bc8351d76f048d2005a9b7c76dad3..6743beb219bc22bd191e050f18e9778b30712709 100644 (file)
@@ -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);