Skip to content

Commit

Permalink
fix check idx index
Browse files Browse the repository at this point in the history
  • Loading branch information
wgliang committed May 25, 2018
1 parent 9f095e0 commit 4c9aadc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion patch.go
Expand Up @@ -397,7 +397,9 @@ func (d *partialArray) add(key string, val *lazyNode) error {
}
idx = len(ary) - idx
}

if idx < 0 || idx >= len(ary) || idx > len(cur) {
return fmt.Errorf("Unable to access invalid index: %d", idx)
}
copy(ary[0:idx], cur[0:idx])
ary[idx] = val
copy(ary[idx+1:], cur[idx:])
Expand Down
4 changes: 4 additions & 0 deletions patch_test.go
Expand Up @@ -227,6 +227,10 @@ var BadCases = []BadCase{
`{ "name":{ "foo": "bat", "qux": "bum"}}`,
`[ { "op": "replace", "path": "/foo/bar", "value":"baz"}]`,
},
{
`{ "foo": ["bar"]}`,
`[ {"op": "add", "path": "/foo/2", "value": "bum"}]`,
},
}

func TestAllCases(t *testing.T) {
Expand Down

0 comments on commit 4c9aadc

Please sign in to comment.