Commit 2271d002 authored by Andrew Dunstan's avatar Andrew Dunstan

Fix "path" infrastructure bug affecting jsonb_set()

jsonb_set() and other clients of the setPathArray() utility function
could get spurious results when an array integer subscript is provided
that is not within the range of int.

To fix, ensure that the value returned by strtol() within setPathArray()
is within the range of int;  when it isn't, assume an invalid input in
line with existing, similar cases.  The path-orientated operators that
appeared in PostgreSQL 9.3 and 9.4 do not call setPathArray(), and
already independently take this precaution, so no change there.

Peter Geoghegan
parent ae58f143
...@@ -3814,11 +3814,14 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls, ...@@ -3814,11 +3814,14 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
if (level < path_len && !path_nulls[level]) if (level < path_len && !path_nulls[level])
{ {
char *c = VARDATA_ANY(path_elems[level]); char *c = VARDATA_ANY(path_elems[level]);
long lindex;
errno = 0; errno = 0;
idx = (int) strtol(c, &badp, 10); lindex = strtol(c, &badp, 10);
if (errno != 0 || badp == c) if (errno != 0 || badp == c || lindex > INT_MAX || lindex < INT_MIN)
idx = nelems; idx = nelems;
else
idx = lindex;
} }
else else
idx = nelems; idx = nelems;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment