Skip to content

Commit

Permalink
Fix zero sized document crash
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Apr 21, 2021
1 parent 0773747 commit 60af5a5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ func (p Patch) Apply(doc []byte) ([]byte, error) {
// ApplyIndent mutates a JSON document according to the patch, and returns the new
// document indented.
func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error) {
if len(doc) == 0 {
return doc, nil
}

var pd container
if doc[0] == '[' {
pd = &partialArray{}
Expand Down
10 changes: 9 additions & 1 deletion patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func repeatedA(r int) string {
}

var Cases = []Case{
{
``,
`[
{ "op": "add", "path": "/baz", "value": "qux" }
]`,
``,
},
{
`{ "foo": "bar"}`,
`[
Expand Down Expand Up @@ -518,8 +525,9 @@ func TestAdd(t *testing.T) {
key: "-1",
val: lazyNode{},
arr: partialArray{},
err: "Unable to access invalid index: -1: invalid index referenced",

rejectNegativeIndicies: true,
err: "Unable to access invalid index: -1: invalid index referenced",
},
}
for _, tc := range testCases {
Expand Down
8 changes: 6 additions & 2 deletions v5/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
startObject = json.Delim('{')
endObject = json.Delim('}')
startArray = json.Delim('[')
endArray = json.Delim(']')
endArray = json.Delim(']')
)

var (
Expand Down Expand Up @@ -57,7 +57,7 @@ type Patch []Operation

type partialDoc struct {
keys []string
obj map[string]*lazyNode
obj map[string]*lazyNode
}

type partialArray []*lazyNode
Expand Down Expand Up @@ -1026,6 +1026,10 @@ func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error) {
// ApplyIndentWithOptions mutates a JSON document according to the patch and the passed in ApplyOptions.
// It returns the new document indented.
func (p Patch) ApplyIndentWithOptions(doc []byte, indent string, options *ApplyOptions) ([]byte, error) {
if len(doc) == 0 {
return doc, nil
}

var pd container
if doc[0] == '[' {
pd = &partialArray{}
Expand Down
9 changes: 9 additions & 0 deletions v5/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func repeatedA(r int) string {
}

var Cases = []Case{
{
``,
`[
{ "op": "add", "path": "/baz", "value": "qux" }
]`,
``,
false,
false,
},
{
`{ "foo": "bar"}`,
`[
Expand Down

0 comments on commit 60af5a5

Please sign in to comment.