Skip to content

Commit ed7cfba

Browse files
authored
Merge pull request #50 from clagraff/master
add nil-pointer checks to avoid runtime panics
2 parents 944e072 + 975445d commit ed7cfba

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

patch.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func (n *lazyNode) intoDoc() (*partialDoc, error) {
6666
return &n.doc, nil
6767
}
6868

69+
if n.raw == nil {
70+
return nil, fmt.Errorf("Unable to unmarshal nil pointer as partial document")
71+
}
72+
6973
err := json.Unmarshal(*n.raw, &n.doc)
7074

7175
if err != nil {
@@ -81,6 +85,10 @@ func (n *lazyNode) intoAry() (*partialArray, error) {
8185
return &n.ary, nil
8286
}
8387

88+
if n.raw == nil {
89+
return nil, fmt.Errorf("Unable to unmarshal nil pointer as partial array")
90+
}
91+
8492
err := json.Unmarshal(*n.raw, &n.ary)
8593

8694
if err != nil {
@@ -94,6 +102,10 @@ func (n *lazyNode) intoAry() (*partialArray, error) {
94102
func (n *lazyNode) compact() []byte {
95103
buf := &bytes.Buffer{}
96104

105+
if n.raw == nil {
106+
return nil
107+
}
108+
97109
err := json.Compact(buf, *n.raw)
98110

99111
if err != nil {
@@ -104,6 +116,10 @@ func (n *lazyNode) compact() []byte {
104116
}
105117

106118
func (n *lazyNode) tryDoc() bool {
119+
if n.raw == nil {
120+
return false
121+
}
122+
107123
err := json.Unmarshal(*n.raw, &n.doc)
108124

109125
if err != nil {
@@ -115,6 +131,10 @@ func (n *lazyNode) tryDoc() bool {
115131
}
116132

117133
func (n *lazyNode) tryAry() bool {
134+
if n.raw == nil {
135+
return false
136+
}
137+
118138
err := json.Unmarshal(*n.raw, &n.ary)
119139

120140
if err != nil {

patch_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ var TestCases = []TestCase{
314314
true,
315315
"",
316316
},
317+
{
318+
`{ "foo": null }`,
319+
`[ { "op": "test", "path": "/foo", "value": null } ]`,
320+
true,
321+
"",
322+
},
323+
{
324+
`{ "foo": {} }`,
325+
`[ { "op": "test", "path": "/foo", "value": null } ]`,
326+
false,
327+
"/foo",
328+
},
329+
{
330+
`{ "foo": [] }`,
331+
`[ { "op": "test", "path": "/foo", "value": null } ]`,
332+
false,
333+
"/foo",
334+
},
317335
{
318336
`{ "baz/foo": "qux" }`,
319337
`[ { "op": "test", "path": "/baz~1foo", "value": "qux"} ]`,

0 commit comments

Comments
 (0)