shithub: mc

Download patch

ref: 571472a78619e3314aaf8e95be4f70cea1ef51a8
parent: 073eb256baff4d87a723518f80ac758db4e0a551
author: Ori Bernstein <ori@markovcorp.com>
date: Thu Dec 29 10:18:22 EST 2016

Add maximum depth to parser.

--- a/lib/json/parse.myr
+++ b/lib/json/parse.myr
@@ -7,8 +7,11 @@
 	const free	: (j : elt# -> void)
 ;;
 
+const Maxdepth = 1024
+
 type parser = struct
 	str	: byte[:]
+	depth	: std.size
 	line	: std.size
 	off	: std.size
 	idx	: std.size
@@ -19,6 +22,7 @@
 
 	parser = [
 		.str = str,
+		.depth = 0,
 		.line = 1,
 		.off = 1,
 		.idx = 0,
@@ -88,6 +92,10 @@
 
 	std.assert(takec(p) == '{', "should only enter 'obj' after '{'")
 	membs = [][:]
+	if !enter(p)
+		err = [.e = `Depth, .line=p.line, .off=p.off]
+		goto error
+	;;
 	takespace(p)
 	if peekc(p) == '}'
 		takec(p)
@@ -113,6 +121,7 @@
 			goto error
 		;;
 	;;
+	exit(p)
 	-> `std.Ok std.mk(`Obj membs)
 :error
 	for (k, v) in membs
@@ -120,6 +129,7 @@
 		free(v)
 	;;
 	std.slfree(membs)
+	exit(p)
 	-> `std.Err err
 }
 
@@ -154,6 +164,10 @@
 
 	std.assert(takec(p) == '[', "should only enter 'obj' after '['\n")
 	elts = [][:]
+	if !enter(p)
+		err = [.e = `Depth, .line=p.line, .off=p.off]
+		goto error
+	;;
 	takespace(p)
 	if peekc(p) == ']'
 		takec(p)
@@ -179,6 +193,7 @@
 			goto error
 		;;
 	;;
+	exit(p)
 	-> `std.Ok std.mk(`Arr elts)
 :error
 	for e in elts
@@ -185,6 +200,7 @@
 		free(e)
 	;;
 	std.slfree(elts)
+	exit(p)
 	-> `std.Err err
 }
 
@@ -376,3 +392,12 @@
 	-> c
 }
 
+const enter = {p
+	std.put("depth: {}, max: {}\n", p.depth, Maxdepth)
+	p.depth++
+	-> p.depth <= Maxdepth
+}
+
+const exit = {p
+	p.depth--
+}
--- a/lib/json/types.myr
+++ b/lib/json/types.myr
@@ -19,6 +19,7 @@
 	type errtype = union
 		`Badesc char
 		`Junk char
+		`Depth
 		`End
 	;;
 ;;