Skip to content

Commit

Permalink
fix: validate empty object/array in ast Get (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 authored Mar 14, 2023
1 parent 18d2734 commit 8f1e08c
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 358 deletions.
5 changes: 3 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ func UnmarshalString(buf string, val interface{}) error {
// and returns its representing ast.Node.
//
// Each path arg must be integer or string:
// - Integer means searching current node as array
// - String means searching current node as object
// - Integer is target index(>=0), means searching current node as array.
// - String is target key, means searching current node as object.
//
//
// Note, the api expects the json is well-formed at least,
// otherwise it may return unexpected result.
func Get(src []byte, path ...interface{}) (ast.Node, error) {
Expand Down
64 changes: 56 additions & 8 deletions ast/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestGC_Search(t *testing.T) {
wg.Wait()
}

func TestExportError(t *testing.T) {
func TestExportErrorInvalidChar(t *testing.T) {
data := `{"a":]`
p := NewSearcher(data)
_, err := p.GetByPath("a")
Expand Down Expand Up @@ -85,27 +85,76 @@ func TestExportError(t *testing.T) {
t.Fatal(err)
}

data = `{"a":null}`
data = `{`
p = NewSearcher(data)
_, err = p.GetByPath("b")
_, err = p.GetByPath("he")
if err == nil {
t.Fatal()
}
if err != ErrNotExist {
if err == ErrNotExist {
t.Fatal(err)
}

data = `[1,2,3]`
data = `[`
p = NewSearcher(data)
_, err = p.GetByPath(4)
_, err = p.GetByPath(0)
if err == nil {
t.Fatal()
}
if err != ErrNotExist {
if err == ErrNotExist {
t.Fatal(err)
}
}

type testExportError struct {
data string
path []interface{}
err error
}

func TestExportErrNotExist(t *testing.T) {
tests := []testExportError{
// object
{`{}`, []interface{}{"b"}, ErrNotExist},
{` { } `, []interface{}{"b"}, ErrNotExist},
{`{"a":null}`, []interface{}{"b"}, ErrNotExist},
// This should be invalid char errors.
// {`{"a":null}`, []interface{}{"a", "b"}, ErrNotExist},
// {`{"a":null}`, []interface{}{"a", 0}, ErrNotExist},
// {`{"a":null}`, []interface{}{"a", "b", 0}, ErrNotExist},
{`{"":{"b":123}}`, []interface{}{"b"}, ErrNotExist},
{`{"a":{"b":123}}`, []interface{}{"b"}, ErrNotExist},
{`{"a":{"b":123}}`, []interface{}{"a", "c"}, ErrNotExist},
{`{"a":{"c": null, "b":{}}}`, []interface{}{"a", "b", "c"}, ErrNotExist},
{`{"a":{"b":123}}`, []interface{}{"b", "b"}, ErrNotExist},
{`{"\"\\":{"b":123}}`, []interface{}{"\"", "b"}, ErrNotExist},
{`{"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\\":{"b":123}}`,
[]interface{}{"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"", "b"}, ErrNotExist},

// array
{`[]`, []interface{}{0}, ErrNotExist},
{`[]`, []interface{}{1}, ErrNotExist},
{` [ ] `, []interface{}{0}, ErrNotExist},
{`[null]`, []interface{}{1}, ErrNotExist},
{`[null, ["null", 123]]`, []interface{}{2}, ErrNotExist},
{`[null, true , false, 14, 2.35, -46, "hello7", "\"8"]`, []interface{}{8}, ErrNotExist},
{`[{}]`, []interface{}{1}, ErrNotExist},
{`[[]]`, []interface{}{1}, ErrNotExist},
{`[[],[{},{}, []],{}]`, []interface{}{3}, ErrNotExist},
}

for _, test := range tests {
f := func(t *testing.T) {
p := NewSearcher(test.data)
node, err := p.GetByPath(test.path...)
if err != test.err || node.Exists(){
t.Fatal(err)
}
}
t.Run(test.data, f)
}
}

func TestSearcher_GetByPath(t *testing.T) {
s := NewSearcher(` { "xx" : [] ,"yy" :{ }, "test" : [ true , 0.1 , "abc", ["h"], {"a":"bc"} ] } `)

Expand Down Expand Up @@ -163,7 +212,6 @@ func TestSearcher_GetByPathSingle(t *testing.T) {
{`[1,2,3]`, Path{0}, 1.0, Ok},
{`[1,2,3]`, Path{1}, 2.0, Ok},
{`[1,2,3]`, Path{2}, 3.0, Ok},
{`[1,2,3]`, Path{2}, 3.0, Ok},

{`tru`, Path{}, nil, Error},
{`fal`, Path{}, nil, Error},
Expand Down
241 changes: 128 additions & 113 deletions internal/native/avx/native_amd64.s

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/native/avx/native_subr_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8f1e08c

Please sign in to comment.