Skip to content

release-25.2: jsonpath: validate array indices are within int32 range #144325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/jsonb_path_query
Original file line number Diff line number Diff line change
Expand Up @@ -1406,3 +1406,25 @@ query T
SELECT jsonb_path_query('{"a": {"b": [{"c": 1}, {"c": 2}]}}', '($.a.b[1]).c');
----
2

statement error pgcode 22033 pq: jsonpath array subscript is out of integer range
SELECT jsonb_path_query('[1]', 'lax $[10000000000000000]');

statement error pgcode 22033 pq: jsonpath array subscript is out of integer range
SELECT jsonb_path_query('[1]', 'lax $[-10000000000000000]');

# MaxInt32
query empty
SELECT jsonb_path_query('[1]', '$[2147483647]');

# MaxInt32 + 1
statement error pgcode 22033 pq: jsonpath array subscript is out of integer range
SELECT jsonb_path_query('[1]', '$[2147483648]');

# MinInt32
query empty
SELECT jsonb_path_query('[1]', '$[-2147483648]');

# MinInt32 - 1
statement error pgcode 22033 pq: jsonpath array subscript is out of integer range
SELECT jsonb_path_query('[1]', '$[-2147483649]');
15 changes: 10 additions & 5 deletions pkg/util/jsonpath/eval/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package eval

import (
"math"

"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/util/json"
Expand All @@ -18,6 +20,7 @@ var (
errIndexOnNonArray = pgerror.Newf(pgcode.SQLJSONArrayNotFound, "jsonpath array accessor can only be applied to an array")
errIndexOutOfBounds = pgerror.Newf(pgcode.InvalidSQLJSONSubscript, "jsonpath array subscript is out of bounds")
errIndexNotSingleNumValue = pgerror.Newf(pgcode.InvalidSQLJSONSubscript, "jsonpath array subscript is not a single numeric value")
errInvalidSubscript = pgerror.Newf(pgcode.InvalidSQLJSONSubscript, "jsonpath array subscript is out of integer range")
)

func (ctx *jsonpathCtx) evalArrayWildcard(jsonValue json.JSON) ([]json.JSON, error) {
Expand Down Expand Up @@ -110,23 +113,25 @@ func (ctx *jsonpathCtx) resolveArrayIndex(
if len(evalResults) != 1 || evalResults[0].Type() != json.NumberJSONType {
return -1, errIndexNotSingleNumValue
}
// TODO(normanchenn): Postgres returns an error if the index is outside int32
// range. (ex. `select jsonb_path_query('[1]', 'lax $[10000000000000000]');
i, err := asInt(evalResults[0])
if err != nil {
return -1, errIndexNotSingleNumValue
return -1, err
}
return i, nil
}

func asInt(j json.JSON) (int, error) {
d, ok := j.AsDecimal()
if !ok {
return 0, errInternal
return 0, errIndexNotSingleNumValue
}
i64, err := d.Int64()
if err != nil {
return 0, err
return 0, errIndexNotSingleNumValue
}
// Postgres returns an error if the index is outside int32 range.
if i64 < math.MinInt32 || i64 > math.MaxInt32 {
return 0, errInvalidSubscript
}
return int(i64), nil
}
Expand Down
1 change: 0 additions & 1 deletion pkg/util/jsonpath/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

var (
errUnimplemented = unimplemented.NewWithIssue(22513, "unimplemented")
errInternal = errors.New("internal error")
errSingleBooleanRequired = pgerror.Newf(pgcode.SingletonSQLJSONItemRequired, "single boolean result is expected")
)

Expand Down