From 233d8b6b89f9277ec6e8b7d5469eb90039f4b431 Mon Sep 17 00:00:00 2001 From: Ishjaap Singh Date: Thu, 3 Dec 2026 07:04:39 +0000 Subject: [PATCH v2] Don't drop a jsonpath word directly followed by a comment When an unquoted word in a jsonpath was directly followed by "/*", the scanner started the comment without returning the word, so the word was silently lost. For example, 'strict/**/$.a' was read as '$.a' in lax mode, and 'abc/**/1' was accepted as '1' instead of failing with a syntax error. Remove the special rule for "/*" after an unquoted word. The rule for a special character after a word then handles "/" like any other delimiter: it returns the word and leaves "/*" to be scanned as a comment in the initial state. --- src/backend/utils/adt/jsonpath_scan.l | 11 +++---- src/test/regress/expected/jsonpath.out | 41 ++++++++++++++++++++++++++ src/test/regress/sql/jsonpath.sql | 9 ++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/backend/utils/adt/jsonpath_scan.l b/src/backend/utils/adt/jsonpath_scan.l index e4fadcc2e69..e045863c148 100644 --- a/src/backend/utils/adt/jsonpath_scan.l +++ b/src/backend/utils/adt/jsonpath_scan.l @@ -129,12 +129,13 @@ hex_fail \\x{hexdigit}{0,1} return checkKeyword(yyscanner); } -\/\* { - yylval->str = yyextra->scanstring; - BEGIN xc; - } - ({special}|\") { + /* + * "/" is in {special}, so this also + * handles a comment right after the + * word: the word is returned, and the + * comment is scanned from INITIAL. + */ yylval->str = yyextra->scanstring; yyless(0); BEGIN INITIAL; diff --git a/src/test/regress/expected/jsonpath.out b/src/test/regress/expected/jsonpath.out index ea971e79854..2dd56a38588 100644 --- a/src/test/regress/expected/jsonpath.out +++ b/src/test/regress/expected/jsonpath.out @@ -1458,6 +1458,47 @@ select '0x_42F'::jsonpath; ERROR: syntax error at end of jsonpath input LINE 1: select '0x_42F'::jsonpath; ^ +-- a comment directly after an unquoted word must not drop the word +select 'strict/**/$.a'::jsonpath; + jsonpath +-------------- + strict $."a" +(1 row) + +select 'strict/* comment */$.a'::jsonpath; + jsonpath +-------------- + strict $."a" +(1 row) + +select '$.a/**/.b'::jsonpath; + jsonpath +----------- + $."a"."b" +(1 row) + +select '$.a/**/'::jsonpath; + jsonpath +---------- + $."a" +(1 row) + +select '$.a.type/**/()'::jsonpath; + jsonpath +-------------- + $."a".type() +(1 row) + +select '$ ? (@ == true/**/)'::jsonpath; + jsonpath +--------------- + $?(@ == true) +(1 row) + +select 'abc/**/1'::jsonpath; +ERROR: syntax error at end of jsonpath input +LINE 1: select 'abc/**/1'::jsonpath; + ^ -- test non-error-throwing API SELECT str as jsonpath, pg_input_is_valid(str,'jsonpath') as ok, diff --git a/src/test/regress/sql/jsonpath.sql b/src/test/regress/sql/jsonpath.sql index 44178d8b45a..8871ca01df1 100644 --- a/src/test/regress/sql/jsonpath.sql +++ b/src/test/regress/sql/jsonpath.sql @@ -292,6 +292,15 @@ select '0o_273'::jsonpath; select '0x_42F'::jsonpath; +-- a comment directly after an unquoted word must not drop the word +select 'strict/**/$.a'::jsonpath; +select 'strict/* comment */$.a'::jsonpath; +select '$.a/**/.b'::jsonpath; +select '$.a/**/'::jsonpath; +select '$.a.type/**/()'::jsonpath; +select '$ ? (@ == true/**/)'::jsonpath; +select 'abc/**/1'::jsonpath; + -- test non-error-throwing API SELECT str as jsonpath, -- 2.43.0