Skip to content
Closed
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
6 changes: 6 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
)

Expand Down Expand Up @@ -156,6 +157,11 @@ func (v *Value) patchAdd(i int, op patchOperation) error {
comp.Members[s.idx].Name.Value = String(s.name)
}
case *Array:
// Per RFC 6902, section 4.1, the index must not be greater
// than the number of elements in the array.
if n, err := strconv.ParseUint(s.name, 10, 0); s.name != "-" && (err != nil || n != uint64(s.idx)) {
return fmt.Errorf("hujson: patch operation %d: array index out of range: %s", i, s.name)
}
insertAt(comp, s.idx, op.value)
}
}
Expand Down
13 changes: 13 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ var testdataPatch = []struct {
in: `{"fizz":["buzz","wuzz"],"fizzy":"wizzy"}`,
patch: `[{ "op": "test", "path": "/noexist", "value": null }]`,
wantErr: errors.New(`hujson: patch operation 0: value not found`),
}, {
in: `[1,2]`,
patch: `[{ "op": "add", "path": "/2", "value": 3 }]`,
want: `[1,2,3]`,
}, {
in: `[1,2]`,
patch: `[{ "op": "add", "path": "/-", "value": 3 }]`,
want: `[1,2,3]`,
}, {
in: `[1,2]`,
patch: `[{ "op": "add", "path": "/5", "value": 3 }]`,
wantErr: errors.New(`hujson: patch operation 0: array index out of range: 5`),
want: `[1,2]`,
}, {
in: `{}`,
patch: `[{`,
Expand Down