Skip to content

Commit 6d10f4e

Browse files
committed
Only adjust negative indexes in json_get up to the length of the path.
The previous code resulted in memory access beyond the path bounds. The cure is to move it into a code branch that checks the value of lex_level is within the correct bounds. Bug reported and diagnosed by Piotr Stefaniak.
1 parent d8f15c9 commit 6d10f4e

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/backend/utils/adt/jsonfuncs.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -977,27 +977,27 @@ get_array_start(void *state)
977977
{
978978
/* Initialize counting of elements in this array */
979979
_state->array_cur_index[lex_level] = -1;
980+
981+
/* INT_MIN value is reserved to represent invalid subscript */
982+
if (_state->path_indexes[lex_level] < 0 &&
983+
_state->path_indexes[lex_level] != INT_MIN)
984+
{
985+
/* Negative subscript -- convert to positive-wise subscript */
986+
int nelements = json_count_array_elements(_state->lex);
987+
988+
if (-_state->path_indexes[lex_level] <= nelements)
989+
_state->path_indexes[lex_level] += nelements;
990+
}
980991
}
981992
else if (lex_level == 0 && _state->npath == 0)
982993
{
983994
/*
984995
* Special case: we should match the entire array. We only need this
985-
* at outermost level because at nested levels the match will have
986-
* been started by the outer field or array element callback.
996+
* at the outermost level because at nested levels the match will
997+
* have been started by the outer field or array element callback.
987998
*/
988999
_state->result_start = _state->lex->token_start;
9891000
}
990-
991-
/* INT_MIN value is reserved to represent invalid subscript */
992-
if (_state->path_indexes[lex_level] < 0 &&
993-
_state->path_indexes[lex_level] != INT_MIN)
994-
{
995-
/* Negative subscript -- convert to positive-wise subscript */
996-
int nelements = json_count_array_elements(_state->lex);
997-
998-
if (-_state->path_indexes[lex_level] <= nelements)
999-
_state->path_indexes[lex_level] += nelements;
1000-
}
10011001
}
10021002

10031003
static void

0 commit comments

Comments
 (0)