Skip to content

Commit 7064efc

Browse files
committed
Convert remove value to node before comparing to test node.
Found with native golang fuzzing. Corpus: 9c67979 Backport: 86f1e19
1 parent 86f1e19 commit 7064efc

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lib/diff_read.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,30 @@ func readPatchDiffElement(patch []patchElement) (DiffElement, []patchElement, er
167167
var err error
168168
switch p.Op {
169169
case "test":
170+
// Read path.
170171
d.Path, err = readPointer(p.Path)
171172
if err != nil {
172173
return d, nil, err
173174
}
174-
old, err := NewJsonNode(p.Value)
175+
// Read value to test and remove.
176+
testValue, err := NewJsonNode(p.Value)
175177
if err != nil {
176178
return d, nil, err
177179
}
178-
d.OldValues = []JsonNode{old}
180+
d.OldValues = []JsonNode{testValue}
179181
patch = patch[1:]
182+
// Validate test and remove are paired because jd remove is strict.
180183
if len(patch) == 0 || patch[0].Op != "remove" {
181184
return d, nil, fmt.Errorf("JSON Patch test op must be followed by a remove op.")
182185
}
183186
if patch[0].Path != p.Path {
184187
return d, nil, fmt.Errorf("JSON Patch remove op must have the same path as test op.")
185188
}
186-
if patch[0].Value != p.Value {
189+
removeValue, err := NewJsonNode(patch[0].Value)
190+
if err != nil {
191+
return d, nil, err
192+
}
193+
if !testValue.Equals(removeValue) {
187194
return d, nil, fmt.Errorf("JSON Patch remove op must have the same value as test op.")
188195
}
189196
return d, patch[1:], nil
@@ -192,11 +199,11 @@ func readPatchDiffElement(patch []patchElement) (DiffElement, []patchElement, er
192199
if err != nil {
193200
return d, nil, err
194201
}
195-
new, err := NewJsonNode(p.Value)
202+
addValue, err := NewJsonNode(p.Value)
196203
if err != nil {
197204
return d, nil, err
198205
}
199-
d.NewValues = []JsonNode{new}
206+
d.NewValues = []JsonNode{addValue}
200207
return d, patch[1:], nil
201208
default:
202209
return d, nil, fmt.Errorf("Invalid JSON Patch. Must be test/remove or add ops.")

0 commit comments

Comments
 (0)