From 767ada45ea9243f42b226431081990363312e121 Mon Sep 17 00:00:00 2001 From: Ishjaap Singh Date: Tue, 24 Nov 2026 14:00:06 +0000 Subject: [PATCH v1] Make jsonpath input report out-of-range numbers softly Since 16, jsonpath input reports invalid input as a soft error, so pg_input_is_valid() and COPY ... ON_ERROR can handle it. But a number that doesn't fit still raised a hard error: SELECT pg_input_is_valid('1e1000000', 'jsonpath'); ERROR: value overflows numeric format SELECT pg_input_is_valid('$.**{2147483648}', 'jsonpath'); ERROR: value "2147483648" is out of range for type integer makeItemNumeric() converted numeric literals with DirectFunctionCall3(numeric_in), and the .**{} level bounds used pg_strtoint32(), so neither reported errors softly. Use DirectInputFunctionCallSafe() and pg_strtoint32_safe() with the parser's escontext, and abort the parse when they fail. Also initialize parsejsonpath()'s result pointer, so it returns NULL rather than an unset value when the parse is aborted. --- src/backend/utils/adt/jsonpath_gram.y | 72 +++++++++++++++++++++----- src/backend/utils/adt/jsonpath_scan.l | 2 +- src/test/regress/expected/jsonpath.out | 10 +++- src/test/regress/sql/jsonpath.sql | 5 +- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/backend/utils/adt/jsonpath_gram.y b/src/backend/utils/adt/jsonpath_gram.y index f826697d098..e003bc31362 100644 --- a/src/backend/utils/adt/jsonpath_gram.y +++ b/src/backend/utils/adt/jsonpath_gram.y @@ -20,6 +20,7 @@ #include "fmgr.h" #include "jsonpath_internal.h" #include "miscadmin.h" +#include "nodes/miscnodes.h" #include "nodes/pg_list.h" #include "regex/regex.h" #include "utils/builtins.h" @@ -28,7 +29,8 @@ static JsonPathParseItem *makeItemType(JsonPathItemType type); static JsonPathParseItem *makeItemString(JsonPathString *s); static JsonPathParseItem *makeItemVariable(JsonPathString *s); static JsonPathParseItem *makeItemKey(JsonPathString *s); -static JsonPathParseItem *makeItemNumeric(JsonPathString *s); +static JsonPathParseItem *makeItemNumeric(JsonPathString *s, + struct Node *escontext); static JsonPathParseItem *makeItemBool(bool val); static JsonPathParseItem *makeItemBinary(JsonPathItemType type, JsonPathParseItem *la, @@ -146,8 +148,18 @@ scalar_value: | NULL_P { $$ = makeItemString(NULL); } | TRUE_P { $$ = makeItemBool(true); } | FALSE_P { $$ = makeItemBool(false); } - | NUMERIC_P { $$ = makeItemNumeric(&$1); } - | INT_P { $$ = makeItemNumeric(&$1); } + | NUMERIC_P + { + $$ = makeItemNumeric(&$1, escontext); + if (!$$) + YYABORT; + } + | INT_P + { + $$ = makeItemNumeric(&$1, escontext); + if (!$$) + YYABORT; + } | VARIABLE_P { $$ = makeItemVariable(&$1); } ; @@ -238,7 +250,12 @@ array_accessor: ; any_level: - INT_P { $$ = pg_strtoint32($1.val); } + INT_P + { + $$ = pg_strtoint32_safe($1.val, escontext); + if (SOFT_ERROR_OCCURRED(escontext)) + YYABORT; + } | LAST_P { $$ = -1; } ; @@ -294,11 +311,27 @@ accessor_op: int_elem: INT_P - { $$ = makeItemNumeric(&$1); } + { + $$ = makeItemNumeric(&$1, escontext); + if (!$$) + YYABORT; + } | '+' INT_P %prec UMINUS - { $$ = makeItemUnary(jpiPlus, makeItemNumeric(&$2)); } + { + JsonPathParseItem *num = makeItemNumeric(&$2, escontext); + + if (!num) + YYABORT; + $$ = makeItemUnary(jpiPlus, num); + } | '-' INT_P %prec UMINUS - { $$ = makeItemUnary(jpiMinus, makeItemNumeric(&$2)); } + { + JsonPathParseItem *num = makeItemNumeric(&$2, escontext); + + if (!num) + YYABORT; + $$ = makeItemUnary(jpiMinus, num); + } ; int_list: @@ -312,7 +345,12 @@ opt_int_list: ; uint_elem: - INT_P { $$ = makeItemNumeric(&$1); } + INT_P + { + $$ = makeItemNumeric(&$1, escontext); + if (!$$) + YYABORT; + } ; opt_uint_arg: @@ -467,17 +505,23 @@ makeItemKey(JsonPathString *s) return v; } +/* + * Returns NULL if the value can't be converted to numeric (e.g. it's out of + * range) and escontext is an ErrorSaveContext; the error is saved there. + */ static JsonPathParseItem * -makeItemNumeric(JsonPathString *s) +makeItemNumeric(JsonPathString *s, struct Node *escontext) { JsonPathParseItem *v; + Datum num; + + if (!DirectInputFunctionCallSafe(numeric_in, s->val, + InvalidOid, -1, + escontext, &num)) + return NULL; v = makeItemType(jpiNumeric); - v->value.numeric = - DatumGetNumeric(DirectFunctionCall3(numeric_in, - CStringGetDatum(s->val), - ObjectIdGetDatum(InvalidOid), - Int32GetDatum(-1))); + v->value.numeric = DatumGetNumeric(num); return v; } diff --git a/src/backend/utils/adt/jsonpath_scan.l b/src/backend/utils/adt/jsonpath_scan.l index e4fadcc2e69..713a851ca62 100644 --- a/src/backend/utils/adt/jsonpath_scan.l +++ b/src/backend/utils/adt/jsonpath_scan.l @@ -535,7 +535,7 @@ addchar(bool init, char c, yyscan_t yyscanner) JsonPathParseResult * parsejsonpath(const char *str, int len, struct Node *escontext) { - JsonPathParseResult *parseresult; + JsonPathParseResult *parseresult = NULL; yyscan_t scanner; struct jsonpath_yy_extra_type yyext; diff --git a/src/test/regress/expected/jsonpath.out b/src/test/regress/expected/jsonpath.out index ea971e79854..b83193a423b 100644 --- a/src/test/regress/expected/jsonpath.out +++ b/src/test/regress/expected/jsonpath.out @@ -1469,7 +1469,10 @@ FROM unnest(ARRAY['$ ? (@ like_regex "pattern" flag "smixq")'::text, '$ ? (@ like_regex "pattern" flag "a")', '@ + 1', '00', - '1a']) str, + '1a', + '1e1000000', + '$ ? (@ > -1e1000000)', + '$.**{2147483648}']) str, LATERAL pg_input_error_info(str, 'jsonpath') as errinfo; jsonpath | ok | sql_error_code | message | detail | hint -------------------------------------------+----+----------------+-----------------------------------------------------------------------+----------------------------------------------------------+------ @@ -1478,5 +1481,8 @@ FROM unnest(ARRAY['$ ? (@ like_regex "pattern" flag "smixq")'::text, @ + 1 | f | 42601 | @ is not allowed in root expressions | | 00 | f | 42601 | trailing junk after numeric literal at or near "00" of jsonpath input | | 1a | f | 42601 | trailing junk after numeric literal at or near "1a" of jsonpath input | | -(5 rows) + 1e1000000 | f | 22003 | value overflows numeric format | | + $ ? (@ > -1e1000000) | f | 22003 | value overflows numeric format | | + $.**{2147483648} | f | 22003 | value "2147483648" is out of range for type integer | | +(8 rows) diff --git a/src/test/regress/sql/jsonpath.sql b/src/test/regress/sql/jsonpath.sql index 44178d8b45a..865f36721a7 100644 --- a/src/test/regress/sql/jsonpath.sql +++ b/src/test/regress/sql/jsonpath.sql @@ -304,5 +304,8 @@ FROM unnest(ARRAY['$ ? (@ like_regex "pattern" flag "smixq")'::text, '$ ? (@ like_regex "pattern" flag "a")', '@ + 1', '00', - '1a']) str, + '1a', + '1e1000000', + '$ ? (@ > -1e1000000)', + '$.**{2147483648}']) str, LATERAL pg_input_error_info(str, 'jsonpath') as errinfo; -- 2.43.0