idx
int64
func_before
string
Vulnerability Classification
string
vul
int64
func_after
string
patch
string
CWE ID
string
lines_before
string
lines_after
string
2,000
array_to_json(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); StringInfo result; result = makeStringInfo(); array_to_json_internal(array, result, false); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
DoS Overflow
0
array_to_json(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); StringInfo result; result = makeStringInfo(); array_to_json_internal(array, result, false); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,001
array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds) { ArrayType *v = DatumGetArrayTypeP(array); Oid element_type = ARR_ELEMTYPE(v); int *dim; int ndim; int nitems; int count = 0; Datum *elements; bool *nulls; int16 typlen; bool typbyval; char typalign; JsonTypeCategory tcategory; Oid outfuncoid; ndim = ARR_NDIM(v); dim = ARR_DIMS(v); nitems = ArrayGetNItems(ndim, dim); if (nitems <= 0) { appendStringInfoString(result, "[]"); return; } get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); json_categorize_type(element_type, &tcategory, &outfuncoid); deconstruct_array(v, element_type, typlen, typbyval, typalign, &elements, &nulls, &nitems); array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory, outfuncoid, use_line_feeds); pfree(elements); pfree(nulls); }
DoS Overflow
0
array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds) { ArrayType *v = DatumGetArrayTypeP(array); Oid element_type = ARR_ELEMTYPE(v); int *dim; int ndim; int nitems; int count = 0; Datum *elements; bool *nulls; int16 typlen; bool typbyval; char typalign; JsonTypeCategory tcategory; Oid outfuncoid; ndim = ARR_NDIM(v); dim = ARR_DIMS(v); nitems = ArrayGetNItems(ndim, dim); if (nitems <= 0) { appendStringInfoString(result, "[]"); return; } get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); json_categorize_type(element_type, &tcategory, &outfuncoid); deconstruct_array(v, element_type, typlen, typbyval, typalign, &elements, &nulls, &nitems); array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory, outfuncoid, use_line_feeds); pfree(elements); pfree(nulls); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,002
array_to_json_pretty(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); bool use_line_feeds = PG_GETARG_BOOL(1); StringInfo result; result = makeStringInfo(); array_to_json_internal(array, result, use_line_feeds); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
DoS Overflow
0
array_to_json_pretty(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); bool use_line_feeds = PG_GETARG_BOOL(1); StringInfo result; result = makeStringInfo(); array_to_json_internal(array, result, use_line_feeds); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,003
catenate_stringinfo_string(StringInfo buffer, const char *addon) { /* custom version of cstring_to_text_with_len */ int buflen = buffer->len; int addlen = strlen(addon); text *result = (text *) palloc(buflen + addlen + VARHDRSZ); SET_VARSIZE(result, buflen + addlen + VARHDRSZ); memcpy(VARDATA(result), buffer->data, buflen); memcpy(VARDATA(result) + buflen, addon, addlen); return result; }
DoS Overflow
0
catenate_stringinfo_string(StringInfo buffer, const char *addon) { /* custom version of cstring_to_text_with_len */ int buflen = buffer->len; int addlen = strlen(addon); text *result = (text *) palloc(buflen + addlen + VARHDRSZ); SET_VARSIZE(result, buflen + addlen + VARHDRSZ); memcpy(VARDATA(result), buffer->data, buflen); memcpy(VARDATA(result) + buflen, addon, addlen); return result; }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,004
composite_to_json(Datum composite, StringInfo result, bool use_line_feeds) { HeapTupleHeader td; Oid tupType; int32 tupTypmod; TupleDesc tupdesc; HeapTupleData tmptup, *tuple; int i; bool needsep = false; const char *sep; sep = use_line_feeds ? ",\n " : ","; td = DatumGetHeapTupleHeader(composite); /* Extract rowtype info and find a tupdesc */ tupType = HeapTupleHeaderGetTypeId(td); tupTypmod = HeapTupleHeaderGetTypMod(td); tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod); /* Build a temporary HeapTuple control structure */ tmptup.t_len = HeapTupleHeaderGetDatumLength(td); tmptup.t_data = td; tuple = &tmptup; appendStringInfoChar(result, '{'); for (i = 0; i < tupdesc->natts; i++) { Datum val; bool isnull; char *attname; JsonTypeCategory tcategory; Oid outfuncoid; if (tupdesc->attrs[i]->attisdropped) continue; if (needsep) appendStringInfoString(result, sep); needsep = true; attname = NameStr(tupdesc->attrs[i]->attname); escape_json(result, attname); appendStringInfoChar(result, ':'); val = heap_getattr(tuple, i + 1, tupdesc, &isnull); if (isnull) { tcategory = JSONTYPE_NULL; outfuncoid = InvalidOid; } else json_categorize_type(tupdesc->attrs[i]->atttypid, &tcategory, &outfuncoid); datum_to_json(val, isnull, result, tcategory, outfuncoid, false); } appendStringInfoChar(result, '}'); ReleaseTupleDesc(tupdesc); }
DoS Overflow
0
composite_to_json(Datum composite, StringInfo result, bool use_line_feeds) { HeapTupleHeader td; Oid tupType; int32 tupTypmod; TupleDesc tupdesc; HeapTupleData tmptup, *tuple; int i; bool needsep = false; const char *sep; sep = use_line_feeds ? ",\n " : ","; td = DatumGetHeapTupleHeader(composite); /* Extract rowtype info and find a tupdesc */ tupType = HeapTupleHeaderGetTypeId(td); tupTypmod = HeapTupleHeaderGetTypMod(td); tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod); /* Build a temporary HeapTuple control structure */ tmptup.t_len = HeapTupleHeaderGetDatumLength(td); tmptup.t_data = td; tuple = &tmptup; appendStringInfoChar(result, '{'); for (i = 0; i < tupdesc->natts; i++) { Datum val; bool isnull; char *attname; JsonTypeCategory tcategory; Oid outfuncoid; if (tupdesc->attrs[i]->attisdropped) continue; if (needsep) appendStringInfoString(result, sep); needsep = true; attname = NameStr(tupdesc->attrs[i]->attname); escape_json(result, attname); appendStringInfoChar(result, ':'); val = heap_getattr(tuple, i + 1, tupdesc, &isnull); if (isnull) { tcategory = JSONTYPE_NULL; outfuncoid = InvalidOid; } else json_categorize_type(tupdesc->attrs[i]->atttypid, &tcategory, &outfuncoid); datum_to_json(val, isnull, result, tcategory, outfuncoid, false); } appendStringInfoChar(result, '}'); ReleaseTupleDesc(tupdesc); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,005
escape_json(StringInfo buf, const char *str) { const char *p; appendStringInfoCharMacro(buf, '\"'); for (p = str; *p; p++) { switch (*p) { case '\b': appendStringInfoString(buf, "\\b"); break; case '\f': appendStringInfoString(buf, "\\f"); break; case '\n': appendStringInfoString(buf, "\\n"); break; case '\r': appendStringInfoString(buf, "\\r"); break; case '\t': appendStringInfoString(buf, "\\t"); break; case '"': appendStringInfoString(buf, "\\\""); break; case '\\': appendStringInfoString(buf, "\\\\"); break; default: if ((unsigned char) *p < ' ') appendStringInfo(buf, "\\u%04x", (int) *p); else appendStringInfoCharMacro(buf, *p); break; } } appendStringInfoCharMacro(buf, '\"'); }
DoS Overflow
0
escape_json(StringInfo buf, const char *str) { const char *p; appendStringInfoCharMacro(buf, '\"'); for (p = str; *p; p++) { switch (*p) { case '\b': appendStringInfoString(buf, "\\b"); break; case '\f': appendStringInfoString(buf, "\\f"); break; case '\n': appendStringInfoString(buf, "\\n"); break; case '\r': appendStringInfoString(buf, "\\r"); break; case '\t': appendStringInfoString(buf, "\\t"); break; case '"': appendStringInfoString(buf, "\\\""); break; case '\\': appendStringInfoString(buf, "\\\\"); break; default: if ((unsigned char) *p < ' ') appendStringInfo(buf, "\\u%04x", (int) *p); else appendStringInfoCharMacro(buf, *p); break; } } appendStringInfoCharMacro(buf, '\"'); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,006
extract_mb_char(char *s) { char *res; int len; len = pg_mblen(s); res = palloc(len + 1); memcpy(res, s, len); res[len] = '\0'; return res; }
DoS Overflow
0
extract_mb_char(char *s) { char *res; int len; len = pg_mblen(s); res = palloc(len + 1); memcpy(res, s, len); res[len] = '\0'; return res; }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,007
json_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; /* cannot be called directly because of internal-type argument */ Assert(AggCheckCallContext(fcinfo, NULL)); state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0); /* NULL result for no rows in, as is standard with aggregates */ if (state == NULL) PG_RETURN_NULL(); /* Else return state with appropriate array terminator added */ PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, "]")); }
DoS Overflow
0
json_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; /* cannot be called directly because of internal-type argument */ Assert(AggCheckCallContext(fcinfo, NULL)); state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0); /* NULL result for no rows in, as is standard with aggregates */ if (state == NULL) PG_RETURN_NULL(); /* Else return state with appropriate array terminator added */ PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, "]")); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,008
json_build_array_noargs(PG_FUNCTION_ARGS) { PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2)); }
DoS Overflow
0
json_build_array_noargs(PG_FUNCTION_ARGS) { PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,009
json_build_object(PG_FUNCTION_ARGS) { int nargs = PG_NARGS(); int i; Datum arg; const char *sep = ""; StringInfo result; Oid val_type; if (nargs % 2 != 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("argument list must have even number of elements"), errhint("The arguments of json_build_object() must consist of alternating keys and values."))); result = makeStringInfo(); appendStringInfoChar(result, '{'); for (i = 0; i < nargs; i += 2) { /* * Note: since json_build_object() is declared as taking type "any", * the parser will not do any type conversion on unknown-type literals * (that is, undecorated strings or NULLs). Such values will arrive * here as type UNKNOWN, which fortunately does not matter to us, * since unknownout() works fine. */ appendStringInfoString(result, sep); sep = ", "; /* process key */ val_type = get_fn_expr_argtype(fcinfo->flinfo, i); if (val_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument %d", i + 1))); if (PG_ARGISNULL(i)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("argument %d cannot be null", i + 1), errhint("Object keys should be text."))); arg = PG_GETARG_DATUM(i); add_json(arg, false, result, val_type, true); appendStringInfoString(result, " : "); /* process value */ val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1); if (val_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument %d", i + 2))); if (PG_ARGISNULL(i + 1)) arg = (Datum) 0; else arg = PG_GETARG_DATUM(i + 1); add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false); } appendStringInfoChar(result, '}'); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
DoS Overflow
0
json_build_object(PG_FUNCTION_ARGS) { int nargs = PG_NARGS(); int i; Datum arg; const char *sep = ""; StringInfo result; Oid val_type; if (nargs % 2 != 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("argument list must have even number of elements"), errhint("The arguments of json_build_object() must consist of alternating keys and values."))); result = makeStringInfo(); appendStringInfoChar(result, '{'); for (i = 0; i < nargs; i += 2) { /* * Note: since json_build_object() is declared as taking type "any", * the parser will not do any type conversion on unknown-type literals * (that is, undecorated strings or NULLs). Such values will arrive * here as type UNKNOWN, which fortunately does not matter to us, * since unknownout() works fine. */ appendStringInfoString(result, sep); sep = ", "; /* process key */ val_type = get_fn_expr_argtype(fcinfo->flinfo, i); if (val_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument %d", i + 1))); if (PG_ARGISNULL(i)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("argument %d cannot be null", i + 1), errhint("Object keys should be text."))); arg = PG_GETARG_DATUM(i); add_json(arg, false, result, val_type, true); appendStringInfoString(result, " : "); /* process value */ val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1); if (val_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument %d", i + 2))); if (PG_ARGISNULL(i + 1)) arg = (Datum) 0; else arg = PG_GETARG_DATUM(i + 1); add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false); } appendStringInfoChar(result, '}'); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,010
json_build_object_noargs(PG_FUNCTION_ARGS) { PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2)); }
DoS Overflow
0
json_build_object_noargs(PG_FUNCTION_ARGS) { PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,011
json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid) { bool typisvarlena; /* Look through any domain */ typoid = getBaseType(typoid); *outfuncoid = InvalidOid; /* * We need to get the output function for everything except date and * timestamp types, array and composite types, booleans, and non-builtin * types where there's a cast to json. */ switch (typoid) { case BOOLOID: *tcategory = JSONTYPE_BOOL; break; case INT2OID: case INT4OID: case INT8OID: case FLOAT4OID: case FLOAT8OID: case NUMERICOID: getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); *tcategory = JSONTYPE_NUMERIC; break; case DATEOID: *tcategory = JSONTYPE_DATE; break; case TIMESTAMPOID: *tcategory = JSONTYPE_TIMESTAMP; break; case TIMESTAMPTZOID: *tcategory = JSONTYPE_TIMESTAMPTZ; break; case JSONOID: case JSONBOID: getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); *tcategory = JSONTYPE_JSON; break; default: /* Check for arrays and composites */ if (OidIsValid(get_element_type(typoid))) *tcategory = JSONTYPE_ARRAY; else if (type_is_rowtype(typoid)) *tcategory = JSONTYPE_COMPOSITE; else { /* It's probably the general case ... */ *tcategory = JSONTYPE_OTHER; /* but let's look for a cast to json, if it's not built-in */ if (typoid >= FirstNormalObjectId) { Oid castfunc; CoercionPathType ctype; ctype = find_coercion_pathway(JSONOID, typoid, COERCION_EXPLICIT, &castfunc); if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc)) { *tcategory = JSONTYPE_CAST; *outfuncoid = castfunc; } else { /* non builtin type with no cast */ getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); } } else { /* any other builtin type */ getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); } } break; } }
DoS Overflow
0
json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid) { bool typisvarlena; /* Look through any domain */ typoid = getBaseType(typoid); *outfuncoid = InvalidOid; /* * We need to get the output function for everything except date and * timestamp types, array and composite types, booleans, and non-builtin * types where there's a cast to json. */ switch (typoid) { case BOOLOID: *tcategory = JSONTYPE_BOOL; break; case INT2OID: case INT4OID: case INT8OID: case FLOAT4OID: case FLOAT8OID: case NUMERICOID: getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); *tcategory = JSONTYPE_NUMERIC; break; case DATEOID: *tcategory = JSONTYPE_DATE; break; case TIMESTAMPOID: *tcategory = JSONTYPE_TIMESTAMP; break; case TIMESTAMPTZOID: *tcategory = JSONTYPE_TIMESTAMPTZ; break; case JSONOID: case JSONBOID: getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); *tcategory = JSONTYPE_JSON; break; default: /* Check for arrays and composites */ if (OidIsValid(get_element_type(typoid))) *tcategory = JSONTYPE_ARRAY; else if (type_is_rowtype(typoid)) *tcategory = JSONTYPE_COMPOSITE; else { /* It's probably the general case ... */ *tcategory = JSONTYPE_OTHER; /* but let's look for a cast to json, if it's not built-in */ if (typoid >= FirstNormalObjectId) { Oid castfunc; CoercionPathType ctype; ctype = find_coercion_pathway(JSONOID, typoid, COERCION_EXPLICIT, &castfunc); if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc)) { *tcategory = JSONTYPE_CAST; *outfuncoid = castfunc; } else { /* non builtin type with no cast */ getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); } } else { /* any other builtin type */ getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); } } break; } }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,012
json_in(PG_FUNCTION_ARGS) { char *json = PG_GETARG_CSTRING(0); text *result = cstring_to_text(json); JsonLexContext *lex; /* validate it */ lex = makeJsonLexContext(result, false); pg_parse_json(lex, &nullSemAction); /* Internal representation is the same as text, for now */ PG_RETURN_TEXT_P(result); }
DoS Overflow
0
json_in(PG_FUNCTION_ARGS) { char *json = PG_GETARG_CSTRING(0); text *result = cstring_to_text(json); JsonLexContext *lex; /* validate it */ lex = makeJsonLexContext(result, false); pg_parse_json(lex, &nullSemAction); /* Internal representation is the same as text, for now */ PG_RETURN_TEXT_P(result); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,013
json_lex(JsonLexContext *lex) { char *s; int len; /* Skip leading whitespace. */ s = lex->token_terminator; len = s - lex->input; while (len < lex->input_length && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { if (*s == '\n') ++lex->line_number; ++s; ++len; } lex->token_start = s; /* Determine token type. */ if (len >= lex->input_length) { lex->token_start = NULL; lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s; lex->token_type = JSON_TOKEN_END; } else switch (*s) { /* Single-character token, some kind of punctuation mark. */ case '{': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_OBJECT_START; break; case '}': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_OBJECT_END; break; case '[': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_ARRAY_START; break; case ']': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_ARRAY_END; break; case ',': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_COMMA; break; case ':': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_COLON; break; case '"': /* string */ json_lex_string(lex); lex->token_type = JSON_TOKEN_STRING; break; case '-': /* Negative number. */ json_lex_number(lex, s + 1, NULL); lex->token_type = JSON_TOKEN_NUMBER; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* Positive number. */ json_lex_number(lex, s, NULL); lex->token_type = JSON_TOKEN_NUMBER; break; default: { char *p; /* * We're not dealing with a string, number, legal * punctuation mark, or end of string. The only legal * tokens we might find here are true, false, and null, * but for error reporting purposes we scan until we see a * non-alphanumeric character. That way, we can report * the whole word as an unexpected token, rather than just * some unintuitive prefix thereof. */ for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++) /* skip */ ; /* * We got some sort of unexpected punctuation or an * otherwise unexpected character, so just complain about * that one character. */ if (p == s) { lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; report_invalid_token(lex); } /* * We've got a real alphanumeric token here. If it * happens to be true, false, or null, all is well. If * not, error out. */ lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = p; if (p - s == 4) { if (memcmp(s, "true", 4) == 0) lex->token_type = JSON_TOKEN_TRUE; else if (memcmp(s, "null", 4) == 0) lex->token_type = JSON_TOKEN_NULL; else report_invalid_token(lex); } else if (p - s == 5 && memcmp(s, "false", 5) == 0) lex->token_type = JSON_TOKEN_FALSE; else report_invalid_token(lex); } } /* end of switch */ }
DoS Overflow
0
json_lex(JsonLexContext *lex) { char *s; int len; /* Skip leading whitespace. */ s = lex->token_terminator; len = s - lex->input; while (len < lex->input_length && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { if (*s == '\n') ++lex->line_number; ++s; ++len; } lex->token_start = s; /* Determine token type. */ if (len >= lex->input_length) { lex->token_start = NULL; lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s; lex->token_type = JSON_TOKEN_END; } else switch (*s) { /* Single-character token, some kind of punctuation mark. */ case '{': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_OBJECT_START; break; case '}': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_OBJECT_END; break; case '[': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_ARRAY_START; break; case ']': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_ARRAY_END; break; case ',': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_COMMA; break; case ':': lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; lex->token_type = JSON_TOKEN_COLON; break; case '"': /* string */ json_lex_string(lex); lex->token_type = JSON_TOKEN_STRING; break; case '-': /* Negative number. */ json_lex_number(lex, s + 1, NULL); lex->token_type = JSON_TOKEN_NUMBER; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* Positive number. */ json_lex_number(lex, s, NULL); lex->token_type = JSON_TOKEN_NUMBER; break; default: { char *p; /* * We're not dealing with a string, number, legal * punctuation mark, or end of string. The only legal * tokens we might find here are true, false, and null, * but for error reporting purposes we scan until we see a * non-alphanumeric character. That way, we can report * the whole word as an unexpected token, rather than just * some unintuitive prefix thereof. */ for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++) /* skip */ ; /* * We got some sort of unexpected punctuation or an * otherwise unexpected character, so just complain about * that one character. */ if (p == s) { lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; report_invalid_token(lex); } /* * We've got a real alphanumeric token here. If it * happens to be true, false, or null, all is well. If * not, error out. */ lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = p; if (p - s == 4) { if (memcmp(s, "true", 4) == 0) lex->token_type = JSON_TOKEN_TRUE; else if (memcmp(s, "null", 4) == 0) lex->token_type = JSON_TOKEN_NULL; else report_invalid_token(lex); } else if (p - s == 5 && memcmp(s, "false", 5) == 0) lex->token_type = JSON_TOKEN_FALSE; else report_invalid_token(lex); } } /* end of switch */ }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,014
json_lex_number(JsonLexContext *lex, char *s, bool *num_err) { bool error = false; char *p; int len; len = s - lex->input; /* Part (1): leading sign indicator. */ /* Caller already did this for us; so do nothing. */ /* Part (2): parse main digit string. */ if (*s == '0') { s++; len++; } else if (*s >= '1' && *s <= '9') { do { s++; len++; } while (len < lex->input_length && *s >= '0' && *s <= '9'); } else error = true; /* Part (3): parse optional decimal portion. */ if (len < lex->input_length && *s == '.') { s++; len++; if (len == lex->input_length || *s < '0' || *s > '9') error = true; else { do { s++; len++; } while (len < lex->input_length && *s >= '0' && *s <= '9'); } } /* Part (4): parse optional exponent. */ if (len < lex->input_length && (*s == 'e' || *s == 'E')) { s++; len++; if (len < lex->input_length && (*s == '+' || *s == '-')) { s++; len++; } if (len == lex->input_length || *s < '0' || *s > '9') error = true; else { do { s++; len++; } while (len < lex->input_length && *s >= '0' && *s <= '9'); } } /* * Check for trailing garbage. As in json_lex(), any alphanumeric stuff * here should be considered part of the token for error-reporting * purposes. */ for (p = s; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*p); p++, len++) error = true; if (num_err != NULL) { /* let the caller handle the error */ *num_err = error; } else { lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = p; if (error) report_invalid_token(lex); } }
DoS Overflow
0
json_lex_number(JsonLexContext *lex, char *s, bool *num_err) { bool error = false; char *p; int len; len = s - lex->input; /* Part (1): leading sign indicator. */ /* Caller already did this for us; so do nothing. */ /* Part (2): parse main digit string. */ if (*s == '0') { s++; len++; } else if (*s >= '1' && *s <= '9') { do { s++; len++; } while (len < lex->input_length && *s >= '0' && *s <= '9'); } else error = true; /* Part (3): parse optional decimal portion. */ if (len < lex->input_length && *s == '.') { s++; len++; if (len == lex->input_length || *s < '0' || *s > '9') error = true; else { do { s++; len++; } while (len < lex->input_length && *s >= '0' && *s <= '9'); } } /* Part (4): parse optional exponent. */ if (len < lex->input_length && (*s == 'e' || *s == 'E')) { s++; len++; if (len < lex->input_length && (*s == '+' || *s == '-')) { s++; len++; } if (len == lex->input_length || *s < '0' || *s > '9') error = true; else { do { s++; len++; } while (len < lex->input_length && *s >= '0' && *s <= '9'); } } /* * Check for trailing garbage. As in json_lex(), any alphanumeric stuff * here should be considered part of the token for error-reporting * purposes. */ for (p = s; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*p); p++, len++) error = true; if (num_err != NULL) { /* let the caller handle the error */ *num_err = error; } else { lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = p; if (error) report_invalid_token(lex); } }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,015
json_lex_string(JsonLexContext *lex) { char *s; int len; int hi_surrogate = -1; if (lex->strval != NULL) resetStringInfo(lex->strval); Assert(lex->input_length > 0); s = lex->token_start; len = lex->token_start - lex->input; for (;;) { s++; len++; /* Premature end of the string. */ if (len >= lex->input_length) { lex->token_terminator = s; report_invalid_token(lex); } else if (*s == '"') break; else if ((unsigned char) *s < 32) { /* Per RFC4627, these characters MUST be escaped. */ /* Since *s isn't printable, exclude it from the context string */ lex->token_terminator = s; ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Character with value 0x%02x must be escaped.", (unsigned char) *s), report_json_context(lex))); } else if (*s == '\\') { /* OK, we have an escape character. */ s++; len++; if (len >= lex->input_length) { lex->token_terminator = s; report_invalid_token(lex); } else if (*s == 'u') { int i; int ch = 0; for (i = 1; i <= 4; i++) { s++; len++; if (len >= lex->input_length) { lex->token_terminator = s; report_invalid_token(lex); } else if (*s >= '0' && *s <= '9') ch = (ch * 16) + (*s - '0'); else if (*s >= 'a' && *s <= 'f') ch = (ch * 16) + (*s - 'a') + 10; else if (*s >= 'A' && *s <= 'F') ch = (ch * 16) + (*s - 'A') + 10; else { lex->token_terminator = s + pg_mblen(s); ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("\"\\u\" must be followed by four hexadecimal digits."), report_json_context(lex))); } } if (lex->strval != NULL) { char utf8str[5]; int utf8len; if (ch >= 0xd800 && ch <= 0xdbff) { if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode high surrogate must not follow a high surrogate."), report_json_context(lex))); hi_surrogate = (ch & 0x3ff) << 10; continue; } else if (ch >= 0xdc00 && ch <= 0xdfff) { if (hi_surrogate == -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); ch = 0x10000 + hi_surrogate + (ch & 0x3ff); hi_surrogate = -1; } if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); /* * For UTF8, replace the escape sequence by the actual * utf8 character in lex->strval. Do this also for other * encodings if the escape designates an ASCII character, * otherwise raise an error. */ if (ch == 0) { /* We can't allow this, since our TEXT type doesn't */ ereport(ERROR, (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER), errmsg("unsupported Unicode escape sequence"), errdetail("\\u0000 cannot be converted to text."), report_json_context(lex))); } else if (GetDatabaseEncoding() == PG_UTF8) { unicode_to_utf8(ch, (unsigned char *) utf8str); utf8len = pg_utf_mblen((unsigned char *) utf8str); appendBinaryStringInfo(lex->strval, utf8str, utf8len); } else if (ch <= 0x007f) { /* * This is the only way to designate things like a * form feed character in JSON, so it's useful in all * encodings. */ appendStringInfoChar(lex->strval, (char) ch); } else { ereport(ERROR, (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER), errmsg("unsupported Unicode escape sequence"), errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."), report_json_context(lex))); } } } else if (lex->strval != NULL) { if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); switch (*s) { case '"': case '\\': case '/': appendStringInfoChar(lex->strval, *s); break; case 'b': appendStringInfoChar(lex->strval, '\b'); break; case 'f': appendStringInfoChar(lex->strval, '\f'); break; case 'n': appendStringInfoChar(lex->strval, '\n'); break; case 'r': appendStringInfoChar(lex->strval, '\r'); break; case 't': appendStringInfoChar(lex->strval, '\t'); break; default: /* Not a valid string escape, so error out. */ lex->token_terminator = s + pg_mblen(s); ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Escape sequence \"\\%s\" is invalid.", extract_mb_char(s)), report_json_context(lex))); } } else if (strchr("\"\\/bfnrt", *s) == NULL) { /* * Simpler processing if we're not bothered about de-escaping * * It's very tempting to remove the strchr() call here and * replace it with a switch statement, but testing so far has * shown it's not a performance win. */ lex->token_terminator = s + pg_mblen(s); ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Escape sequence \"\\%s\" is invalid.", extract_mb_char(s)), report_json_context(lex))); } } else if (lex->strval != NULL) { if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); appendStringInfoChar(lex->strval, *s); } } if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); /* Hooray, we found the end of the string! */ lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; }
DoS Overflow
0
json_lex_string(JsonLexContext *lex) { char *s; int len; int hi_surrogate = -1; if (lex->strval != NULL) resetStringInfo(lex->strval); Assert(lex->input_length > 0); s = lex->token_start; len = lex->token_start - lex->input; for (;;) { s++; len++; /* Premature end of the string. */ if (len >= lex->input_length) { lex->token_terminator = s; report_invalid_token(lex); } else if (*s == '"') break; else if ((unsigned char) *s < 32) { /* Per RFC4627, these characters MUST be escaped. */ /* Since *s isn't printable, exclude it from the context string */ lex->token_terminator = s; ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Character with value 0x%02x must be escaped.", (unsigned char) *s), report_json_context(lex))); } else if (*s == '\\') { /* OK, we have an escape character. */ s++; len++; if (len >= lex->input_length) { lex->token_terminator = s; report_invalid_token(lex); } else if (*s == 'u') { int i; int ch = 0; for (i = 1; i <= 4; i++) { s++; len++; if (len >= lex->input_length) { lex->token_terminator = s; report_invalid_token(lex); } else if (*s >= '0' && *s <= '9') ch = (ch * 16) + (*s - '0'); else if (*s >= 'a' && *s <= 'f') ch = (ch * 16) + (*s - 'a') + 10; else if (*s >= 'A' && *s <= 'F') ch = (ch * 16) + (*s - 'A') + 10; else { lex->token_terminator = s + pg_mblen(s); ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("\"\\u\" must be followed by four hexadecimal digits."), report_json_context(lex))); } } if (lex->strval != NULL) { char utf8str[5]; int utf8len; if (ch >= 0xd800 && ch <= 0xdbff) { if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode high surrogate must not follow a high surrogate."), report_json_context(lex))); hi_surrogate = (ch & 0x3ff) << 10; continue; } else if (ch >= 0xdc00 && ch <= 0xdfff) { if (hi_surrogate == -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); ch = 0x10000 + hi_surrogate + (ch & 0x3ff); hi_surrogate = -1; } if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); /* * For UTF8, replace the escape sequence by the actual * utf8 character in lex->strval. Do this also for other * encodings if the escape designates an ASCII character, * otherwise raise an error. */ if (ch == 0) { /* We can't allow this, since our TEXT type doesn't */ ereport(ERROR, (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER), errmsg("unsupported Unicode escape sequence"), errdetail("\\u0000 cannot be converted to text."), report_json_context(lex))); } else if (GetDatabaseEncoding() == PG_UTF8) { unicode_to_utf8(ch, (unsigned char *) utf8str); utf8len = pg_utf_mblen((unsigned char *) utf8str); appendBinaryStringInfo(lex->strval, utf8str, utf8len); } else if (ch <= 0x007f) { /* * This is the only way to designate things like a * form feed character in JSON, so it's useful in all * encodings. */ appendStringInfoChar(lex->strval, (char) ch); } else { ereport(ERROR, (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER), errmsg("unsupported Unicode escape sequence"), errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."), report_json_context(lex))); } } } else if (lex->strval != NULL) { if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); switch (*s) { case '"': case '\\': case '/': appendStringInfoChar(lex->strval, *s); break; case 'b': appendStringInfoChar(lex->strval, '\b'); break; case 'f': appendStringInfoChar(lex->strval, '\f'); break; case 'n': appendStringInfoChar(lex->strval, '\n'); break; case 'r': appendStringInfoChar(lex->strval, '\r'); break; case 't': appendStringInfoChar(lex->strval, '\t'); break; default: /* Not a valid string escape, so error out. */ lex->token_terminator = s + pg_mblen(s); ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Escape sequence \"\\%s\" is invalid.", extract_mb_char(s)), report_json_context(lex))); } } else if (strchr("\"\\/bfnrt", *s) == NULL) { /* * Simpler processing if we're not bothered about de-escaping * * It's very tempting to remove the strchr() call here and * replace it with a switch statement, but testing so far has * shown it's not a performance win. */ lex->token_terminator = s + pg_mblen(s); ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Escape sequence \"\\%s\" is invalid.", extract_mb_char(s)), report_json_context(lex))); } } else if (lex->strval != NULL) { if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); appendStringInfoChar(lex->strval, *s); } } if (hi_surrogate != -1) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Unicode low surrogate must follow a high surrogate."), report_json_context(lex))); /* Hooray, we found the end of the string! */ lex->prev_token_terminator = lex->token_terminator; lex->token_terminator = s + 1; }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,016
json_object(PG_FUNCTION_ARGS) { ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0); int ndims = ARR_NDIM(in_array); StringInfoData result; Datum *in_datums; bool *in_nulls; int in_count, count, i; text *rval; char *v; switch (ndims) { case 0: PG_RETURN_DATUM(CStringGetTextDatum("{}")); break; case 1: if ((ARR_DIMS(in_array)[0]) % 2) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must have even number of elements"))); break; case 2: if ((ARR_DIMS(in_array)[1]) != 2) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must have two columns"))); break; default: ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); } deconstruct_array(in_array, TEXTOID, -1, false, 'i', &in_datums, &in_nulls, &in_count); count = in_count / 2; initStringInfo(&result); appendStringInfoChar(&result, '{'); for (i = 0; i < count; ++i) { if (in_nulls[i * 2]) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("null value not allowed for object key"))); v = TextDatumGetCString(in_datums[i * 2]); if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); appendStringInfoString(&result, " : "); pfree(v); if (in_nulls[i * 2 + 1]) appendStringInfoString(&result, "null"); else { v = TextDatumGetCString(in_datums[i * 2 + 1]); escape_json(&result, v); pfree(v); } } appendStringInfoChar(&result, '}'); pfree(in_datums); pfree(in_nulls); rval = cstring_to_text_with_len(result.data, result.len); pfree(result.data); PG_RETURN_TEXT_P(rval); }
DoS Overflow
0
json_object(PG_FUNCTION_ARGS) { ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0); int ndims = ARR_NDIM(in_array); StringInfoData result; Datum *in_datums; bool *in_nulls; int in_count, count, i; text *rval; char *v; switch (ndims) { case 0: PG_RETURN_DATUM(CStringGetTextDatum("{}")); break; case 1: if ((ARR_DIMS(in_array)[0]) % 2) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must have even number of elements"))); break; case 2: if ((ARR_DIMS(in_array)[1]) != 2) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array must have two columns"))); break; default: ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); } deconstruct_array(in_array, TEXTOID, -1, false, 'i', &in_datums, &in_nulls, &in_count); count = in_count / 2; initStringInfo(&result); appendStringInfoChar(&result, '{'); for (i = 0; i < count; ++i) { if (in_nulls[i * 2]) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("null value not allowed for object key"))); v = TextDatumGetCString(in_datums[i * 2]); if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); appendStringInfoString(&result, " : "); pfree(v); if (in_nulls[i * 2 + 1]) appendStringInfoString(&result, "null"); else { v = TextDatumGetCString(in_datums[i * 2 + 1]); escape_json(&result, v); pfree(v); } } appendStringInfoChar(&result, '}'); pfree(in_datums); pfree(in_nulls); rval = cstring_to_text_with_len(result.data, result.len); pfree(result.data); PG_RETURN_TEXT_P(rval); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,017
json_object_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; /* cannot be called directly because of internal-type argument */ Assert(AggCheckCallContext(fcinfo, NULL)); state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0); /* NULL result for no rows in, as is standard with aggregates */ if (state == NULL) PG_RETURN_NULL(); /* Else return state with appropriate object terminator added */ PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }"));
DoS Overflow
0
json_object_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; /* cannot be called directly because of internal-type argument */ Assert(AggCheckCallContext(fcinfo, NULL)); state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0); /* NULL result for no rows in, as is standard with aggregates */ if (state == NULL) PG_RETURN_NULL(); /* Else return state with appropriate object terminator added */ PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }"));
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,018
json_object_agg_transfn(PG_FUNCTION_ARGS) { MemoryContext aggcontext, oldcontext; JsonAggState *state; Datum arg; if (!AggCheckCallContext(fcinfo, &aggcontext)) { /* cannot be called directly because of internal-type argument */ elog(ERROR, "json_object_agg_transfn called in non-aggregate context"); } if (PG_ARGISNULL(0)) { Oid arg_type; /* * Make the StringInfo in a context where it will persist for the * duration of the aggregate call. Switching context is only needed * for this initial step, as the StringInfo routines make sure they * use the right context to enlarge the object if necessary. */ oldcontext = MemoryContextSwitchTo(aggcontext); state = (JsonAggState *) palloc(sizeof(JsonAggState)); state->str = makeStringInfo(); MemoryContextSwitchTo(oldcontext); arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1); if (arg_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument 1"))); json_categorize_type(arg_type,&state->key_category, &state->key_output_func); arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2); if (arg_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument 2"))); json_categorize_type(arg_type,&state->val_category, &state->val_output_func); appendStringInfoString(state->str, "{ "); } else { state = (JsonAggState *) PG_GETARG_POINTER(0); appendStringInfoString(state->str, ", "); } /* * Note: since json_object_agg() is declared as taking type "any", the * parser will not do any type conversion on unknown-type literals (that * is, undecorated strings or NULLs). Such values will arrive here as * type UNKNOWN, which fortunately does not matter to us, since * unknownout() works fine. */ if (PG_ARGISNULL(1)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("field name must not be null"))); arg = PG_GETARG_DATUM(1); datum_to_json(arg, false, state->str, state->key_category, state->key_output_func, true); appendStringInfoString(state->str, " : "); if (PG_ARGISNULL(2)) arg = (Datum) 0; else arg = PG_GETARG_DATUM(2); datum_to_json(arg, PG_ARGISNULL(2), state->str, state->val_category, state->val_output_func, false); PG_RETURN_POINTER(state); } /* * json_object_agg final function. */ Datum json_object_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; /* cannot be called directly because of internal-type argument */ Assert(AggCheckCallContext(fcinfo, NULL)); state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0); /* NULL result for no rows in, as is standard with aggregates */ if (state == NULL) PG_RETURN_NULL(); /* Else return state with appropriate object terminator added */ PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }")); }
DoS Overflow
0
json_object_agg_transfn(PG_FUNCTION_ARGS) { MemoryContext aggcontext, oldcontext; JsonAggState *state; Datum arg; if (!AggCheckCallContext(fcinfo, &aggcontext)) { /* cannot be called directly because of internal-type argument */ elog(ERROR, "json_object_agg_transfn called in non-aggregate context"); } if (PG_ARGISNULL(0)) { Oid arg_type; /* * Make the StringInfo in a context where it will persist for the * duration of the aggregate call. Switching context is only needed * for this initial step, as the StringInfo routines make sure they * use the right context to enlarge the object if necessary. */ oldcontext = MemoryContextSwitchTo(aggcontext); state = (JsonAggState *) palloc(sizeof(JsonAggState)); state->str = makeStringInfo(); MemoryContextSwitchTo(oldcontext); arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1); if (arg_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument 1"))); json_categorize_type(arg_type,&state->key_category, &state->key_output_func); arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2); if (arg_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine data type for argument 2"))); json_categorize_type(arg_type,&state->val_category, &state->val_output_func); appendStringInfoString(state->str, "{ "); } else { state = (JsonAggState *) PG_GETARG_POINTER(0); appendStringInfoString(state->str, ", "); } /* * Note: since json_object_agg() is declared as taking type "any", the * parser will not do any type conversion on unknown-type literals (that * is, undecorated strings or NULLs). Such values will arrive here as * type UNKNOWN, which fortunately does not matter to us, since * unknownout() works fine. */ if (PG_ARGISNULL(1)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("field name must not be null"))); arg = PG_GETARG_DATUM(1); datum_to_json(arg, false, state->str, state->key_category, state->key_output_func, true); appendStringInfoString(state->str, " : "); if (PG_ARGISNULL(2)) arg = (Datum) 0; else arg = PG_GETARG_DATUM(2); datum_to_json(arg, PG_ARGISNULL(2), state->str, state->val_category, state->val_output_func, false); PG_RETURN_POINTER(state); } /* * json_object_agg final function. */ Datum json_object_agg_finalfn(PG_FUNCTION_ARGS) { JsonAggState *state; /* cannot be called directly because of internal-type argument */ Assert(AggCheckCallContext(fcinfo, NULL)); state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0); /* NULL result for no rows in, as is standard with aggregates */ if (state == NULL) PG_RETURN_NULL(); /* Else return state with appropriate object terminator added */ PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }")); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,019
json_object_two_arg(PG_FUNCTION_ARGS) { ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0); ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1); int nkdims = ARR_NDIM(key_array); int nvdims = ARR_NDIM(val_array); StringInfoData result; Datum *key_datums, *val_datums; bool *key_nulls, *val_nulls; int key_count, val_count, i; text *rval; char *v; if (nkdims > 1 || nkdims != nvdims) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); if (nkdims == 0) PG_RETURN_DATUM(CStringGetTextDatum("{}")); deconstruct_array(key_array, TEXTOID, -1, false, 'i', &key_datums, &key_nulls, &key_count); deconstruct_array(val_array, TEXTOID, -1, false, 'i', &val_datums, &val_nulls, &val_count); if (key_count != val_count) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("mismatched array dimensions"))); initStringInfo(&result); appendStringInfoChar(&result, '{'); for (i = 0; i < key_count; ++i) { if (key_nulls[i]) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("null value not allowed for object key"))); v = TextDatumGetCString(key_datums[i]); if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); appendStringInfoString(&result, " : "); pfree(v); if (val_nulls[i]) appendStringInfoString(&result, "null"); else { v = TextDatumGetCString(val_datums[i]); escape_json(&result, v); pfree(v); } } appendStringInfoChar(&result, '}'); pfree(key_datums); pfree(key_nulls); pfree(val_datums); pfree(val_nulls); rval = cstring_to_text_with_len(result.data, result.len); pfree(result.data); PG_RETURN_TEXT_P(rval); }
DoS Overflow
0
json_object_two_arg(PG_FUNCTION_ARGS) { ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0); ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1); int nkdims = ARR_NDIM(key_array); int nvdims = ARR_NDIM(val_array); StringInfoData result; Datum *key_datums, *val_datums; bool *key_nulls, *val_nulls; int key_count, val_count, i; text *rval; char *v; if (nkdims > 1 || nkdims != nvdims) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); if (nkdims == 0) PG_RETURN_DATUM(CStringGetTextDatum("{}")); deconstruct_array(key_array, TEXTOID, -1, false, 'i', &key_datums, &key_nulls, &key_count); deconstruct_array(val_array, TEXTOID, -1, false, 'i', &val_datums, &val_nulls, &val_count); if (key_count != val_count) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("mismatched array dimensions"))); initStringInfo(&result); appendStringInfoChar(&result, '{'); for (i = 0; i < key_count; ++i) { if (key_nulls[i]) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("null value not allowed for object key"))); v = TextDatumGetCString(key_datums[i]); if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); appendStringInfoString(&result, " : "); pfree(v); if (val_nulls[i]) appendStringInfoString(&result, "null"); else { v = TextDatumGetCString(val_datums[i]); escape_json(&result, v); pfree(v); } } appendStringInfoChar(&result, '}'); pfree(key_datums); pfree(key_nulls); pfree(val_datums); pfree(val_nulls); rval = cstring_to_text_with_len(result.data, result.len); pfree(result.data); PG_RETURN_TEXT_P(rval); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,020
json_out(PG_FUNCTION_ARGS) { /* we needn't detoast because text_to_cstring will handle that */ Datum txt = PG_GETARG_DATUM(0); PG_RETURN_CSTRING(TextDatumGetCString(txt)); }
DoS Overflow
0
json_out(PG_FUNCTION_ARGS) { /* we needn't detoast because text_to_cstring will handle that */ Datum txt = PG_GETARG_DATUM(0); PG_RETURN_CSTRING(TextDatumGetCString(txt)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,021
json_recv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); char *str; int nbytes; JsonLexContext *lex; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); /* Validate it. */ lex = makeJsonLexContextCstringLen(str, nbytes, false); pg_parse_json(lex, &nullSemAction); PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes)); }
DoS Overflow
0
json_recv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); char *str; int nbytes; JsonLexContext *lex; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); /* Validate it. */ lex = makeJsonLexContextCstringLen(str, nbytes, false); pg_parse_json(lex, &nullSemAction); PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,022
json_send(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); StringInfoData buf; pq_begintypsend(&buf); pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); }
DoS Overflow
0
json_send(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); StringInfoData buf; pq_begintypsend(&buf); pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,023
json_typeof(PG_FUNCTION_ARGS) { text *json; JsonLexContext *lex; JsonTokenType tok; char *type; json = PG_GETARG_TEXT_P(0); lex = makeJsonLexContext(json, false); /* Lex exactly one token from the input and check its type. */ json_lex(lex); tok = lex_peek(lex); switch (tok) { case JSON_TOKEN_OBJECT_START: type = "object"; break; case JSON_TOKEN_ARRAY_START: type = "array"; break; case JSON_TOKEN_STRING: type = "string"; break; case JSON_TOKEN_NUMBER: type = "number"; break; case JSON_TOKEN_TRUE: case JSON_TOKEN_FALSE: type = "boolean"; break; case JSON_TOKEN_NULL: type = "null"; break; default: elog(ERROR, "unexpected json token: %d", tok); } PG_RETURN_TEXT_P(cstring_to_text(type)); }
DoS Overflow
0
json_typeof(PG_FUNCTION_ARGS) { text *json; JsonLexContext *lex; JsonTokenType tok; char *type; json = PG_GETARG_TEXT_P(0); lex = makeJsonLexContext(json, false); /* Lex exactly one token from the input and check its type. */ json_lex(lex); tok = lex_peek(lex); switch (tok) { case JSON_TOKEN_OBJECT_START: type = "object"; break; case JSON_TOKEN_ARRAY_START: type = "array"; break; case JSON_TOKEN_STRING: type = "string"; break; case JSON_TOKEN_NUMBER: type = "number"; break; case JSON_TOKEN_TRUE: case JSON_TOKEN_FALSE: type = "boolean"; break; case JSON_TOKEN_NULL: type = "null"; break; default: elog(ERROR, "unexpected json token: %d", tok); } PG_RETURN_TEXT_P(cstring_to_text(type)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,024
lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token) { if (!lex_accept(lex, token, NULL)) report_parse_error(ctx, lex); }
DoS Overflow
0
lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token) { if (!lex_accept(lex, token, NULL)) report_parse_error(ctx, lex); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,025
lex_peek(JsonLexContext *lex) { return lex->token_type; }
DoS Overflow
0
lex_peek(JsonLexContext *lex) { return lex->token_type; }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,026
makeJsonLexContext(text *json, bool need_escapes) { return makeJsonLexContextCstringLen(VARDATA(json), VARSIZE(json) - VARHDRSZ, need_escapes); }
DoS Overflow
0
makeJsonLexContext(text *json, bool need_escapes) { return makeJsonLexContextCstringLen(VARDATA(json), VARSIZE(json) - VARHDRSZ, need_escapes); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,027
makeJsonLexContextCstringLen(char *json, int len, bool need_escapes) { JsonLexContext *lex = palloc0(sizeof(JsonLexContext)); lex->input = lex->token_terminator = lex->line_start = json; lex->line_number = 1; lex->input_length = len; if (need_escapes) lex->strval = makeStringInfo(); return lex; }
DoS Overflow
0
makeJsonLexContextCstringLen(char *json, int len, bool need_escapes) { JsonLexContext *lex = palloc0(sizeof(JsonLexContext)); lex->input = lex->token_terminator = lex->line_start = json; lex->line_number = 1; lex->input_length = len; if (need_escapes) lex->strval = makeStringInfo(); return lex; }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,028
parse_object_field(JsonLexContext *lex, JsonSemAction *sem) { /* * An object field is "fieldname" : value where value can be a scalar, * object or array. Note: in user-facing docs and error messages, we * generally call a field name a "key". */ char *fname = NULL; /* keep compiler quiet */ json_ofield_action ostart = sem->object_field_start; json_ofield_action oend = sem->object_field_end; bool isnull; char **fnameaddr = NULL; JsonTokenType tok; if (ostart != NULL || oend != NULL) fnameaddr = &fname; if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr)) report_parse_error(JSON_PARSE_STRING, lex); lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON); tok = lex_peek(lex); isnull = tok == JSON_TOKEN_NULL; if (ostart != NULL) (*ostart) (sem->semstate, fname, isnull); switch (tok) { case JSON_TOKEN_OBJECT_START: parse_object(lex, sem); break; case JSON_TOKEN_ARRAY_START: parse_array(lex, sem); break; default: parse_scalar(lex, sem); } if (oend != NULL) (*oend) (sem->semstate, fname, isnull); }
DoS Overflow
0
parse_object_field(JsonLexContext *lex, JsonSemAction *sem) { /* * An object field is "fieldname" : value where value can be a scalar, * object or array. Note: in user-facing docs and error messages, we * generally call a field name a "key". */ char *fname = NULL; /* keep compiler quiet */ json_ofield_action ostart = sem->object_field_start; json_ofield_action oend = sem->object_field_end; bool isnull; char **fnameaddr = NULL; JsonTokenType tok; if (ostart != NULL || oend != NULL) fnameaddr = &fname; if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr)) report_parse_error(JSON_PARSE_STRING, lex); lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON); tok = lex_peek(lex); isnull = tok == JSON_TOKEN_NULL; if (ostart != NULL) (*ostart) (sem->semstate, fname, isnull); switch (tok) { case JSON_TOKEN_OBJECT_START: parse_object(lex, sem); break; case JSON_TOKEN_ARRAY_START: parse_array(lex, sem); break; default: parse_scalar(lex, sem); } if (oend != NULL) (*oend) (sem->semstate, fname, isnull); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,029
parse_scalar(JsonLexContext *lex, JsonSemAction *sem) { char *val = NULL; json_scalar_action sfunc = sem->scalar; char **valaddr; JsonTokenType tok = lex_peek(lex); valaddr = sfunc == NULL ? NULL : &val; /* a scalar must be a string, a number, true, false, or null */ switch (tok) { case JSON_TOKEN_TRUE: lex_accept(lex, JSON_TOKEN_TRUE, valaddr); break; case JSON_TOKEN_FALSE: lex_accept(lex, JSON_TOKEN_FALSE, valaddr); break; case JSON_TOKEN_NULL: lex_accept(lex, JSON_TOKEN_NULL, valaddr); break; case JSON_TOKEN_NUMBER: lex_accept(lex, JSON_TOKEN_NUMBER, valaddr); break; case JSON_TOKEN_STRING: lex_accept(lex, JSON_TOKEN_STRING, valaddr); break; default: report_parse_error(JSON_PARSE_VALUE, lex); } if (sfunc != NULL) (*sfunc) (sem->semstate, val, tok); }
DoS Overflow
0
parse_scalar(JsonLexContext *lex, JsonSemAction *sem) { char *val = NULL; json_scalar_action sfunc = sem->scalar; char **valaddr; JsonTokenType tok = lex_peek(lex); valaddr = sfunc == NULL ? NULL : &val; /* a scalar must be a string, a number, true, false, or null */ switch (tok) { case JSON_TOKEN_TRUE: lex_accept(lex, JSON_TOKEN_TRUE, valaddr); break; case JSON_TOKEN_FALSE: lex_accept(lex, JSON_TOKEN_FALSE, valaddr); break; case JSON_TOKEN_NULL: lex_accept(lex, JSON_TOKEN_NULL, valaddr); break; case JSON_TOKEN_NUMBER: lex_accept(lex, JSON_TOKEN_NUMBER, valaddr); break; case JSON_TOKEN_STRING: lex_accept(lex, JSON_TOKEN_STRING, valaddr); break; default: report_parse_error(JSON_PARSE_VALUE, lex); } if (sfunc != NULL) (*sfunc) (sem->semstate, val, tok); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,030
pg_parse_json(JsonLexContext *lex, JsonSemAction *sem) { JsonTokenType tok; /* get the initial token */ json_lex(lex); tok = lex_peek(lex); /* parse by recursive descent */ switch (tok) { case JSON_TOKEN_OBJECT_START: parse_object(lex, sem); break; case JSON_TOKEN_ARRAY_START: parse_array(lex, sem); break; default: parse_scalar(lex, sem); /* json can be a bare scalar */ } lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END); }
DoS Overflow
0
pg_parse_json(JsonLexContext *lex, JsonSemAction *sem) { JsonTokenType tok; /* get the initial token */ json_lex(lex); tok = lex_peek(lex); /* parse by recursive descent */ switch (tok) { case JSON_TOKEN_OBJECT_START: parse_object(lex, sem); break; case JSON_TOKEN_ARRAY_START: parse_array(lex, sem); break; default: parse_scalar(lex, sem); /* json can be a bare scalar */ } lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,031
report_invalid_token(JsonLexContext *lex) { char *token; int toklen; /* Separate out the offending token. */ toklen = lex->token_terminator - lex->token_start; token = palloc(toklen + 1); memcpy(token, lex->token_start, toklen); token[toklen] = '\0'; ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Token \"%s\" is invalid.", token), report_json_context(lex))); }
DoS Overflow
0
report_invalid_token(JsonLexContext *lex) { char *token; int toklen; /* Separate out the offending token. */ toklen = lex->token_terminator - lex->token_start; token = palloc(toklen + 1); memcpy(token, lex->token_start, toklen); token[toklen] = '\0'; ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Token \"%s\" is invalid.", token), report_json_context(lex))); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,032
report_json_context(JsonLexContext *lex) { const char *context_start; const char *context_end; const char *line_start; int line_number; char *ctxt; int ctxtlen; const char *prefix; const char *suffix; /* Choose boundaries for the part of the input we will display */ context_start = lex->input; context_end = lex->token_terminator; line_start = context_start; line_number = 1; for (;;) { /* Always advance over newlines */ if (context_start < context_end && *context_start == '\n') { context_start++; line_start = context_start; line_number++; continue; } /* Otherwise, done as soon as we are close enough to context_end */ if (context_end - context_start < 50) break; /* Advance to next multibyte character */ if (IS_HIGHBIT_SET(*context_start)) context_start += pg_mblen(context_start); else context_start++; } /* * We add "..." to indicate that the excerpt doesn't start at the * beginning of the line ... but if we're within 3 characters of the * beginning of the line, we might as well just show the whole line. */ if (context_start - line_start <= 3) context_start = line_start; /* Get a null-terminated copy of the data to present */ ctxtlen = context_end - context_start; ctxt = palloc(ctxtlen + 1); memcpy(ctxt, context_start, ctxtlen); ctxt[ctxtlen] = '\0'; /* * Show the context, prefixing "..." if not starting at start of line, and * suffixing "..." if not ending at end of line. */ prefix = (context_start > line_start) ? "..." : ""; suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : ""; return errcontext("JSON data, line %d: %s%s%s", line_number, prefix, ctxt, suffix); }
DoS Overflow
0
report_json_context(JsonLexContext *lex) { const char *context_start; const char *context_end; const char *line_start; int line_number; char *ctxt; int ctxtlen; const char *prefix; const char *suffix; /* Choose boundaries for the part of the input we will display */ context_start = lex->input; context_end = lex->token_terminator; line_start = context_start; line_number = 1; for (;;) { /* Always advance over newlines */ if (context_start < context_end && *context_start == '\n') { context_start++; line_start = context_start; line_number++; continue; } /* Otherwise, done as soon as we are close enough to context_end */ if (context_end - context_start < 50) break; /* Advance to next multibyte character */ if (IS_HIGHBIT_SET(*context_start)) context_start += pg_mblen(context_start); else context_start++; } /* * We add "..." to indicate that the excerpt doesn't start at the * beginning of the line ... but if we're within 3 characters of the * beginning of the line, we might as well just show the whole line. */ if (context_start - line_start <= 3) context_start = line_start; /* Get a null-terminated copy of the data to present */ ctxtlen = context_end - context_start; ctxt = palloc(ctxtlen + 1); memcpy(ctxt, context_start, ctxtlen); ctxt[ctxtlen] = '\0'; /* * Show the context, prefixing "..." if not starting at start of line, and * suffixing "..." if not ending at end of line. */ prefix = (context_start > line_start) ? "..." : ""; suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : ""; return errcontext("JSON data, line %d: %s%s%s", line_number, prefix, ctxt, suffix); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,033
report_parse_error(JsonParseContext ctx, JsonLexContext *lex) { char *token; int toklen; /* Handle case where the input ended prematurely. */ if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("The input string ended unexpectedly."), report_json_context(lex))); /* Separate out the current token. */ toklen = lex->token_terminator - lex->token_start; token = palloc(toklen + 1); memcpy(token, lex->token_start, toklen); token[toklen] = '\0'; /* Complain, with the appropriate detail message. */ if (ctx == JSON_PARSE_END) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected end of input, but found \"%s\".", token), report_json_context(lex))); else { switch (ctx) { case JSON_PARSE_VALUE: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected JSON value, but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_STRING: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected string, but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_ARRAY_START: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected array element or \"]\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_ARRAY_NEXT: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected \",\" or \"]\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_START: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected string or \"}\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_LABEL: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected \":\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_NEXT: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected \",\" or \"}\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_COMMA: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected string, but found \"%s\".", token), report_json_context(lex))); break; default: elog(ERROR, "unexpected json parse state: %d", ctx); }
DoS Overflow
0
report_parse_error(JsonParseContext ctx, JsonLexContext *lex) { char *token; int toklen; /* Handle case where the input ended prematurely. */ if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("The input string ended unexpectedly."), report_json_context(lex))); /* Separate out the current token. */ toklen = lex->token_terminator - lex->token_start; token = palloc(toklen + 1); memcpy(token, lex->token_start, toklen); token[toklen] = '\0'; /* Complain, with the appropriate detail message. */ if (ctx == JSON_PARSE_END) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected end of input, but found \"%s\".", token), report_json_context(lex))); else { switch (ctx) { case JSON_PARSE_VALUE: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected JSON value, but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_STRING: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected string, but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_ARRAY_START: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected array element or \"]\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_ARRAY_NEXT: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected \",\" or \"]\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_START: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected string or \"}\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_LABEL: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected \":\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_NEXT: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected \",\" or \"}\", but found \"%s\".", token), report_json_context(lex))); break; case JSON_PARSE_OBJECT_COMMA: ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type json"), errdetail("Expected string, but found \"%s\".", token), report_json_context(lex))); break; default: elog(ERROR, "unexpected json parse state: %d", ctx); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,034
row_to_json(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); StringInfo result; result = makeStringInfo(); composite_to_json(array, result, false); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
DoS Overflow
0
row_to_json(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); StringInfo result; result = makeStringInfo(); composite_to_json(array, result, false); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,035
row_to_json_pretty(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); bool use_line_feeds = PG_GETARG_BOOL(1); StringInfo result; result = makeStringInfo(); composite_to_json(array, result, use_line_feeds); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
DoS Overflow
0
row_to_json_pretty(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); bool use_line_feeds = PG_GETARG_BOOL(1); StringInfo result; result = makeStringInfo(); composite_to_json(array, result, use_line_feeds); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,036
to_json(PG_FUNCTION_ARGS) { Datum val = PG_GETARG_DATUM(0); Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0); StringInfo result; JsonTypeCategory tcategory; Oid outfuncoid; if (val_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine input data type"))); json_categorize_type(val_type, &tcategory, &outfuncoid); result = makeStringInfo(); datum_to_json(val, false, result, tcategory, outfuncoid, false); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
DoS Overflow
0
to_json(PG_FUNCTION_ARGS) { Datum val = PG_GETARG_DATUM(0); Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0); StringInfo result; JsonTypeCategory tcategory; Oid outfuncoid; if (val_type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine input data type"))); json_categorize_type(val_type, &tcategory, &outfuncoid); result = makeStringInfo(); datum_to_json(val, false, result, tcategory, outfuncoid, false); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); }
@@ -490,6 +490,8 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem) json_struct_action oend = sem->object_end; JsonTokenType tok; + check_stack_depth(); + if (ostart != NULL) (*ostart) (sem->semstate); @@ -568,6 +570,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem) json_struct_action astart = sem->array_start; json_struct_action aend = sem->array_end; + check_stack_depth(); + if (astart != NULL) (*astart) (sem->semstate); @@ -1433,6 +1437,8 @@ datum_to_json(Datum val, bool is_null, StringInfo result, char *outputstr; text *jsontext; + check_stack_depth(); + /* callers are expected to ensure that null keys are not passed in */ Assert(!(key_scalar && is_null));
CWE-119
null
null
2,037
JsonbToCString(StringInfo out, JsonbContainer *in, int estimated_len) { return JsonbToCStringWorker(out, in, estimated_len, false); }
DoS Overflow
0
JsonbToCString(StringInfo out, JsonbContainer *in, int estimated_len) { return JsonbToCStringWorker(out, in, estimated_len, false); }
@@ -712,6 +712,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, JsonbValue jb; bool scalar_jsonb = false; + check_stack_depth(); + if (is_null) { Assert(!key_scalar);
CWE-119
null
null
2,038
JsonbToCStringIndent(StringInfo out, JsonbContainer *in, int estimated_len) { return JsonbToCStringWorker(out, in, estimated_len, true); }
DoS Overflow
0
JsonbToCStringIndent(StringInfo out, JsonbContainer *in, int estimated_len) { return JsonbToCStringWorker(out, in, estimated_len, true); }
@@ -712,6 +712,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, JsonbValue jb; bool scalar_jsonb = false; + check_stack_depth(); + if (is_null) { Assert(!key_scalar);
CWE-119
null
null
2,039
JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent) { bool first = true; JsonbIterator *it; JsonbIteratorToken type = WJB_DONE; JsonbValue v; int level = 0; bool redo_switch = false; /* If we are indenting, don't add a space after a comma */ int ispaces = indent ? 1 : 2; /* * Don't indent the very first item. This gets set to the indent flag at * the bottom of the loop. */ bool use_indent = false; bool raw_scalar = false; bool last_was_key = false; if (out == NULL) out = makeStringInfo(); enlargeStringInfo(out, (estimated_len >= 0) ? estimated_len : 64); it = JsonbIteratorInit(in); while (redo_switch || ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)) { redo_switch = false; switch (type) { case WJB_BEGIN_ARRAY: if (!first) appendBinaryStringInfo(out, ", ", ispaces); if (!v.val.array.rawScalar) { add_indent(out, use_indent && !last_was_key, level); appendStringInfoCharMacro(out, '['); } else raw_scalar = true; first = true; level++; break; case WJB_BEGIN_OBJECT: if (!first) appendBinaryStringInfo(out, ", ", ispaces); add_indent(out, use_indent && !last_was_key, level); appendStringInfoCharMacro(out, '{'); first = true; level++; break; case WJB_KEY: if (!first) appendBinaryStringInfo(out, ", ", ispaces); first = true; add_indent(out, use_indent, level); /* json rules guarantee this is a string */ jsonb_put_escaped_value(out, &v); appendBinaryStringInfo(out, ": ", 2); type = JsonbIteratorNext(&it, &v, false); if (type == WJB_VALUE) { first = false; jsonb_put_escaped_value(out, &v); } else { Assert(type == WJB_BEGIN_OBJECT || type == WJB_BEGIN_ARRAY); /* * We need to rerun the current switch() since we need to * output the object which we just got from the iterator * before calling the iterator again. */ redo_switch = true; } break; case WJB_ELEM: if (!first) appendBinaryStringInfo(out, ", ", ispaces); first = false; if (!raw_scalar) add_indent(out, use_indent, level); jsonb_put_escaped_value(out, &v); break; case WJB_END_ARRAY: level--; if (!raw_scalar) { add_indent(out, use_indent, level); appendStringInfoCharMacro(out, ']'); } first = false; break; case WJB_END_OBJECT: level--; add_indent(out, use_indent, level); appendStringInfoCharMacro(out, '}'); first = false; break; default: elog(ERROR, "unknown jsonb iterator token type"); } use_indent = indent; last_was_key = redo_switch; } Assert(level == 0); return out->data; }
DoS Overflow
0
JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent) { bool first = true; JsonbIterator *it; JsonbIteratorToken type = WJB_DONE; JsonbValue v; int level = 0; bool redo_switch = false; /* If we are indenting, don't add a space after a comma */ int ispaces = indent ? 1 : 2; /* * Don't indent the very first item. This gets set to the indent flag at * the bottom of the loop. */ bool use_indent = false; bool raw_scalar = false; bool last_was_key = false; if (out == NULL) out = makeStringInfo(); enlargeStringInfo(out, (estimated_len >= 0) ? estimated_len : 64); it = JsonbIteratorInit(in); while (redo_switch || ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)) { redo_switch = false; switch (type) { case WJB_BEGIN_ARRAY: if (!first) appendBinaryStringInfo(out, ", ", ispaces); if (!v.val.array.rawScalar) { add_indent(out, use_indent && !last_was_key, level); appendStringInfoCharMacro(out, '['); } else raw_scalar = true; first = true; level++; break; case WJB_BEGIN_OBJECT: if (!first) appendBinaryStringInfo(out, ", ", ispaces); add_indent(out, use_indent && !last_was_key, level); appendStringInfoCharMacro(out, '{'); first = true; level++; break; case WJB_KEY: if (!first) appendBinaryStringInfo(out, ", ", ispaces); first = true; add_indent(out, use_indent, level); /* json rules guarantee this is a string */ jsonb_put_escaped_value(out, &v); appendBinaryStringInfo(out, ": ", 2); type = JsonbIteratorNext(&it, &v, false); if (type == WJB_VALUE) { first = false; jsonb_put_escaped_value(out, &v); } else { Assert(type == WJB_BEGIN_OBJECT || type == WJB_BEGIN_ARRAY); /* * We need to rerun the current switch() since we need to * output the object which we just got from the iterator * before calling the iterator again. */ redo_switch = true; } break; case WJB_ELEM: if (!first) appendBinaryStringInfo(out, ", ", ispaces); first = false; if (!raw_scalar) add_indent(out, use_indent, level); jsonb_put_escaped_value(out, &v); break; case WJB_END_ARRAY: level--; if (!raw_scalar) { add_indent(out, use_indent, level); appendStringInfoCharMacro(out, ']'); } first = false; break; case WJB_END_OBJECT: level--; add_indent(out, use_indent, level); appendStringInfoCharMacro(out, '}'); first = false; break; default: elog(ERROR, "unknown jsonb iterator token type"); } use_indent = indent; last_was_key = redo_switch; } Assert(level == 0); return out->data; }
@@ -712,6 +712,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, JsonbValue jb; bool scalar_jsonb = false; + check_stack_depth(); + if (is_null) { Assert(!key_scalar);
CWE-119
null
null
2,040
add_indent(StringInfo out, bool indent, int level) { if (indent) { int i; appendStringInfoCharMacro(out, '\n'); for (i = 0; i < level; i++) appendBinaryStringInfo(out, " ", 4); } }
DoS Overflow
0
add_indent(StringInfo out, bool indent, int level) { if (indent) { int i; appendStringInfoCharMacro(out, '\n'); for (i = 0; i < level; i++) appendBinaryStringInfo(out, " ", 4); } }
@@ -712,6 +712,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, JsonbValue jb; bool scalar_jsonb = false; + check_stack_depth(); + if (is_null) { Assert(!key_scalar);
CWE-119
null
null
2,041
checkStringLen(size_t len) { if (len > JENTRY_OFFLENMASK) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("string too long to represent as jsonb string"), errdetail("Due to an implementation restriction, jsonb strings cannot exceed %d bytes.", JENTRY_OFFLENMASK))); return len; }
DoS Overflow
0
checkStringLen(size_t len) { if (len > JENTRY_OFFLENMASK) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("string too long to represent as jsonb string"), errdetail("Due to an implementation restriction, jsonb strings cannot exceed %d bytes.", JENTRY_OFFLENMASK))); return len; }
@@ -712,6 +712,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, JsonbValue jb; bool scalar_jsonb = false; + check_stack_depth(); + if (is_null) { Assert(!key_scalar);
CWE-119
null
null
2,042
IteratorConcat(JsonbIterator **it1, JsonbIterator **it2, JsonbParseState **state) { uint32 r1, r2, rk1, rk2; JsonbValue v1, v2, *res = NULL; r1 = rk1 = JsonbIteratorNext(it1, &v1, false); r2 = rk2 = JsonbIteratorNext(it2, &v2, false); /* * Both elements are objects. */ if (rk1 == WJB_BEGIN_OBJECT && rk2 == WJB_BEGIN_OBJECT) { /* * Append the all tokens from v1 to res, except last WJB_END_OBJECT * (because res will not be finished yet). */ pushJsonbValue(state, r1, NULL); while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_OBJECT) pushJsonbValue(state, r1, &v1); /* * Append the all tokens from v2 to res, include last WJB_END_OBJECT * (the concatenation will be completed). */ while ((r2 = JsonbIteratorNext(it2, &v2, true)) != 0) res = pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL); } /* * Both elements are arrays (either can be scalar). */ else if (rk1 == WJB_BEGIN_ARRAY && rk2 == WJB_BEGIN_ARRAY) { pushJsonbValue(state, r1, NULL); while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_ARRAY) { Assert(r1 == WJB_ELEM); pushJsonbValue(state, r1, &v1); } while ((r2 = JsonbIteratorNext(it2, &v2, true)) != WJB_END_ARRAY) { Assert(r2 == WJB_ELEM); pushJsonbValue(state, WJB_ELEM, &v2); } res = pushJsonbValue(state, WJB_END_ARRAY, NULL /* signal to sort */ ); } /* have we got array || object or object || array? */ else if (((rk1 == WJB_BEGIN_ARRAY && !(*it1)->isScalar) && rk2 == WJB_BEGIN_OBJECT) || (rk1 == WJB_BEGIN_OBJECT && (rk2 == WJB_BEGIN_ARRAY && !(*it2)->isScalar))) { JsonbIterator **it_array = rk1 == WJB_BEGIN_ARRAY ? it1 : it2; JsonbIterator **it_object = rk1 == WJB_BEGIN_OBJECT ? it1 : it2; bool prepend = (rk1 == WJB_BEGIN_OBJECT); pushJsonbValue(state, WJB_BEGIN_ARRAY, NULL); if (prepend) { pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL); while ((r1 = JsonbIteratorNext(it_object, &v1, true)) != 0) pushJsonbValue(state, r1, r1 != WJB_END_OBJECT ? &v1 : NULL); while ((r2 = JsonbIteratorNext(it_array, &v2, true)) != 0) res = pushJsonbValue(state, r2, r2 != WJB_END_ARRAY ? &v2 : NULL); } else { while ((r1 = JsonbIteratorNext(it_array, &v1, true)) != WJB_END_ARRAY) pushJsonbValue(state, r1, &v1); pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL); while ((r2 = JsonbIteratorNext(it_object, &v2, true)) != 0) pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL); res = pushJsonbValue(state, WJB_END_ARRAY, NULL); } } else { /* * This must be scalar || object or object || scalar, as that's all * that's left. Both of these make no sense, so error out. */ ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid concatenation of jsonb objects"))); } return res; }
DoS Overflow
0
IteratorConcat(JsonbIterator **it1, JsonbIterator **it2, JsonbParseState **state) { uint32 r1, r2, rk1, rk2; JsonbValue v1, v2, *res = NULL; r1 = rk1 = JsonbIteratorNext(it1, &v1, false); r2 = rk2 = JsonbIteratorNext(it2, &v2, false); /* * Both elements are objects. */ if (rk1 == WJB_BEGIN_OBJECT && rk2 == WJB_BEGIN_OBJECT) { /* * Append the all tokens from v1 to res, except last WJB_END_OBJECT * (because res will not be finished yet). */ pushJsonbValue(state, r1, NULL); while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_OBJECT) pushJsonbValue(state, r1, &v1); /* * Append the all tokens from v2 to res, include last WJB_END_OBJECT * (the concatenation will be completed). */ while ((r2 = JsonbIteratorNext(it2, &v2, true)) != 0) res = pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL); } /* * Both elements are arrays (either can be scalar). */ else if (rk1 == WJB_BEGIN_ARRAY && rk2 == WJB_BEGIN_ARRAY) { pushJsonbValue(state, r1, NULL); while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_ARRAY) { Assert(r1 == WJB_ELEM); pushJsonbValue(state, r1, &v1); } while ((r2 = JsonbIteratorNext(it2, &v2, true)) != WJB_END_ARRAY) { Assert(r2 == WJB_ELEM); pushJsonbValue(state, WJB_ELEM, &v2); } res = pushJsonbValue(state, WJB_END_ARRAY, NULL /* signal to sort */ ); } /* have we got array || object or object || array? */ else if (((rk1 == WJB_BEGIN_ARRAY && !(*it1)->isScalar) && rk2 == WJB_BEGIN_OBJECT) || (rk1 == WJB_BEGIN_OBJECT && (rk2 == WJB_BEGIN_ARRAY && !(*it2)->isScalar))) { JsonbIterator **it_array = rk1 == WJB_BEGIN_ARRAY ? it1 : it2; JsonbIterator **it_object = rk1 == WJB_BEGIN_OBJECT ? it1 : it2; bool prepend = (rk1 == WJB_BEGIN_OBJECT); pushJsonbValue(state, WJB_BEGIN_ARRAY, NULL); if (prepend) { pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL); while ((r1 = JsonbIteratorNext(it_object, &v1, true)) != 0) pushJsonbValue(state, r1, r1 != WJB_END_OBJECT ? &v1 : NULL); while ((r2 = JsonbIteratorNext(it_array, &v2, true)) != 0) res = pushJsonbValue(state, r2, r2 != WJB_END_ARRAY ? &v2 : NULL); } else { while ((r1 = JsonbIteratorNext(it_array, &v1, true)) != WJB_END_ARRAY) pushJsonbValue(state, r1, &v1); pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL); while ((r2 = JsonbIteratorNext(it_object, &v2, true)) != 0) pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL); res = pushJsonbValue(state, WJB_END_ARRAY, NULL); } } else { /* * This must be scalar || object or object || scalar, as that's all * that's left. Both of these make no sense, so error out. */ ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid concatenation of jsonb objects"))); } return res; }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,043
addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb) { JsonbIterator *it; JsonbValue *o = &(*jbps)->contVal; int type; JsonbValue v; it = JsonbIteratorInit(&jb->root); Assert(o->type == jbvArray || o->type == jbvObject); if (JB_ROOT_IS_SCALAR(jb)) { (void) JsonbIteratorNext(&it, &v, false); /* skip array header */ (void) JsonbIteratorNext(&it, &v, false); /* fetch scalar value */ switch (o->type) { case jbvArray: (void) pushJsonbValue(jbps, WJB_ELEM, &v); break; case jbvObject: (void) pushJsonbValue(jbps, WJB_VALUE, &v); break; default: elog(ERROR, "unexpected parent of nested structure"); } } else { while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) { if (type == WJB_KEY || type == WJB_VALUE || type == WJB_ELEM) (void) pushJsonbValue(jbps, type, &v); else (void) pushJsonbValue(jbps, type, NULL); } } }
DoS Overflow
0
addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb) { JsonbIterator *it; JsonbValue *o = &(*jbps)->contVal; int type; JsonbValue v; it = JsonbIteratorInit(&jb->root); Assert(o->type == jbvArray || o->type == jbvObject); if (JB_ROOT_IS_SCALAR(jb)) { (void) JsonbIteratorNext(&it, &v, false); /* skip array header */ (void) JsonbIteratorNext(&it, &v, false); /* fetch scalar value */ switch (o->type) { case jbvArray: (void) pushJsonbValue(jbps, WJB_ELEM, &v); break; case jbvObject: (void) pushJsonbValue(jbps, WJB_VALUE, &v); break; default: elog(ERROR, "unexpected parent of nested structure"); } } else { while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) { if (type == WJB_KEY || type == WJB_VALUE || type == WJB_ELEM) (void) pushJsonbValue(jbps, type, &v); else (void) pushJsonbValue(jbps, type, NULL); } } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,044
alen_array_element_start(void *state, bool isnull) { AlenState *_state = (AlenState *) state; /* just count up all the level 1 elements */ if (_state->lex->lex_level == 1) _state->count++; }
DoS Overflow
0
alen_array_element_start(void *state, bool isnull) { AlenState *_state = (AlenState *) state; /* just count up all the level 1 elements */ if (_state->lex->lex_level == 1) _state->count++; }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,045
alen_object_start(void *state) { AlenState *_state = (AlenState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a non-array"))); }
DoS Overflow
0
alen_object_start(void *state) { AlenState *_state = (AlenState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a non-array"))); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,046
alen_scalar(void *state, char *token, JsonTokenType tokentype) { AlenState *_state = (AlenState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a scalar"))); }
DoS Overflow
0
alen_scalar(void *state, char *token, JsonTokenType tokentype) { AlenState *_state = (AlenState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a scalar"))); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,047
each_object_field_end(void *state, char *fname, bool isnull) { EachState *_state = (EachState *) state; MemoryContext old_cxt; int len; text *val; HeapTuple tuple; Datum values[2]; bool nulls[2] = {false, false}; /* skip over nested objects */ if (_state->lex->lex_level != 1) return; /* use the tmp context so we can clean up after each tuple is done */ old_cxt = MemoryContextSwitchTo(_state->tmp_cxt); values[0] = CStringGetTextDatum(fname); if (isnull && _state->normalize_results) { nulls[1] = true; values[1] = (Datum) 0; } else if (_state->next_scalar) { values[1] = CStringGetTextDatum(_state->normalized_scalar); _state->next_scalar = false; } else { len = _state->lex->prev_token_terminator - _state->result_start; val = cstring_to_text_with_len(_state->result_start, len); values[1] = PointerGetDatum(val); } tuple = heap_form_tuple(_state->ret_tdesc, values, nulls); tuplestore_puttuple(_state->tuple_store, tuple); /* clean up and switch back */ MemoryContextSwitchTo(old_cxt); MemoryContextReset(_state->tmp_cxt); }
DoS Overflow
0
each_object_field_end(void *state, char *fname, bool isnull) { EachState *_state = (EachState *) state; MemoryContext old_cxt; int len; text *val; HeapTuple tuple; Datum values[2]; bool nulls[2] = {false, false}; /* skip over nested objects */ if (_state->lex->lex_level != 1) return; /* use the tmp context so we can clean up after each tuple is done */ old_cxt = MemoryContextSwitchTo(_state->tmp_cxt); values[0] = CStringGetTextDatum(fname); if (isnull && _state->normalize_results) { nulls[1] = true; values[1] = (Datum) 0; } else if (_state->next_scalar) { values[1] = CStringGetTextDatum(_state->normalized_scalar); _state->next_scalar = false; } else { len = _state->lex->prev_token_terminator - _state->result_start; val = cstring_to_text_with_len(_state->result_start, len); values[1] = PointerGetDatum(val); } tuple = heap_form_tuple(_state->ret_tdesc, values, nulls); tuplestore_puttuple(_state->tuple_store, tuple); /* clean up and switch back */ MemoryContextSwitchTo(old_cxt); MemoryContextReset(_state->tmp_cxt); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,048
each_object_field_start(void *state, char *fname, bool isnull) { EachState *_state = (EachState *) state; /* save a pointer to where the value starts */ if (_state->lex->lex_level == 1) { /* * next_scalar will be reset in the object_field_end handler, and * since we know the value is a scalar there is no danger of it being * on while recursing down the tree. */ if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) _state->next_scalar = true; else _state->result_start = _state->lex->token_start; } }
DoS Overflow
0
each_object_field_start(void *state, char *fname, bool isnull) { EachState *_state = (EachState *) state; /* save a pointer to where the value starts */ if (_state->lex->lex_level == 1) { /* * next_scalar will be reset in the object_field_end handler, and * since we know the value is a scalar there is no danger of it being * on while recursing down the tree. */ if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) _state->next_scalar = true; else _state->result_start = _state->lex->token_start; } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,049
each_worker(FunctionCallInfo fcinfo, bool as_text) { text *json = PG_GETARG_TEXT_P(0); JsonLexContext *lex; JsonSemAction *sem; ReturnSetInfo *rsi; MemoryContext old_cxt; TupleDesc tupdesc; EachState *state; lex = makeJsonLexContext(json, true); state = palloc0(sizeof(EachState)); sem = palloc0(sizeof(JsonSemAction)); rsi = (ReturnSetInfo *) fcinfo->resultinfo; if (!rsi || !IsA(rsi, ReturnSetInfo) || (rsi->allowedModes & SFRM_Materialize) == 0 || rsi->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that " "cannot accept a set"))); rsi->returnMode = SFRM_Materialize; (void) get_call_result_type(fcinfo, NULL, &tupdesc); /* make these in a sufficiently long-lived memory context */ old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory); state->ret_tdesc = CreateTupleDescCopy(tupdesc); BlessTupleDesc(state->ret_tdesc); state->tuple_store = tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random, false, work_mem); MemoryContextSwitchTo(old_cxt); sem->semstate = (void *) state; sem->array_start = each_array_start; sem->scalar = each_scalar; sem->object_field_start = each_object_field_start; sem->object_field_end = each_object_field_end; state->normalize_results = as_text; state->next_scalar = false; state->lex = lex; state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext, "json_each temporary cxt", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); pg_parse_json(lex, sem); MemoryContextDelete(state->tmp_cxt); rsi->setResult = state->tuple_store; rsi->setDesc = state->ret_tdesc; PG_RETURN_NULL(); }
DoS Overflow
0
each_worker(FunctionCallInfo fcinfo, bool as_text) { text *json = PG_GETARG_TEXT_P(0); JsonLexContext *lex; JsonSemAction *sem; ReturnSetInfo *rsi; MemoryContext old_cxt; TupleDesc tupdesc; EachState *state; lex = makeJsonLexContext(json, true); state = palloc0(sizeof(EachState)); sem = palloc0(sizeof(JsonSemAction)); rsi = (ReturnSetInfo *) fcinfo->resultinfo; if (!rsi || !IsA(rsi, ReturnSetInfo) || (rsi->allowedModes & SFRM_Materialize) == 0 || rsi->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that " "cannot accept a set"))); rsi->returnMode = SFRM_Materialize; (void) get_call_result_type(fcinfo, NULL, &tupdesc); /* make these in a sufficiently long-lived memory context */ old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory); state->ret_tdesc = CreateTupleDescCopy(tupdesc); BlessTupleDesc(state->ret_tdesc); state->tuple_store = tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random, false, work_mem); MemoryContextSwitchTo(old_cxt); sem->semstate = (void *) state; sem->array_start = each_array_start; sem->scalar = each_scalar; sem->object_field_start = each_object_field_start; sem->object_field_end = each_object_field_end; state->normalize_results = as_text; state->next_scalar = false; state->lex = lex; state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext, "json_each temporary cxt", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); pg_parse_json(lex, sem); MemoryContextDelete(state->tmp_cxt); rsi->setResult = state->tuple_store; rsi->setDesc = state->ret_tdesc; PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,050
elements_array_element_end(void *state, bool isnull) { ElementsState *_state = (ElementsState *) state; MemoryContext old_cxt; int len; text *val; HeapTuple tuple; Datum values[1]; bool nulls[1] = {false}; /* skip over nested objects */ if (_state->lex->lex_level != 1) return; /* use the tmp context so we can clean up after each tuple is done */ old_cxt = MemoryContextSwitchTo(_state->tmp_cxt); if (isnull && _state->normalize_results) { nulls[0] = true; values[0] = (Datum) NULL; } else if (_state->next_scalar) { values[0] = CStringGetTextDatum(_state->normalized_scalar); _state->next_scalar = false; } else { len = _state->lex->prev_token_terminator - _state->result_start; val = cstring_to_text_with_len(_state->result_start, len); values[0] = PointerGetDatum(val); } tuple = heap_form_tuple(_state->ret_tdesc, values, nulls); tuplestore_puttuple(_state->tuple_store, tuple); /* clean up and switch back */ MemoryContextSwitchTo(old_cxt); MemoryContextReset(_state->tmp_cxt); }
DoS Overflow
0
elements_array_element_end(void *state, bool isnull) { ElementsState *_state = (ElementsState *) state; MemoryContext old_cxt; int len; text *val; HeapTuple tuple; Datum values[1]; bool nulls[1] = {false}; /* skip over nested objects */ if (_state->lex->lex_level != 1) return; /* use the tmp context so we can clean up after each tuple is done */ old_cxt = MemoryContextSwitchTo(_state->tmp_cxt); if (isnull && _state->normalize_results) { nulls[0] = true; values[0] = (Datum) NULL; } else if (_state->next_scalar) { values[0] = CStringGetTextDatum(_state->normalized_scalar); _state->next_scalar = false; } else { len = _state->lex->prev_token_terminator - _state->result_start; val = cstring_to_text_with_len(_state->result_start, len); values[0] = PointerGetDatum(val); } tuple = heap_form_tuple(_state->ret_tdesc, values, nulls); tuplestore_puttuple(_state->tuple_store, tuple); /* clean up and switch back */ MemoryContextSwitchTo(old_cxt); MemoryContextReset(_state->tmp_cxt); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,051
elements_array_element_start(void *state, bool isnull) { ElementsState *_state = (ElementsState *) state; /* save a pointer to where the value starts */ if (_state->lex->lex_level == 1) { /* * next_scalar will be reset in the array_element_end handler, and * since we know the value is a scalar there is no danger of it being * on while recursing down the tree. */ if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) _state->next_scalar = true; else _state->result_start = _state->lex->token_start; } }
DoS Overflow
0
elements_array_element_start(void *state, bool isnull) { ElementsState *_state = (ElementsState *) state; /* save a pointer to where the value starts */ if (_state->lex->lex_level == 1) { /* * next_scalar will be reset in the array_element_end handler, and * since we know the value is a scalar there is no danger of it being * on while recursing down the tree. */ if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) _state->next_scalar = true; else _state->result_start = _state->lex->token_start; } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,052
elements_object_start(void *state) { ElementsState *_state = (ElementsState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on a non-array", _state->function_name))); }
DoS Overflow
0
elements_object_start(void *state) { ElementsState *_state = (ElementsState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on a non-array", _state->function_name))); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,053
elements_scalar(void *state, char *token, JsonTokenType tokentype) { ElementsState *_state = (ElementsState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on a scalar", _state->function_name))); /* supply de-escaped value if required */ if (_state->next_scalar) _state->normalized_scalar = token; }
DoS Overflow
0
elements_scalar(void *state, char *token, JsonTokenType tokentype) { ElementsState *_state = (ElementsState *) state; /* json structure check */ if (_state->lex->lex_level == 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on a scalar", _state->function_name))); /* supply de-escaped value if required */ if (_state->next_scalar) _state->normalized_scalar = token; }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,054
elements_worker(FunctionCallInfo fcinfo, const char *funcname, bool as_text) { text *json = PG_GETARG_TEXT_P(0); /* elements only needs escaped strings when as_text */ JsonLexContext *lex = makeJsonLexContext(json, as_text); JsonSemAction *sem; ReturnSetInfo *rsi; MemoryContext old_cxt; TupleDesc tupdesc; ElementsState *state; state = palloc0(sizeof(ElementsState)); sem = palloc0(sizeof(JsonSemAction)); rsi = (ReturnSetInfo *) fcinfo->resultinfo; if (!rsi || !IsA(rsi, ReturnSetInfo) || (rsi->allowedModes & SFRM_Materialize) == 0 || rsi->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that " "cannot accept a set"))); rsi->returnMode = SFRM_Materialize; /* it's a simple type, so don't use get_call_result_type() */ tupdesc = rsi->expectedDesc; /* make these in a sufficiently long-lived memory context */ old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory); state->ret_tdesc = CreateTupleDescCopy(tupdesc); BlessTupleDesc(state->ret_tdesc); state->tuple_store = tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random, false, work_mem); MemoryContextSwitchTo(old_cxt); sem->semstate = (void *) state; sem->object_start = elements_object_start; sem->scalar = elements_scalar; sem->array_element_start = elements_array_element_start; sem->array_element_end = elements_array_element_end; state->function_name = funcname; state->normalize_results = as_text; state->next_scalar = false; state->lex = lex; state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext, "json_array_elements temporary cxt", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); pg_parse_json(lex, sem); MemoryContextDelete(state->tmp_cxt); rsi->setResult = state->tuple_store; rsi->setDesc = state->ret_tdesc; PG_RETURN_NULL(); }
DoS Overflow
0
elements_worker(FunctionCallInfo fcinfo, const char *funcname, bool as_text) { text *json = PG_GETARG_TEXT_P(0); /* elements only needs escaped strings when as_text */ JsonLexContext *lex = makeJsonLexContext(json, as_text); JsonSemAction *sem; ReturnSetInfo *rsi; MemoryContext old_cxt; TupleDesc tupdesc; ElementsState *state; state = palloc0(sizeof(ElementsState)); sem = palloc0(sizeof(JsonSemAction)); rsi = (ReturnSetInfo *) fcinfo->resultinfo; if (!rsi || !IsA(rsi, ReturnSetInfo) || (rsi->allowedModes & SFRM_Materialize) == 0 || rsi->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that " "cannot accept a set"))); rsi->returnMode = SFRM_Materialize; /* it's a simple type, so don't use get_call_result_type() */ tupdesc = rsi->expectedDesc; /* make these in a sufficiently long-lived memory context */ old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory); state->ret_tdesc = CreateTupleDescCopy(tupdesc); BlessTupleDesc(state->ret_tdesc); state->tuple_store = tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random, false, work_mem); MemoryContextSwitchTo(old_cxt); sem->semstate = (void *) state; sem->object_start = elements_object_start; sem->scalar = elements_scalar; sem->array_element_start = elements_array_element_start; sem->array_element_end = elements_array_element_end; state->function_name = funcname; state->normalize_results = as_text; state->next_scalar = false; state->lex = lex; state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext, "json_array_elements temporary cxt", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); pg_parse_json(lex, sem); MemoryContextDelete(state->tmp_cxt); rsi->setResult = state->tuple_store; rsi->setDesc = state->ret_tdesc; PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,055
elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text) { Jsonb *jb = PG_GETARG_JSONB(0); ReturnSetInfo *rsi; Tuplestorestate *tuple_store; TupleDesc tupdesc; TupleDesc ret_tdesc; MemoryContext old_cxt, tmp_cxt; bool skipNested = false; JsonbIterator *it; JsonbValue v; int r; if (JB_ROOT_IS_SCALAR(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot extract elements from a scalar"))); else if (!JB_ROOT_IS_ARRAY(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot extract elements from an object"))); rsi = (ReturnSetInfo *) fcinfo->resultinfo; if (!rsi || !IsA(rsi, ReturnSetInfo) || (rsi->allowedModes & SFRM_Materialize) == 0 || rsi->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that " "cannot accept a set"))); rsi->returnMode = SFRM_Materialize; /* it's a simple type, so don't use get_call_result_type() */ tupdesc = rsi->expectedDesc; old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory); ret_tdesc = CreateTupleDescCopy(tupdesc); BlessTupleDesc(ret_tdesc); tuple_store = tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random, false, work_mem); MemoryContextSwitchTo(old_cxt); tmp_cxt = AllocSetContextCreate(CurrentMemoryContext, "jsonb_array_elements temporary cxt", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); it = JsonbIteratorInit(&jb->root); while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE) { skipNested = true; if (r == WJB_ELEM) { HeapTuple tuple; Datum values[1]; bool nulls[1] = {false}; /* use the tmp context so we can clean up after each tuple is done */ old_cxt = MemoryContextSwitchTo(tmp_cxt); if (!as_text) { Jsonb *val = JsonbValueToJsonb(&v); values[0] = PointerGetDatum(val); } else { if (v.type == jbvNull) { /* a json null is an sql null in text mode */ nulls[0] = true; values[0] = (Datum) NULL; } else { text *sv; if (v.type == jbvString) { /* in text mode scalar strings should be dequoted */ sv = cstring_to_text_with_len(v.val.string.val, v.val.string.len); } else { /* turn anything else into a json string */ StringInfo jtext = makeStringInfo(); Jsonb *jb = JsonbValueToJsonb(&v); (void) JsonbToCString(jtext, &jb->root, 0); sv = cstring_to_text_with_len(jtext->data, jtext->len); } values[0] = PointerGetDatum(sv); } } tuple = heap_form_tuple(ret_tdesc, values, nulls); tuplestore_puttuple(tuple_store, tuple); /* clean up and switch back */ MemoryContextSwitchTo(old_cxt); MemoryContextReset(tmp_cxt); } } MemoryContextDelete(tmp_cxt); rsi->setResult = tuple_store; rsi->setDesc = ret_tdesc; PG_RETURN_NULL(); }
DoS Overflow
0
elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text) { Jsonb *jb = PG_GETARG_JSONB(0); ReturnSetInfo *rsi; Tuplestorestate *tuple_store; TupleDesc tupdesc; TupleDesc ret_tdesc; MemoryContext old_cxt, tmp_cxt; bool skipNested = false; JsonbIterator *it; JsonbValue v; int r; if (JB_ROOT_IS_SCALAR(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot extract elements from a scalar"))); else if (!JB_ROOT_IS_ARRAY(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot extract elements from an object"))); rsi = (ReturnSetInfo *) fcinfo->resultinfo; if (!rsi || !IsA(rsi, ReturnSetInfo) || (rsi->allowedModes & SFRM_Materialize) == 0 || rsi->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that " "cannot accept a set"))); rsi->returnMode = SFRM_Materialize; /* it's a simple type, so don't use get_call_result_type() */ tupdesc = rsi->expectedDesc; old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory); ret_tdesc = CreateTupleDescCopy(tupdesc); BlessTupleDesc(ret_tdesc); tuple_store = tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random, false, work_mem); MemoryContextSwitchTo(old_cxt); tmp_cxt = AllocSetContextCreate(CurrentMemoryContext, "jsonb_array_elements temporary cxt", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); it = JsonbIteratorInit(&jb->root); while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE) { skipNested = true; if (r == WJB_ELEM) { HeapTuple tuple; Datum values[1]; bool nulls[1] = {false}; /* use the tmp context so we can clean up after each tuple is done */ old_cxt = MemoryContextSwitchTo(tmp_cxt); if (!as_text) { Jsonb *val = JsonbValueToJsonb(&v); values[0] = PointerGetDatum(val); } else { if (v.type == jbvNull) { /* a json null is an sql null in text mode */ nulls[0] = true; values[0] = (Datum) NULL; } else { text *sv; if (v.type == jbvString) { /* in text mode scalar strings should be dequoted */ sv = cstring_to_text_with_len(v.val.string.val, v.val.string.len); } else { /* turn anything else into a json string */ StringInfo jtext = makeStringInfo(); Jsonb *jb = JsonbValueToJsonb(&v); (void) JsonbToCString(jtext, &jb->root, 0); sv = cstring_to_text_with_len(jtext->data, jtext->len); } values[0] = PointerGetDatum(sv); } } tuple = heap_form_tuple(ret_tdesc, values, nulls); tuplestore_puttuple(tuple_store, tuple); /* clean up and switch back */ MemoryContextSwitchTo(old_cxt); MemoryContextReset(tmp_cxt); } } MemoryContextDelete(tmp_cxt); rsi->setResult = tuple_store; rsi->setDesc = ret_tdesc; PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,056
get_array_element_end(void *state, bool isnull) { GetState *_state = (GetState *) state; bool get_last = false; int lex_level = _state->lex->lex_level; /* same tests as in get_array_element_start */ if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_indexes != NULL && _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1]) { if (lex_level < _state->npath) { /* done with this element so reset pathok */ _state->pathok[lex_level] = false; } else { /* end of path, so we want this value */ get_last = true; } } /* same logic as for objects */ if (get_last && _state->result_start != NULL) { if (isnull && _state->normalize_results) _state->tresult = (text *) NULL; else { char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } _state->result_start = NULL; } }
DoS Overflow
0
get_array_element_end(void *state, bool isnull) { GetState *_state = (GetState *) state; bool get_last = false; int lex_level = _state->lex->lex_level; /* same tests as in get_array_element_start */ if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_indexes != NULL && _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1]) { if (lex_level < _state->npath) { /* done with this element so reset pathok */ _state->pathok[lex_level] = false; } else { /* end of path, so we want this value */ get_last = true; } } /* same logic as for objects */ if (get_last && _state->result_start != NULL) { if (isnull && _state->normalize_results) _state->tresult = (text *) NULL; else { char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } _state->result_start = NULL; } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,057
get_array_element_start(void *state, bool isnull) { GetState *_state = (GetState *) state; bool get_next = false; int lex_level = _state->lex->lex_level; /* Update array element counter */ if (lex_level <= _state->npath) _state->array_cur_index[lex_level - 1]++; if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_indexes != NULL && _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1]) { if (lex_level < _state->npath) { /* if not at end of path just mark path ok */ _state->pathok[lex_level] = true; } else { /* end of path, so we want this value */ get_next = true; } } /* same logic as for objects */ if (get_next) { _state->tresult = NULL; _state->result_start = NULL; if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) { _state->next_scalar = true; } else { _state->result_start = _state->lex->token_start; } } }
DoS Overflow
0
get_array_element_start(void *state, bool isnull) { GetState *_state = (GetState *) state; bool get_next = false; int lex_level = _state->lex->lex_level; /* Update array element counter */ if (lex_level <= _state->npath) _state->array_cur_index[lex_level - 1]++; if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_indexes != NULL && _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1]) { if (lex_level < _state->npath) { /* if not at end of path just mark path ok */ _state->pathok[lex_level] = true; } else { /* end of path, so we want this value */ get_next = true; } } /* same logic as for objects */ if (get_next) { _state->tresult = NULL; _state->result_start = NULL; if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) { _state->next_scalar = true; } else { _state->result_start = _state->lex->token_start; } } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,058
get_array_end(void *state) { GetState *_state = (GetState *) state; int lex_level = _state->lex->lex_level; if (lex_level == 0 && _state->npath == 0) { /* Special case: return the entire array */ char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } }
DoS Overflow
0
get_array_end(void *state) { GetState *_state = (GetState *) state; int lex_level = _state->lex->lex_level; if (lex_level == 0 && _state->npath == 0) { /* Special case: return the entire array */ char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,059
get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text) { Jsonb *jb = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); Jsonb *res; Datum *pathtext; bool *pathnulls; int npath; int i; bool have_object = false, have_array = false; JsonbValue *jbvp = NULL; JsonbValue tv; JsonbContainer *container; /* * If the array contains any null elements, return NULL, on the grounds * that you'd have gotten NULL if any RHS value were NULL in a nested * series of applications of the -> operator. (Note: because we also * return NULL for error cases such as no-such-field, this is true * regardless of the contents of the rest of the array.) */ if (array_contains_nulls(path)) PG_RETURN_NULL(); deconstruct_array(path, TEXTOID, -1, false, 'i', &pathtext, &pathnulls, &npath); /* Identify whether we have object, array, or scalar at top-level */ container = &jb->root; if (JB_ROOT_IS_OBJECT(jb)) have_object = true; else if (JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb)) have_array = true; else { Assert(JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb)); /* Extract the scalar value, if it is what we'll return */ if (npath <= 0) jbvp = getIthJsonbValueFromContainer(container, 0); } /* * If the array is empty, return the entire LHS object, on the grounds * that we should do zero field or element extractions. For the * non-scalar case we can just hand back the object without much work. For * the scalar case, fall through and deal with the value below the loop. * (This inconsistency arises because there's no easy way to generate a * JsonbValue directly for root-level containers.) */ if (npath <= 0 && jbvp == NULL) { if (as_text) { PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL, container, VARSIZE(jb)))); } else { /* not text mode - just hand back the jsonb */ PG_RETURN_JSONB(jb); } } for (i = 0; i < npath; i++) { if (have_object) { jbvp = findJsonbValueFromContainerLen(container, JB_FOBJECT, VARDATA_ANY(pathtext[i]), VARSIZE_ANY_EXHDR(pathtext[i])); } else if (have_array) { long lindex; uint32 index; char *indextext = TextDatumGetCString(pathtext[i]); char *endptr; errno = 0; lindex = strtol(indextext, &endptr, 10); if (endptr == indextext || *endptr != '\0' || errno != 0 || lindex > INT_MAX || lindex < INT_MIN) PG_RETURN_NULL(); if (lindex >= 0) { index = (uint32) lindex; } else { /* Handle negative subscript */ uint32 nelements; /* Container must be array, but make sure */ if ((container->header & JB_FARRAY) == 0) elog(ERROR, "not a jsonb array"); nelements = container->header & JB_CMASK; if (-lindex > nelements) PG_RETURN_NULL(); else index = nelements + lindex; } jbvp = getIthJsonbValueFromContainer(container, index); } else { /* scalar, extraction yields a null */ PG_RETURN_NULL(); } if (jbvp == NULL) PG_RETURN_NULL(); else if (i == npath - 1) break; if (jbvp->type == jbvBinary) { JsonbIterator *it = JsonbIteratorInit((JsonbContainer *) jbvp->val.binary.data); int r; r = JsonbIteratorNext(&it, &tv, true); container = (JsonbContainer *) jbvp->val.binary.data; have_object = r == WJB_BEGIN_OBJECT; have_array = r == WJB_BEGIN_ARRAY; } else { have_object = jbvp->type == jbvObject; have_array = jbvp->type == jbvArray; } } if (as_text) { /* special-case outputs for string and null values */ if (jbvp->type == jbvString) PG_RETURN_TEXT_P(cstring_to_text_with_len(jbvp->val.string.val, jbvp->val.string.len)); if (jbvp->type == jbvNull) PG_RETURN_NULL(); } res = JsonbValueToJsonb(jbvp); if (as_text) { PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL, &res->root, VARSIZE(res)))); } else { /* not text mode - just hand back the jsonb */ PG_RETURN_JSONB(res); } }
DoS Overflow
0
get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text) { Jsonb *jb = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); Jsonb *res; Datum *pathtext; bool *pathnulls; int npath; int i; bool have_object = false, have_array = false; JsonbValue *jbvp = NULL; JsonbValue tv; JsonbContainer *container; /* * If the array contains any null elements, return NULL, on the grounds * that you'd have gotten NULL if any RHS value were NULL in a nested * series of applications of the -> operator. (Note: because we also * return NULL for error cases such as no-such-field, this is true * regardless of the contents of the rest of the array.) */ if (array_contains_nulls(path)) PG_RETURN_NULL(); deconstruct_array(path, TEXTOID, -1, false, 'i', &pathtext, &pathnulls, &npath); /* Identify whether we have object, array, or scalar at top-level */ container = &jb->root; if (JB_ROOT_IS_OBJECT(jb)) have_object = true; else if (JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb)) have_array = true; else { Assert(JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb)); /* Extract the scalar value, if it is what we'll return */ if (npath <= 0) jbvp = getIthJsonbValueFromContainer(container, 0); } /* * If the array is empty, return the entire LHS object, on the grounds * that we should do zero field or element extractions. For the * non-scalar case we can just hand back the object without much work. For * the scalar case, fall through and deal with the value below the loop. * (This inconsistency arises because there's no easy way to generate a * JsonbValue directly for root-level containers.) */ if (npath <= 0 && jbvp == NULL) { if (as_text) { PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL, container, VARSIZE(jb)))); } else { /* not text mode - just hand back the jsonb */ PG_RETURN_JSONB(jb); } } for (i = 0; i < npath; i++) { if (have_object) { jbvp = findJsonbValueFromContainerLen(container, JB_FOBJECT, VARDATA_ANY(pathtext[i]), VARSIZE_ANY_EXHDR(pathtext[i])); } else if (have_array) { long lindex; uint32 index; char *indextext = TextDatumGetCString(pathtext[i]); char *endptr; errno = 0; lindex = strtol(indextext, &endptr, 10); if (endptr == indextext || *endptr != '\0' || errno != 0 || lindex > INT_MAX || lindex < INT_MIN) PG_RETURN_NULL(); if (lindex >= 0) { index = (uint32) lindex; } else { /* Handle negative subscript */ uint32 nelements; /* Container must be array, but make sure */ if ((container->header & JB_FARRAY) == 0) elog(ERROR, "not a jsonb array"); nelements = container->header & JB_CMASK; if (-lindex > nelements) PG_RETURN_NULL(); else index = nelements + lindex; } jbvp = getIthJsonbValueFromContainer(container, index); } else { /* scalar, extraction yields a null */ PG_RETURN_NULL(); } if (jbvp == NULL) PG_RETURN_NULL(); else if (i == npath - 1) break; if (jbvp->type == jbvBinary) { JsonbIterator *it = JsonbIteratorInit((JsonbContainer *) jbvp->val.binary.data); int r; r = JsonbIteratorNext(&it, &tv, true); container = (JsonbContainer *) jbvp->val.binary.data; have_object = r == WJB_BEGIN_OBJECT; have_array = r == WJB_BEGIN_ARRAY; } else { have_object = jbvp->type == jbvObject; have_array = jbvp->type == jbvArray; } } if (as_text) { /* special-case outputs for string and null values */ if (jbvp->type == jbvString) PG_RETURN_TEXT_P(cstring_to_text_with_len(jbvp->val.string.val, jbvp->val.string.len)); if (jbvp->type == jbvNull) PG_RETURN_NULL(); } res = JsonbValueToJsonb(jbvp); if (as_text) { PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL, &res->root, VARSIZE(res)))); } else { /* not text mode - just hand back the jsonb */ PG_RETURN_JSONB(res); } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,060
get_object_end(void *state) { GetState *_state = (GetState *) state; int lex_level = _state->lex->lex_level; if (lex_level == 0 && _state->npath == 0) { /* Special case: return the entire object */ char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } }
DoS Overflow
0
get_object_end(void *state) { GetState *_state = (GetState *) state; int lex_level = _state->lex->lex_level; if (lex_level == 0 && _state->npath == 0) { /* Special case: return the entire object */ char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,061
get_object_field_end(void *state, char *fname, bool isnull) { GetState *_state = (GetState *) state; bool get_last = false; int lex_level = _state->lex->lex_level; /* same tests as in get_object_field_start */ if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_names != NULL && _state->path_names[lex_level - 1] != NULL && strcmp(fname, _state->path_names[lex_level - 1]) == 0) { if (lex_level < _state->npath) { /* done with this field so reset pathok */ _state->pathok[lex_level] = false; } else { /* end of path, so we want this value */ get_last = true; } } /* for as_text scalar case, our work is already done */ if (get_last && _state->result_start != NULL) { /* * make a text object from the string from the prevously noted json * start up to the end of the previous token (the lexer is by now * ahead of us on whatever came after what we're interested in). */ if (isnull && _state->normalize_results) _state->tresult = (text *) NULL; else { char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } /* this should be unnecessary but let's do it for cleanliness: */ _state->result_start = NULL; } }
DoS Overflow
0
get_object_field_end(void *state, char *fname, bool isnull) { GetState *_state = (GetState *) state; bool get_last = false; int lex_level = _state->lex->lex_level; /* same tests as in get_object_field_start */ if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_names != NULL && _state->path_names[lex_level - 1] != NULL && strcmp(fname, _state->path_names[lex_level - 1]) == 0) { if (lex_level < _state->npath) { /* done with this field so reset pathok */ _state->pathok[lex_level] = false; } else { /* end of path, so we want this value */ get_last = true; } } /* for as_text scalar case, our work is already done */ if (get_last && _state->result_start != NULL) { /* * make a text object from the string from the prevously noted json * start up to the end of the previous token (the lexer is by now * ahead of us on whatever came after what we're interested in). */ if (isnull && _state->normalize_results) _state->tresult = (text *) NULL; else { char *start = _state->result_start; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } /* this should be unnecessary but let's do it for cleanliness: */ _state->result_start = NULL; } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,062
get_object_field_start(void *state, char *fname, bool isnull) { GetState *_state = (GetState *) state; bool get_next = false; int lex_level = _state->lex->lex_level; if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_names != NULL && _state->path_names[lex_level - 1] != NULL && strcmp(fname, _state->path_names[lex_level - 1]) == 0) { if (lex_level < _state->npath) { /* if not at end of path just mark path ok */ _state->pathok[lex_level] = true; } else { /* end of path, so we want this value */ get_next = true; } } if (get_next) { /* this object overrides any previous matching object */ _state->tresult = NULL; _state->result_start = NULL; if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) { /* for as_text variants, tell get_scalar to set it for us */ _state->next_scalar = true; } else { /* for non-as_text variants, just note the json starting point */ _state->result_start = _state->lex->token_start; } } }
DoS Overflow
0
get_object_field_start(void *state, char *fname, bool isnull) { GetState *_state = (GetState *) state; bool get_next = false; int lex_level = _state->lex->lex_level; if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_names != NULL && _state->path_names[lex_level - 1] != NULL && strcmp(fname, _state->path_names[lex_level - 1]) == 0) { if (lex_level < _state->npath) { /* if not at end of path just mark path ok */ _state->pathok[lex_level] = true; } else { /* end of path, so we want this value */ get_next = true; } } if (get_next) { /* this object overrides any previous matching object */ _state->tresult = NULL; _state->result_start = NULL; if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING) { /* for as_text variants, tell get_scalar to set it for us */ _state->next_scalar = true; } else { /* for non-as_text variants, just note the json starting point */ _state->result_start = _state->lex->token_start; } } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,063
get_path_all(FunctionCallInfo fcinfo, bool as_text) { text *json = PG_GETARG_TEXT_P(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); text *result; Datum *pathtext; bool *pathnulls; int npath; char **tpath; int *ipath; int i; /* * If the array contains any null elements, return NULL, on the grounds * that you'd have gotten NULL if any RHS value were NULL in a nested * series of applications of the -> operator. (Note: because we also * return NULL for error cases such as no-such-field, this is true * regardless of the contents of the rest of the array.) */ if (array_contains_nulls(path)) PG_RETURN_NULL(); deconstruct_array(path, TEXTOID, -1, false, 'i', &pathtext, &pathnulls, &npath); tpath = palloc(npath * sizeof(char *)); ipath = palloc(npath * sizeof(int)); for (i = 0; i < npath; i++) { Assert(!pathnulls[i]); tpath[i] = TextDatumGetCString(pathtext[i]); /* * we have no idea at this stage what structure the document is so * just convert anything in the path that we can to an integer and set * all the other integers to INT_MIN which will never match. */ if (*tpath[i] != '\0') { long ind; char *endptr; errno = 0; ind = strtol(tpath[i], &endptr, 10); if (*endptr == '\0' && errno == 0 && ind <= INT_MAX && ind >= INT_MIN) ipath[i] = (int) ind; else ipath[i] = INT_MIN; } else ipath[i] = INT_MIN; } result = get_worker(json, tpath, ipath, npath, as_text); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
DoS Overflow
0
get_path_all(FunctionCallInfo fcinfo, bool as_text) { text *json = PG_GETARG_TEXT_P(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); text *result; Datum *pathtext; bool *pathnulls; int npath; char **tpath; int *ipath; int i; /* * If the array contains any null elements, return NULL, on the grounds * that you'd have gotten NULL if any RHS value were NULL in a nested * series of applications of the -> operator. (Note: because we also * return NULL for error cases such as no-such-field, this is true * regardless of the contents of the rest of the array.) */ if (array_contains_nulls(path)) PG_RETURN_NULL(); deconstruct_array(path, TEXTOID, -1, false, 'i', &pathtext, &pathnulls, &npath); tpath = palloc(npath * sizeof(char *)); ipath = palloc(npath * sizeof(int)); for (i = 0; i < npath; i++) { Assert(!pathnulls[i]); tpath[i] = TextDatumGetCString(pathtext[i]); /* * we have no idea at this stage what structure the document is so * just convert anything in the path that we can to an integer and set * all the other integers to INT_MIN which will never match. */ if (*tpath[i] != '\0') { long ind; char *endptr; errno = 0; ind = strtol(tpath[i], &endptr, 10); if (*endptr == '\0' && errno == 0 && ind <= INT_MAX && ind >= INT_MIN) ipath[i] = (int) ind; else ipath[i] = INT_MIN; } else ipath[i] = INT_MIN; } result = get_worker(json, tpath, ipath, npath, as_text); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,064
get_scalar(void *state, char *token, JsonTokenType tokentype) { GetState *_state = (GetState *) state; int lex_level = _state->lex->lex_level; /* Check for whole-object match */ if (lex_level == 0 && _state->npath == 0) { if (_state->normalize_results && tokentype == JSON_TOKEN_STRING) { /* we want the de-escaped string */ _state->next_scalar = true; } else if (_state->normalize_results && tokentype == JSON_TOKEN_NULL) { _state->tresult = (text *) NULL; } else { /* * This is a bit hokey: we will suppress whitespace after the * scalar token, but not whitespace before it. Probably not worth * doing our own space-skipping to avoid that. */ char *start = _state->lex->input; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } } if (_state->next_scalar) { /* a de-escaped text value is wanted, so supply it */ _state->tresult = cstring_to_text(token); /* make sure the next call to get_scalar doesn't overwrite it */ _state->next_scalar = false; } }
DoS Overflow
0
get_scalar(void *state, char *token, JsonTokenType tokentype) { GetState *_state = (GetState *) state; int lex_level = _state->lex->lex_level; /* Check for whole-object match */ if (lex_level == 0 && _state->npath == 0) { if (_state->normalize_results && tokentype == JSON_TOKEN_STRING) { /* we want the de-escaped string */ _state->next_scalar = true; } else if (_state->normalize_results && tokentype == JSON_TOKEN_NULL) { _state->tresult = (text *) NULL; } else { /* * This is a bit hokey: we will suppress whitespace after the * scalar token, but not whitespace before it. Probably not worth * doing our own space-skipping to avoid that. */ char *start = _state->lex->input; int len = _state->lex->prev_token_terminator - start; _state->tresult = cstring_to_text_with_len(start, len); } } if (_state->next_scalar) { /* a de-escaped text value is wanted, so supply it */ _state->tresult = cstring_to_text(token); /* make sure the next call to get_scalar doesn't overwrite it */ _state->next_scalar = false; } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,065
get_worker(text *json, char **tpath, int *ipath, int npath, bool normalize_results) { JsonLexContext *lex = makeJsonLexContext(json, true); JsonSemAction *sem = palloc0(sizeof(JsonSemAction)); GetState *state = palloc0(sizeof(GetState)); Assert(npath >= 0); state->lex = lex; /* is it "_as_text" variant? */ state->normalize_results = normalize_results; state->npath = npath; state->path_names = tpath; state->path_indexes = ipath; state->pathok = palloc0(sizeof(bool) * npath); state->array_cur_index = palloc(sizeof(int) * npath); if (npath > 0) state->pathok[0] = true; sem->semstate = (void *) state; /* * Not all variants need all the semantic routines. Only set the ones that * are actually needed for maximum efficiency. */ sem->scalar = get_scalar; if (npath == 0) { sem->object_start = get_object_start; sem->object_end = get_object_end; sem->array_start = get_array_start; sem->array_end = get_array_end; } if (tpath != NULL) { sem->object_field_start = get_object_field_start; sem->object_field_end = get_object_field_end; } if (ipath != NULL) { sem->array_start = get_array_start; sem->array_element_start = get_array_element_start; sem->array_element_end = get_array_element_end; } pg_parse_json(lex, sem); return state->tresult; }
DoS Overflow
0
get_worker(text *json, char **tpath, int *ipath, int npath, bool normalize_results) { JsonLexContext *lex = makeJsonLexContext(json, true); JsonSemAction *sem = palloc0(sizeof(JsonSemAction)); GetState *state = palloc0(sizeof(GetState)); Assert(npath >= 0); state->lex = lex; /* is it "_as_text" variant? */ state->normalize_results = normalize_results; state->npath = npath; state->path_names = tpath; state->path_indexes = ipath; state->pathok = palloc0(sizeof(bool) * npath); state->array_cur_index = palloc(sizeof(int) * npath); if (npath > 0) state->pathok[0] = true; sem->semstate = (void *) state; /* * Not all variants need all the semantic routines. Only set the ones that * are actually needed for maximum efficiency. */ sem->scalar = get_scalar; if (npath == 0) { sem->object_start = get_object_start; sem->object_end = get_object_end; sem->array_start = get_array_start; sem->array_end = get_array_end; } if (tpath != NULL) { sem->object_field_start = get_object_field_start; sem->object_field_end = get_object_field_end; } if (ipath != NULL) { sem->array_start = get_array_start; sem->array_element_start = get_array_element_start; sem->array_element_end = get_array_element_end; } pg_parse_json(lex, sem); return state->tresult; }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,066
hash_object_field_end(void *state, char *fname, bool isnull) { JHashState *_state = (JHashState *) state; JsonHashEntry *hashentry; bool found; /* * Ignore nested fields. */ if (_state->lex->lex_level > 2) return; /* * Ignore field names >= NAMEDATALEN - they can't match a record field. * (Note: without this test, the hash code would truncate the string at * NAMEDATALEN-1, and could then match against a similarly-truncated * record field name. That would be a reasonable behavior, but this code * has previously insisted on exact equality, so we keep this behavior.) */ if (strlen(fname) >= NAMEDATALEN) return; hashentry = hash_search(_state->hash, fname, HASH_ENTER, &found); /* * found being true indicates a duplicate. We don't do anything about * that, a later field with the same name overrides the earlier field. */ hashentry->isnull = isnull; if (_state->save_json_start != NULL) { int len = _state->lex->prev_token_terminator - _state->save_json_start; char *val = palloc((len + 1) * sizeof(char)); memcpy(val, _state->save_json_start, len); val[len] = '\0'; hashentry->val = val; } else { /* must have had a scalar instead */ hashentry->val = _state->saved_scalar; } }
DoS Overflow
0
hash_object_field_end(void *state, char *fname, bool isnull) { JHashState *_state = (JHashState *) state; JsonHashEntry *hashentry; bool found; /* * Ignore nested fields. */ if (_state->lex->lex_level > 2) return; /* * Ignore field names >= NAMEDATALEN - they can't match a record field. * (Note: without this test, the hash code would truncate the string at * NAMEDATALEN-1, and could then match against a similarly-truncated * record field name. That would be a reasonable behavior, but this code * has previously insisted on exact equality, so we keep this behavior.) */ if (strlen(fname) >= NAMEDATALEN) return; hashentry = hash_search(_state->hash, fname, HASH_ENTER, &found); /* * found being true indicates a duplicate. We don't do anything about * that, a later field with the same name overrides the earlier field. */ hashentry->isnull = isnull; if (_state->save_json_start != NULL) { int len = _state->lex->prev_token_terminator - _state->save_json_start; char *val = palloc((len + 1) * sizeof(char)); memcpy(val, _state->save_json_start, len); val[len] = '\0'; hashentry->val = val; } else { /* must have had a scalar instead */ hashentry->val = _state->saved_scalar; } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,067
hash_object_field_start(void *state, char *fname, bool isnull) { JHashState *_state = (JHashState *) state; if (_state->lex->lex_level > 1) return; if (_state->lex->token_type == JSON_TOKEN_ARRAY_START || _state->lex->token_type == JSON_TOKEN_OBJECT_START) { /* remember start position of the whole text of the subobject */ _state->save_json_start = _state->lex->token_start; } else { /* must be a scalar */ _state->save_json_start = NULL; } }
DoS Overflow
0
hash_object_field_start(void *state, char *fname, bool isnull) { JHashState *_state = (JHashState *) state; if (_state->lex->lex_level > 1) return; if (_state->lex->token_type == JSON_TOKEN_ARRAY_START || _state->lex->token_type == JSON_TOKEN_OBJECT_START) { /* remember start position of the whole text of the subobject */ _state->save_json_start = _state->lex->token_start; } else { /* must be a scalar */ _state->save_json_start = NULL; } }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,068
json_array_element(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); int element = PG_GETARG_INT32(1); text *result; result = get_worker(json, NULL, &element, 1, false); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
DoS Overflow
0
json_array_element(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); int element = PG_GETARG_INT32(1); text *result; result = get_worker(json, NULL, &element, 1, false); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,069
json_array_element_text(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); int element = PG_GETARG_INT32(1); text *result; result = get_worker(json, NULL, &element, 1, true); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
DoS Overflow
0
json_array_element_text(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); int element = PG_GETARG_INT32(1); text *result; result = get_worker(json, NULL, &element, 1, true); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,070
json_array_elements(PG_FUNCTION_ARGS) { return elements_worker(fcinfo, "json_array_elements", false); }
DoS Overflow
0
json_array_elements(PG_FUNCTION_ARGS) { return elements_worker(fcinfo, "json_array_elements", false); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,071
json_array_elements_text(PG_FUNCTION_ARGS) { return elements_worker(fcinfo, "json_array_elements_text", true); }
DoS Overflow
0
json_array_elements_text(PG_FUNCTION_ARGS) { return elements_worker(fcinfo, "json_array_elements_text", true); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,072
json_array_length(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); AlenState *state; JsonLexContext *lex; JsonSemAction *sem; lex = makeJsonLexContext(json, false); state = palloc0(sizeof(AlenState)); sem = palloc0(sizeof(JsonSemAction)); /* palloc0 does this for us */ #if 0 state->count = 0; #endif state->lex = lex; sem->semstate = (void *) state; sem->object_start = alen_object_start; sem->scalar = alen_scalar; sem->array_element_start = alen_array_element_start; pg_parse_json(lex, sem); PG_RETURN_INT32(state->count); }
DoS Overflow
0
json_array_length(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); AlenState *state; JsonLexContext *lex; JsonSemAction *sem; lex = makeJsonLexContext(json, false); state = palloc0(sizeof(AlenState)); sem = palloc0(sizeof(JsonSemAction)); /* palloc0 does this for us */ #if 0 state->count = 0; #endif state->lex = lex; sem->semstate = (void *) state; sem->object_start = alen_object_start; sem->scalar = alen_scalar; sem->array_element_start = alen_array_element_start; pg_parse_json(lex, sem); PG_RETURN_INT32(state->count); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,073
json_each(PG_FUNCTION_ARGS) { return each_worker(fcinfo, false); }
DoS Overflow
0
json_each(PG_FUNCTION_ARGS) { return each_worker(fcinfo, false); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,074
json_each_text(PG_FUNCTION_ARGS) { return each_worker(fcinfo, true); }
DoS Overflow
0
json_each_text(PG_FUNCTION_ARGS) { return each_worker(fcinfo, true); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,075
json_extract_path(PG_FUNCTION_ARGS) { return get_path_all(fcinfo, false); }
DoS Overflow
0
json_extract_path(PG_FUNCTION_ARGS) { return get_path_all(fcinfo, false); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,076
json_extract_path_text(PG_FUNCTION_ARGS) { return get_path_all(fcinfo, true); }
DoS Overflow
0
json_extract_path_text(PG_FUNCTION_ARGS) { return get_path_all(fcinfo, true); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,077
json_object_field(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); text *fname = PG_GETARG_TEXT_PP(1); char *fnamestr = text_to_cstring(fname); text *result; result = get_worker(json, &fnamestr, NULL, 1, false); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
DoS Overflow
0
json_object_field(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); text *fname = PG_GETARG_TEXT_PP(1); char *fnamestr = text_to_cstring(fname); text *result; result = get_worker(json, &fnamestr, NULL, 1, false); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,078
json_object_field_text(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); text *fname = PG_GETARG_TEXT_PP(1); char *fnamestr = text_to_cstring(fname); text *result; result = get_worker(json, &fnamestr, NULL, 1, true); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
DoS Overflow
0
json_object_field_text(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); text *fname = PG_GETARG_TEXT_PP(1); char *fnamestr = text_to_cstring(fname); text *result; result = get_worker(json, &fnamestr, NULL, 1, true); if (result != NULL) PG_RETURN_TEXT_P(result); else PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,079
json_object_keys(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; OkeysState *state; int i; if (SRF_IS_FIRSTCALL()) { text *json = PG_GETARG_TEXT_P(0); JsonLexContext *lex = makeJsonLexContext(json, true); JsonSemAction *sem; MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); state = palloc(sizeof(OkeysState)); sem = palloc0(sizeof(JsonSemAction)); state->lex = lex; state->result_size = 256; state->result_count = 0; state->sent_count = 0; state->result = palloc(256 * sizeof(char *)); sem->semstate = (void *) state; sem->array_start = okeys_array_start; sem->scalar = okeys_scalar; sem->object_field_start = okeys_object_field_start; /* remainder are all NULL, courtesy of palloc0 above */ pg_parse_json(lex, sem); /* keys are now in state->result */ pfree(lex->strval->data); pfree(lex->strval); pfree(lex); pfree(sem); MemoryContextSwitchTo(oldcontext); funcctx->user_fctx = (void *) state; } funcctx = SRF_PERCALL_SETUP(); state = (OkeysState *) funcctx->user_fctx; if (state->sent_count < state->result_count) { char *nxt = state->result[state->sent_count++]; SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt)); } /* cleanup to reduce or eliminate memory leaks */ for (i = 0; i < state->result_count; i++) pfree(state->result[i]); pfree(state->result); pfree(state); SRF_RETURN_DONE(funcctx); }
DoS Overflow
0
json_object_keys(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; OkeysState *state; int i; if (SRF_IS_FIRSTCALL()) { text *json = PG_GETARG_TEXT_P(0); JsonLexContext *lex = makeJsonLexContext(json, true); JsonSemAction *sem; MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); state = palloc(sizeof(OkeysState)); sem = palloc0(sizeof(JsonSemAction)); state->lex = lex; state->result_size = 256; state->result_count = 0; state->sent_count = 0; state->result = palloc(256 * sizeof(char *)); sem->semstate = (void *) state; sem->array_start = okeys_array_start; sem->scalar = okeys_scalar; sem->object_field_start = okeys_object_field_start; /* remainder are all NULL, courtesy of palloc0 above */ pg_parse_json(lex, sem); /* keys are now in state->result */ pfree(lex->strval->data); pfree(lex->strval); pfree(lex); pfree(sem); MemoryContextSwitchTo(oldcontext); funcctx->user_fctx = (void *) state; } funcctx = SRF_PERCALL_SETUP(); state = (OkeysState *) funcctx->user_fctx; if (state->sent_count < state->result_count) { char *nxt = state->result[state->sent_count++]; SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt)); } /* cleanup to reduce or eliminate memory leaks */ for (i = 0; i < state->result_count; i++) pfree(state->result[i]); pfree(state->result); pfree(state); SRF_RETURN_DONE(funcctx); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,080
json_populate_record(PG_FUNCTION_ARGS) { return populate_record_worker(fcinfo, "json_populate_record", true); }
DoS Overflow
0
json_populate_record(PG_FUNCTION_ARGS) { return populate_record_worker(fcinfo, "json_populate_record", true); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,081
json_strip_nulls(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); StripnullState *state; JsonLexContext *lex; JsonSemAction *sem; lex = makeJsonLexContext(json, true); state = palloc0(sizeof(StripnullState)); sem = palloc0(sizeof(JsonSemAction)); state->strval = makeStringInfo(); state->skip_next_null = false; state->lex = lex; sem->semstate = (void *) state; sem->object_start = sn_object_start; sem->object_end = sn_object_end; sem->array_start = sn_array_start; sem->array_end = sn_array_end; sem->scalar = sn_scalar; sem->array_element_start = sn_array_element_start; sem->object_field_start = sn_object_field_start; pg_parse_json(lex, sem); PG_RETURN_TEXT_P(cstring_to_text_with_len(state->strval->data, state->strval->len)); }
DoS Overflow
0
json_strip_nulls(PG_FUNCTION_ARGS) { text *json = PG_GETARG_TEXT_P(0); StripnullState *state; JsonLexContext *lex; JsonSemAction *sem; lex = makeJsonLexContext(json, true); state = palloc0(sizeof(StripnullState)); sem = palloc0(sizeof(JsonSemAction)); state->strval = makeStringInfo(); state->skip_next_null = false; state->lex = lex; sem->semstate = (void *) state; sem->object_start = sn_object_start; sem->object_end = sn_object_end; sem->array_start = sn_array_start; sem->array_end = sn_array_end; sem->scalar = sn_scalar; sem->array_element_start = sn_array_element_start; sem->object_field_start = sn_object_field_start; pg_parse_json(lex, sem); PG_RETURN_TEXT_P(cstring_to_text_with_len(state->strval->data, state->strval->len)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,082
json_to_record(PG_FUNCTION_ARGS) { return populate_record_worker(fcinfo, "json_to_record", false); }
DoS Overflow
0
json_to_record(PG_FUNCTION_ARGS) { return populate_record_worker(fcinfo, "json_to_record", false); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,083
json_to_recordset(PG_FUNCTION_ARGS) { return populate_recordset_worker(fcinfo, "json_to_recordset", false); }
DoS Overflow
0
json_to_recordset(PG_FUNCTION_ARGS) { return populate_recordset_worker(fcinfo, "json_to_recordset", false); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,084
jsonb_array_element(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); int element = PG_GETARG_INT32(1); JsonbValue *v; if (!JB_ROOT_IS_ARRAY(jb)) PG_RETURN_NULL(); /* Handle negative subscript */ if (element < 0) { uint32 nelements = JB_ROOT_COUNT(jb); if (-element > nelements) PG_RETURN_NULL(); else element += nelements; } v = getIthJsonbValueFromContainer(&jb->root, element); if (v != NULL) PG_RETURN_JSONB(JsonbValueToJsonb(v)); PG_RETURN_NULL(); }
DoS Overflow
0
jsonb_array_element(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); int element = PG_GETARG_INT32(1); JsonbValue *v; if (!JB_ROOT_IS_ARRAY(jb)) PG_RETURN_NULL(); /* Handle negative subscript */ if (element < 0) { uint32 nelements = JB_ROOT_COUNT(jb); if (-element > nelements) PG_RETURN_NULL(); else element += nelements; } v = getIthJsonbValueFromContainer(&jb->root, element); if (v != NULL) PG_RETURN_JSONB(JsonbValueToJsonb(v)); PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,085
jsonb_array_elements_text(PG_FUNCTION_ARGS) { return elements_worker_jsonb(fcinfo, "jsonb_array_elements_text", true); }
DoS Overflow
0
jsonb_array_elements_text(PG_FUNCTION_ARGS) { return elements_worker_jsonb(fcinfo, "jsonb_array_elements_text", true); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,086
jsonb_array_length(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); if (JB_ROOT_IS_SCALAR(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a scalar"))); else if (!JB_ROOT_IS_ARRAY(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a non-array"))); PG_RETURN_INT32(JB_ROOT_COUNT(jb)); }
DoS Overflow
0
jsonb_array_length(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); if (JB_ROOT_IS_SCALAR(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a scalar"))); else if (!JB_ROOT_IS_ARRAY(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot get array length of a non-array"))); PG_RETURN_INT32(JB_ROOT_COUNT(jb)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,087
jsonb_concat(PG_FUNCTION_ARGS) { Jsonb *jb1 = PG_GETARG_JSONB(0); Jsonb *jb2 = PG_GETARG_JSONB(1); JsonbParseState *state = NULL; JsonbValue *res; JsonbIterator *it1, *it2; /* * If one of the jsonb is empty, just return the other if it's not * scalar and both are of the same kind. If it's a scalar or they are * of different kinds we need to perform the concatenation even if one is * empty. */ if (JB_ROOT_IS_OBJECT(jb1) == JB_ROOT_IS_OBJECT(jb2)) { if (JB_ROOT_COUNT(jb1) == 0 && !JB_ROOT_IS_SCALAR(jb2)) PG_RETURN_JSONB(jb2); else if (JB_ROOT_COUNT(jb2) == 0 && !JB_ROOT_IS_SCALAR(jb1)) PG_RETURN_JSONB(jb1); } it1 = JsonbIteratorInit(&jb1->root); it2 = JsonbIteratorInit(&jb2->root); res = IteratorConcat(&it1, &it2, &state); Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
DoS Overflow
0
jsonb_concat(PG_FUNCTION_ARGS) { Jsonb *jb1 = PG_GETARG_JSONB(0); Jsonb *jb2 = PG_GETARG_JSONB(1); JsonbParseState *state = NULL; JsonbValue *res; JsonbIterator *it1, *it2; /* * If one of the jsonb is empty, just return the other if it's not * scalar and both are of the same kind. If it's a scalar or they are * of different kinds we need to perform the concatenation even if one is * empty. */ if (JB_ROOT_IS_OBJECT(jb1) == JB_ROOT_IS_OBJECT(jb2)) { if (JB_ROOT_COUNT(jb1) == 0 && !JB_ROOT_IS_SCALAR(jb2)) PG_RETURN_JSONB(jb2); else if (JB_ROOT_COUNT(jb2) == 0 && !JB_ROOT_IS_SCALAR(jb1)) PG_RETURN_JSONB(jb1); } it1 = JsonbIteratorInit(&jb1->root); it2 = JsonbIteratorInit(&jb2->root); res = IteratorConcat(&it1, &it2, &state); Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,088
jsonb_delete(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); text *key = PG_GETARG_TEXT_PP(1); char *keyptr = VARDATA_ANY(key); int keylen = VARSIZE_ANY_EXHDR(key); JsonbParseState *state = NULL; JsonbIterator *it; uint32 r; JsonbValue v, *res = NULL; bool skipNested = false; if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete from scalar"))); if (JB_ROOT_COUNT(in) == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0) { skipNested = true; if ((r == WJB_ELEM || r == WJB_KEY) && (v.type == jbvString && keylen == v.val.string.len && memcmp(keyptr, v.val.string.val, keylen) == 0)) { /* skip corresponding value as well */ if (r == WJB_KEY) JsonbIteratorNext(&it, &v, true); continue; } res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL); } Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
DoS Overflow
0
jsonb_delete(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); text *key = PG_GETARG_TEXT_PP(1); char *keyptr = VARDATA_ANY(key); int keylen = VARSIZE_ANY_EXHDR(key); JsonbParseState *state = NULL; JsonbIterator *it; uint32 r; JsonbValue v, *res = NULL; bool skipNested = false; if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete from scalar"))); if (JB_ROOT_COUNT(in) == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0) { skipNested = true; if ((r == WJB_ELEM || r == WJB_KEY) && (v.type == jbvString && keylen == v.val.string.len && memcmp(keyptr, v.val.string.val, keylen) == 0)) { /* skip corresponding value as well */ if (r == WJB_KEY) JsonbIteratorNext(&it, &v, true); continue; } res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL); } Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,089
jsonb_delete_idx(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); int idx = PG_GETARG_INT32(1); JsonbParseState *state = NULL; JsonbIterator *it; uint32 r, i = 0, n; JsonbValue v, *res = NULL; if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete from scalar"))); if (JB_ROOT_IS_OBJECT(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete from object using integer subscript"))); if (JB_ROOT_COUNT(in) == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); r = JsonbIteratorNext(&it, &v, false); Assert (r == WJB_BEGIN_ARRAY); n = v.val.array.nElems; if (idx < 0) { if (-idx > n) idx = n; else idx = n + idx; } if (idx >= n) PG_RETURN_JSONB(in); pushJsonbValue(&state, r, NULL); while ((r = JsonbIteratorNext(&it, &v, true)) != 0) { if (r == WJB_ELEM) { if (i++ == idx) continue; } res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL); } Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
DoS Overflow
0
jsonb_delete_idx(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); int idx = PG_GETARG_INT32(1); JsonbParseState *state = NULL; JsonbIterator *it; uint32 r, i = 0, n; JsonbValue v, *res = NULL; if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete from scalar"))); if (JB_ROOT_IS_OBJECT(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete from object using integer subscript"))); if (JB_ROOT_COUNT(in) == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); r = JsonbIteratorNext(&it, &v, false); Assert (r == WJB_BEGIN_ARRAY); n = v.val.array.nElems; if (idx < 0) { if (-idx > n) idx = n; else idx = n + idx; } if (idx >= n) PG_RETURN_JSONB(in); pushJsonbValue(&state, r, NULL); while ((r = JsonbIteratorNext(&it, &v, true)) != 0) { if (r == WJB_ELEM) { if (i++ == idx) continue; } res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL); } Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,090
jsonb_delete_path(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); JsonbValue *res = NULL; Datum *path_elems; bool *path_nulls; int path_len; JsonbIterator *it; JsonbParseState *st = NULL; if (ARR_NDIM(path) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete path in scalar"))); if (JB_ROOT_COUNT(in) == 0) PG_RETURN_JSONB(in); deconstruct_array(path, TEXTOID, -1, false, 'i', &path_elems, &path_nulls, &path_len); if (path_len == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, NULL, false); Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
DoS Overflow
0
jsonb_delete_path(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); JsonbValue *res = NULL; Datum *path_elems; bool *path_nulls; int path_len; JsonbIterator *it; JsonbParseState *st = NULL; if (ARR_NDIM(path) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot delete path in scalar"))); if (JB_ROOT_COUNT(in) == 0) PG_RETURN_JSONB(in); deconstruct_array(path, TEXTOID, -1, false, 'i', &path_elems, &path_nulls, &path_len); if (path_len == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, NULL, false); Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,091
jsonb_each(PG_FUNCTION_ARGS) { return each_worker_jsonb(fcinfo, "jsonb_each", false); }
DoS Overflow
0
jsonb_each(PG_FUNCTION_ARGS) { return each_worker_jsonb(fcinfo, "jsonb_each", false); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,092
jsonb_extract_path_text(PG_FUNCTION_ARGS) { return get_jsonb_path_all(fcinfo, true); }
DoS Overflow
0
jsonb_extract_path_text(PG_FUNCTION_ARGS) { return get_jsonb_path_all(fcinfo, true); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,093
jsonb_object_field(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); text *key = PG_GETARG_TEXT_PP(1); JsonbValue *v; if (!JB_ROOT_IS_OBJECT(jb)) PG_RETURN_NULL(); v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT, VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); if (v != NULL) PG_RETURN_JSONB(JsonbValueToJsonb(v)); PG_RETURN_NULL(); }
DoS Overflow
0
jsonb_object_field(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); text *key = PG_GETARG_TEXT_PP(1); JsonbValue *v; if (!JB_ROOT_IS_OBJECT(jb)) PG_RETURN_NULL(); v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT, VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); if (v != NULL) PG_RETURN_JSONB(JsonbValueToJsonb(v)); PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,094
jsonb_object_field_text(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); text *key = PG_GETARG_TEXT_PP(1); JsonbValue *v; if (!JB_ROOT_IS_OBJECT(jb)) PG_RETURN_NULL(); v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT, VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); if (v != NULL) { text *result = NULL; switch (v->type) { case jbvNull: break; case jbvBool: result = cstring_to_text(v->val.boolean ? "true" : "false"); break; case jbvString: result = cstring_to_text_with_len(v->val.string.val, v->val.string.len); break; case jbvNumeric: result = cstring_to_text(DatumGetCString(DirectFunctionCall1(numeric_out, PointerGetDatum(v->val.numeric)))); break; case jbvBinary: { StringInfo jtext = makeStringInfo(); (void) JsonbToCString(jtext, v->val.binary.data, -1); result = cstring_to_text_with_len(jtext->data, jtext->len); } break; default: elog(ERROR, "unrecognized jsonb type: %d", (int) v->type); } if (result) PG_RETURN_TEXT_P(result); } PG_RETURN_NULL(); }
DoS Overflow
0
jsonb_object_field_text(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); text *key = PG_GETARG_TEXT_PP(1); JsonbValue *v; if (!JB_ROOT_IS_OBJECT(jb)) PG_RETURN_NULL(); v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT, VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); if (v != NULL) { text *result = NULL; switch (v->type) { case jbvNull: break; case jbvBool: result = cstring_to_text(v->val.boolean ? "true" : "false"); break; case jbvString: result = cstring_to_text_with_len(v->val.string.val, v->val.string.len); break; case jbvNumeric: result = cstring_to_text(DatumGetCString(DirectFunctionCall1(numeric_out, PointerGetDatum(v->val.numeric)))); break; case jbvBinary: { StringInfo jtext = makeStringInfo(); (void) JsonbToCString(jtext, v->val.binary.data, -1); result = cstring_to_text_with_len(jtext->data, jtext->len); } break; default: elog(ERROR, "unrecognized jsonb type: %d", (int) v->type); } if (result) PG_RETURN_TEXT_P(result); } PG_RETURN_NULL(); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,095
jsonb_object_keys(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; OkeysState *state; int i; if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; Jsonb *jb = PG_GETARG_JSONB(0); bool skipNested = false; JsonbIterator *it; JsonbValue v; int r; if (JB_ROOT_IS_SCALAR(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on a scalar", "jsonb_object_keys"))); else if (JB_ROOT_IS_ARRAY(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on an array", "jsonb_object_keys"))); funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); state = palloc(sizeof(OkeysState)); state->result_size = JB_ROOT_COUNT(jb); state->result_count = 0; state->sent_count = 0; state->result = palloc(state->result_size * sizeof(char *)); it = JsonbIteratorInit(&jb->root); while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE) { skipNested = true; if (r == WJB_KEY) { char *cstr; cstr = palloc(v.val.string.len + 1 * sizeof(char)); memcpy(cstr, v.val.string.val, v.val.string.len); cstr[v.val.string.len] = '\0'; state->result[state->result_count++] = cstr; } } MemoryContextSwitchTo(oldcontext); funcctx->user_fctx = (void *) state; } funcctx = SRF_PERCALL_SETUP(); state = (OkeysState *) funcctx->user_fctx; if (state->sent_count < state->result_count) { char *nxt = state->result[state->sent_count++]; SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt)); } /* cleanup to reduce or eliminate memory leaks */ for (i = 0; i < state->result_count; i++) pfree(state->result[i]); pfree(state->result); pfree(state); SRF_RETURN_DONE(funcctx); }
DoS Overflow
0
jsonb_object_keys(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; OkeysState *state; int i; if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; Jsonb *jb = PG_GETARG_JSONB(0); bool skipNested = false; JsonbIterator *it; JsonbValue v; int r; if (JB_ROOT_IS_SCALAR(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on a scalar", "jsonb_object_keys"))); else if (JB_ROOT_IS_ARRAY(jb)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot call %s on an array", "jsonb_object_keys"))); funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); state = palloc(sizeof(OkeysState)); state->result_size = JB_ROOT_COUNT(jb); state->result_count = 0; state->sent_count = 0; state->result = palloc(state->result_size * sizeof(char *)); it = JsonbIteratorInit(&jb->root); while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE) { skipNested = true; if (r == WJB_KEY) { char *cstr; cstr = palloc(v.val.string.len + 1 * sizeof(char)); memcpy(cstr, v.val.string.val, v.val.string.len); cstr[v.val.string.len] = '\0'; state->result[state->result_count++] = cstr; } } MemoryContextSwitchTo(oldcontext); funcctx->user_fctx = (void *) state; } funcctx = SRF_PERCALL_SETUP(); state = (OkeysState *) funcctx->user_fctx; if (state->sent_count < state->result_count) { char *nxt = state->result[state->sent_count++]; SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt)); } /* cleanup to reduce or eliminate memory leaks */ for (i = 0; i < state->result_count; i++) pfree(state->result[i]); pfree(state->result); pfree(state); SRF_RETURN_DONE(funcctx); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,096
jsonb_populate_record(PG_FUNCTION_ARGS) { return populate_record_worker(fcinfo, "jsonb_populate_record", true); }
DoS Overflow
0
jsonb_populate_record(PG_FUNCTION_ARGS) { return populate_record_worker(fcinfo, "jsonb_populate_record", true); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,097
jsonb_pretty(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); StringInfo str = makeStringInfo(); JsonbToCStringIndent(str, &jb->root, VARSIZE(jb)); PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len)); }
DoS Overflow
0
jsonb_pretty(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); StringInfo str = makeStringInfo(); JsonbToCStringIndent(str, &jb->root, VARSIZE(jb)); PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,098
jsonb_set(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); Jsonb *newval = PG_GETARG_JSONB(2); bool create = PG_GETARG_BOOL(3); JsonbValue *res = NULL; Datum *path_elems; bool *path_nulls; int path_len; JsonbIterator *it; JsonbParseState *st = NULL; if (ARR_NDIM(path) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot set path in scalar"))); if (JB_ROOT_COUNT(in) == 0 && !create) PG_RETURN_JSONB(in); deconstruct_array(path, TEXTOID, -1, false, 'i', &path_elems, &path_nulls, &path_len); if (path_len == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, newval, create); Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
DoS Overflow
0
jsonb_set(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); Jsonb *newval = PG_GETARG_JSONB(2); bool create = PG_GETARG_BOOL(3); JsonbValue *res = NULL; Datum *path_elems; bool *path_nulls; int path_len; JsonbIterator *it; JsonbParseState *st = NULL; if (ARR_NDIM(path) > 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); if (JB_ROOT_IS_SCALAR(in)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("cannot set path in scalar"))); if (JB_ROOT_COUNT(in) == 0 && !create) PG_RETURN_JSONB(in); deconstruct_array(path, TEXTOID, -1, false, 'i', &path_elems, &path_nulls, &path_len); if (path_len == 0) PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, newval, create); Assert(res != NULL); PG_RETURN_JSONB(JsonbValueToJsonb(res)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null
2,099
jsonb_strip_nulls(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); JsonbIterator *it; JsonbParseState *parseState = NULL; JsonbValue *res = NULL; int type; JsonbValue v, k; bool last_was_key = false; if (JB_ROOT_IS_SCALAR(jb)) PG_RETURN_POINTER(jb); it = JsonbIteratorInit(&jb->root); while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) { Assert(!(type == WJB_KEY && last_was_key)); if (type == WJB_KEY) { /* stash the key until we know if it has a null value */ k = v; last_was_key = true; continue; } if (last_was_key) { /* if the last element was a key this one can't be */ last_was_key = false; /* skip this field if value is null */ if (type == WJB_VALUE && v.type == jbvNull) continue; /* otherwise, do a delayed push of the key */ (void) pushJsonbValue(&parseState, WJB_KEY, &k); } if (type == WJB_VALUE || type == WJB_ELEM) res = pushJsonbValue(&parseState, type, &v); else res = pushJsonbValue(&parseState, type, NULL); } Assert(res != NULL); PG_RETURN_POINTER(JsonbValueToJsonb(res)); }
DoS Overflow
0
jsonb_strip_nulls(PG_FUNCTION_ARGS) { Jsonb *jb = PG_GETARG_JSONB(0); JsonbIterator *it; JsonbParseState *parseState = NULL; JsonbValue *res = NULL; int type; JsonbValue v, k; bool last_was_key = false; if (JB_ROOT_IS_SCALAR(jb)) PG_RETURN_POINTER(jb); it = JsonbIteratorInit(&jb->root); while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) { Assert(!(type == WJB_KEY && last_was_key)); if (type == WJB_KEY) { /* stash the key until we know if it has a null value */ k = v; last_was_key = true; continue; } if (last_was_key) { /* if the last element was a key this one can't be */ last_was_key = false; /* skip this field if value is null */ if (type == WJB_VALUE && v.type == jbvNull) continue; /* otherwise, do a delayed push of the key */ (void) pushJsonbValue(&parseState, WJB_KEY, &k); } if (type == WJB_VALUE || type == WJB_ELEM) res = pushJsonbValue(&parseState, type, &v); else res = pushJsonbValue(&parseState, type, NULL); } Assert(res != NULL); PG_RETURN_POINTER(JsonbValueToJsonb(res)); }
@@ -3724,6 +3724,8 @@ setPath(JsonbIterator **it, Datum *path_elems, JsonbValue *res = NULL; int r; + check_stack_depth(); + if (path_nulls[level]) elog(ERROR, "path element at the position %d is NULL", level + 1);
CWE-119
null
null