Merge pull request #62 from edwargix/gjson

Upgrade gjson and fix "slice bounds out of range" error
This commit is contained in:
Tulir Asokan 2022-01-18 17:15:01 +02:00 committed by GitHub
commit 80b906ad13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 15 deletions

View file

@ -47,8 +47,7 @@ func CanonicalJSONAssumeValid(input []byte) []byte {
func SortJSON(input, output []byte) []byte {
result := gjson.ParseBytes(input)
RawJSON := RawJSONFromResult(result, input)
return sortJSONValue(result, RawJSON, output)
return sortJSONValue(result, input, output)
}
// sortJSONValue takes a gjson.Result and sorts it. inputJSON must be the
@ -64,7 +63,7 @@ func sortJSONValue(input gjson.Result, inputJSON, output []byte) []byte {
// If its neither an object nor an array then there is no sub structure
// to sort, so just append the raw bytes.
return append(output, inputJSON...)
return append(output, input.Raw...)
}
// sortJSONArray takes a gjson.Result and sorts it, assuming its an array.
@ -76,10 +75,7 @@ func sortJSONArray(input gjson.Result, inputJSON, output []byte) []byte {
input.ForEach(func(_, value gjson.Result) bool {
output = append(output, sep)
sep = ','
RawJSON := RawJSONFromResult(value, inputJSON)
output = sortJSONValue(value, RawJSON, output)
output = sortJSONValue(value, inputJSON, output)
return true // keep iterating
})
@ -99,7 +95,7 @@ func sortJSONArray(input gjson.Result, inputJSON, output []byte) []byte {
func sortJSONObject(input gjson.Result, inputJSON, output []byte) []byte {
type entry struct {
key string // The parsed key string
rawKey []byte // The raw, unparsed key JSON string
rawKey string // The raw, unparsed key JSON string
value gjson.Result
}
@ -110,7 +106,7 @@ func sortJSONObject(input gjson.Result, inputJSON, output []byte) []byte {
input.ForEach(func(key, value gjson.Result) bool {
entries = append(entries, entry{
key: key.String(),
rawKey: RawJSONFromResult(key, inputJSON),
rawKey: key.Raw,
value: value,
})
return true // keep iterating
@ -130,10 +126,7 @@ func sortJSONObject(input gjson.Result, inputJSON, output []byte) []byte {
// Append the raw unparsed JSON key, *not* the parsed key
output = append(output, entry.rawKey...)
output = append(output, ':')
RawJSON := RawJSONFromResult(entry.value, inputJSON)
output = sortJSONValue(entry.value, RawJSON, output)
output = sortJSONValue(entry.value, inputJSON, output)
}
if sep == '{' {
// If sep is still '{' then the object was empty and we never wrote the

2
go.mod
View file

@ -9,7 +9,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.10
github.com/russross/blackfriday/v2 v2.1.0
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.10.2
github.com/tidwall/gjson v1.13.0
github.com/tidwall/sjson v1.2.3
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f

3
go.sum
View file

@ -34,8 +34,9 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.10.2 h1:APbLGOM0rrEkd8WBw9C24nllro4ajFuJu0Sc9hRz8Bo=
github.com/tidwall/gjson v1.10.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M=
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=