Skip to content

Commit

Permalink
Merge pull request #78 from guzba/pr
Browse files Browse the repository at this point in the history
fixes + more fuzzing
  • Loading branch information
treeform authored Sep 14, 2023
2 parents 4fa3a9b + c2728e8 commit 45f462e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/jsony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ proc parseHook*(s: string, i: var int, v: var SomeFloat) =

proc parseUnicodeEscape(s: string, i: var int): int =
inc i
if i + 4 > s.len:
error("Expected unicode escape hex but end reached.", i)
result = parseHexInt(s[i ..< i + 4])
i += 3
# Deal with UTF-16 surrogates. Most of the time strings are encoded as utf8
Expand All @@ -168,6 +170,8 @@ proc parseStringSlow(s: string, i: var int, v: var string) =
break
of '\\':
inc i
if i >= s.len:
error("Expected escaped character but end reached.", i)
let c = s[i]
case c
of '"', '\\', '/': v.add(c)
Expand Down Expand Up @@ -198,10 +202,12 @@ proc parseStringFast(s: string, i: var int, v: var string) =
break
of '\\':
inc j
if j >= s.len:
error("Expected escaped character but end reached.", j)
let c = s[j]
case c
of 'u':
ll += Rune(parseUnicodeEscape(s, j)).toUTF8().len
ll += Rune(parseUnicodeEscape(s, j)).size
else:
inc ll
else:
Expand Down
16 changes: 9 additions & 7 deletions tests/fuzz.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import jsony, os, random, strformat, strutils, tables

type
NodeKind = enum
nkRed, nkBlue, nkGree, nkBlack
nkRed, nkBlue, nkGreen, nkBlack
Node = ref object
active: bool
kind: NodeKind
Expand All @@ -11,6 +11,7 @@ type
id: int
kids: seq[Node]
table: Table[string, uint8]
body: string

var r = initRand(2020)
var genId: int
Expand All @@ -25,9 +26,10 @@ proc genTree(depth: int): Node =
if r.rand(0 .. 1) == 0:
result.active = true
result.name = "node" & $result.id
result.kind = [nkRed, nkBlue, nkGree, nkBlack][r.rand(0 .. 3)]
result.kind = [nkRed, nkBlue, nkGreen, nkBlack][r.rand(0 .. 3)]
result.table["cat"] = 4
result.table["dog"] = 4
result.body = "abc🔒\n"
if depth > 0:
for i in 0 .. r.rand(0..3):
result.kids.add genTree(depth - 1)
Expand All @@ -42,7 +44,7 @@ randomize()
for i in 0 ..< 10000:
var
data = treeStr
pos = rand(data.len)
pos = rand(data.high)
value = rand(255).char
#pos = 18716
#value = 125.char
Expand All @@ -52,25 +54,25 @@ for i in 0 ..< 10000:
try:
let node = data.fromJson(Node)
doAssert node != nil
except JsonError:
except:
discard

var data2 = data[0 ..< pos]
try:
let node = data2.fromJson(Node)
doAssert node != nil
except JsonError:
except:
discard

# JsonNode
try:
let node = data.fromJson()
doAssert node != nil
except JsonError:
except:
discard

try:
let node = data2.fromJson()
doAssert node != nil
except JsonError:
except:
discard
6 changes: 6 additions & 0 deletions tests/fuzz_strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ for i in 0 ..< 100_000:
if s.toJson().fromJson(string) != s:
echo "some thing wrong!"
echo repr(s)

for i in 0 ..< 10_000:
var s = ""
for i in 0 ..< rand(0 .. 1_000):
s.add cast[char](rand(0 .. 255))
discard s.toJson().fromJson(string)
10 changes: 10 additions & 0 deletions tests/test_strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ block:
echo "jsony - ", parsed2.content
echo "std/json - ", parsedStd2.content
doAssert parsed2.content == parsedStd2.content

block:
var s = "\"\\u00\""
doAssertRaises jsony.JsonError:
discard fromJson(s, string)

block:
var s = "\"\\"
doAssertRaises jsony.JsonError:
discard fromJson(s, string)

0 comments on commit 45f462e

Please sign in to comment.