Skip to content

Commit 9a982e5

Browse files
Bridget Lanebuger
authored andcommitted
Prevent infinite looping in GetString on certain malformed strings (#167)
1 parent 3460630 commit 9a982e5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ func searchKeys(data []byte, keys ...string) int {
333333
i += arraySkip - 1
334334
}
335335
}
336+
case ':': // If encountered, JSON data is malformed
337+
return -1
336338
}
337339

338340
i++

parser_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,20 @@ var getStringTests = []GetTest{
886886
isFound: true,
887887
data: "value\b\f\n\r\tvalue", // value is unescaped since this is GetString()
888888
},
889+
{ // This test checks we avoid an infinite loop for certain malformed JSON. We don't check for all malformed JSON as it would reduce performance.
890+
desc: `malformed with double quotes`,
891+
json: `{"a"":1}`,
892+
path: []string{"a"},
893+
isFound: false,
894+
data: ``,
895+
},
896+
{ // More malformed JSON testing, to be sure we avoid an infinite loop.
897+
desc: `malformed with double quotes, and path does not exist`,
898+
json: `{"z":123,"y":{"x":7,"w":0},"v":{"u":"t","s":"r","q":0,"p":1558051800},"a":"b","c":"2016-11-02T20:10:11Z","d":"e","f":"g","h":{"i":"j""},"k":{"l":"m"}}`,
899+
path: []string{"o"},
900+
isFound: false,
901+
data: ``,
902+
},
889903
}
890904

891905
var getBoolTests = []GetTest{

0 commit comments

Comments
 (0)