From: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Date: Mon, 30 Nov 2026 15:09:00 +0500
Subject: [PATCH] Fix json negative subscripts on the selected array

get_array_start() added the first array's length at that depth to
path_indexes[]. A later array then reused the converted index.

Count the length only after the path has reached the array. Store the
resolved index in array_match_index[] and leave path_indexes[] unchanged.

BUG #19956
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reported-by: Yibo Dong <e1638616@u.nus.edu>
---
 src/backend/utils/adt/jsonfuncs.c  | 28 ++++++++++++++++++-------
 src/test/regress/expected/json.out | 43 ++++++++++++++++++++++++++++++++++++++
 src/test/regress/sql/json.sql      |  9 ++++++++
 3 files changed, 72 insertions(+), 8 deletions(-)

diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 25f14f92370..e53c99f1818 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -97,6 +97,7 @@ typedef struct GetState
 	bool	   *pathok;			/* is path matched to current depth? */
 	int		   *array_cur_index;	/* current element index at each path
 									 * level */
+	int		   *array_match_index;	/* subscript compared at this level */
 } GetState;
 
 /* state for json_array_length */
@@ -1122,7 +1123,10 @@ get_worker(text *json,
 	state->array_cur_index = palloc_array(int, npath);
 
 	if (npath > 0)
+	{
 		state->pathok[0] = true;
+		state->array_match_index = palloc_array(int, npath);
+	}
 
 	sem->semstate = state;
 
@@ -1299,23 +1303,31 @@ get_array_start(void *state)
 
 	if (lex_level < _state->npath)
 	{
+		int			idx = _state->path_indexes[lex_level];
+
 		/* Initialize counting of elements in this array */
 		_state->array_cur_index[lex_level] = -1;
+		_state->array_match_index[lex_level] = idx;
 
-		/* INT_MIN value is reserved to represent invalid subscript */
-		if (_state->path_indexes[lex_level] < 0 &&
-			_state->path_indexes[lex_level] != INT_MIN)
+		/*
+		 * Count only an array the path has reached. Leave the subscript in
+		 * path_indexes, so the next array at this depth still sees it. A
+		 * subscript past the end of the input cannot match.
+		 */
+		if (_state->pathok[lex_level] &&
+			idx < 0 && idx != INT_MIN &&
+			(size_t) -idx <= _state->lex->input_length)
 		{
-			/* Negative subscript -- convert to positive-wise subscript */
 			JsonParseErrorType error;
 			int			nelements;
+			int			k = -idx;
 
 			error = json_count_array_elements(_state->lex, &nelements);
 			if (error != JSON_SUCCESS)
 				json_errsave_error(error, _state->lex, NULL);
 
-			if (-_state->path_indexes[lex_level] <= nelements)
-				_state->path_indexes[lex_level] += nelements;
+			if (k <= nelements)
+				_state->array_match_index[lex_level] = nelements - k;
 		}
 	}
 	else if (lex_level == 0 && _state->npath == 0)
@@ -1363,7 +1375,7 @@ get_array_element_start(void *state, bool isnull)
 	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])
+		_state->array_cur_index[lex_level - 1] == _state->array_match_index[lex_level - 1])
 	{
 		if (lex_level < _state->npath)
 		{
@@ -1408,7 +1420,7 @@ get_array_element_end(void *state, bool isnull)
 	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])
+		_state->array_cur_index[lex_level - 1] == _state->array_match_index[lex_level - 1])
 	{
 		if (lex_level < _state->npath)
 		{
diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out
index 461c8781752..9e7b07ea94a 100644
--- a/src/test/regress/expected/json.out
+++ b/src/test/regress/expected/json.out
@@ -1140,6 +1140,49 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
  1
 (1 row)
 
+-- negative subscripts count from the end of the selected array
+select '{"x":[1,2,3],"a":[7,8]}'::json #> '{a,-1}';
+ ?column? 
+----------
+ 8
+(1 row)
+
+select '{"x":[1],"a":[7,8,9]}'::json #> '{a,-1}';
+ ?column? 
+----------
+ 9
+(1 row)
+
+select '[[1,2],[3]]'::json #> '{1,-2}';
+ ?column? 
+----------
+ 
+(1 row)
+
+select '[1,2,3]'::json #> '{-1}';
+ ?column? 
+----------
+ 3
+(1 row)
+
+select '{"a":[1,2,3],"a":[7,8]}'::json #> '{a,-1}';
+ ?column? 
+----------
+ 8
+(1 row)
+
+select '{"a":[{"b":1},{"b":2}]}'::json #> '{a,-1,b}';
+ ?column? 
+----------
+ 2
+(1 row)
+
+select '{"x":[1,2,3],"a":[7,8]}'::json #>> '{a,-1}';
+ ?column? 
+----------
+ 8
+(1 row)
+
 -- corner cases for same
 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
          ?column?          
diff --git a/src/test/regress/sql/json.sql b/src/test/regress/sql/json.sql
index dc8804600a0..f38aac2bd47 100644
--- a/src/test/regress/sql/json.sql
+++ b/src/test/regress/sql/json.sql
@@ -369,6 +369,15 @@ select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
 
+-- negative subscripts count from the end of the selected array
+select '{"x":[1,2,3],"a":[7,8]}'::json #> '{a,-1}';
+select '{"x":[1],"a":[7,8,9]}'::json #> '{a,-1}';
+select '[[1,2],[3]]'::json #> '{1,-2}';
+select '[1,2,3]'::json #> '{-1}';
+select '{"a":[1,2,3],"a":[7,8]}'::json #> '{a,-1}';
+select '{"a":[{"b":1},{"b":2}]}'::json #> '{a,-1,b}';
+select '{"x":[1,2,3],"a":[7,8]}'::json #>> '{a,-1}';
+
 -- corner cases for same
 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
 select '[1,2,3]'::json #> '{}';
