Skip to content

Commit d017043

Browse files
quakebobzhang
authored andcommitted
chore: apply review comment
1 parent cda65e0 commit d017043

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

json/internal_types.mbt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ priv struct ParseContext {
1717
mut offset : Int
1818
input : String
1919
end_offset : Int
20-
max_nesting_depth : Int
21-
mut depth : Int
20+
mut remaining_available_depth : Int
2221
}
2322

2423
///|
2524
fn ParseContext::make(input : String, max_nesting_depth : Int) -> ParseContext {
26-
{ offset: 0, input, end_offset: input.length(), max_nesting_depth, depth: 0 }
25+
{
26+
offset: 0,
27+
input,
28+
end_offset: input.length(),
29+
remaining_available_depth: max_nesting_depth,
30+
}
2731
}
2832

2933
///|

json/json_test.mbt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,5 +538,16 @@ test "call stack overflow" {
538538
json_string.write_char(']')
539539
}
540540
let _ = @json.parse(json_string.to_string()) catch { _ => null }
541+
let depth = 1500
542+
let json_string = StringBuilder::new()
543+
for _ in 0..=depth {
544+
json_string.write_char('[')
545+
}
546+
for _ in 0..=depth {
547+
json_string.write_char(']')
548+
}
549+
let _ = @json.parse(json_string.to_string(), max_nesting_depth=2000) catch {
550+
_ => null
551+
}
541552

542553
}

json/parse.mbt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn valid(input : String) -> Bool {
2323
}
2424

2525
///|
26+
/// Parse a JSON input string into a Json value, with an optional maximum nesting depth (default is 1024)
2627
pub fn parse(
2728
input : String,
2829
max_nesting_depth? : Int = 1024,
@@ -62,14 +63,14 @@ fn ParseContext::parse_value2(
6263

6364
///|
6465
fn ParseContext::parse_object(ctx : ParseContext) -> Json raise ParseError {
65-
if ctx.depth >= ctx.max_nesting_depth {
66+
if ctx.remaining_available_depth <= 0 {
6667
raise DepthLimitExceeded
6768
}
68-
ctx.depth += 1
69+
ctx.remaining_available_depth -= 1
6970
let map = Map::new()
7071
loop ctx.lex_property_name() {
7172
RBrace => {
72-
ctx.depth -= 1
73+
ctx.remaining_available_depth += 1
7374
Json::object(map)
7475
}
7576
String(name) => {
@@ -78,7 +79,7 @@ fn ParseContext::parse_object(ctx : ParseContext) -> Json raise ParseError {
7879
match ctx.lex_after_object_value() {
7980
Comma => continue ctx.lex_property_name2()
8081
RBrace => {
81-
ctx.depth -= 1
82+
ctx.remaining_available_depth += 1
8283
Json::object(map)
8384
}
8485
_ => abort("unreachable")
@@ -90,14 +91,14 @@ fn ParseContext::parse_object(ctx : ParseContext) -> Json raise ParseError {
9091

9192
///|
9293
fn ParseContext::parse_array(ctx : ParseContext) -> Json raise ParseError {
93-
if ctx.depth >= ctx.max_nesting_depth {
94+
if ctx.remaining_available_depth <= 0 {
9495
raise DepthLimitExceeded
9596
}
96-
ctx.depth += 1
97+
ctx.remaining_available_depth -= 1
9798
let vec = []
9899
loop ctx.lex_value(allow_rbracket=true) {
99100
RBracket => {
100-
ctx.depth -= 1
101+
ctx.remaining_available_depth += 1
101102
Json::array(vec)
102103
}
103104
tok => {
@@ -106,7 +107,7 @@ fn ParseContext::parse_array(ctx : ParseContext) -> Json raise ParseError {
106107
match tok2 {
107108
Comma => continue ctx.lex_value(allow_rbracket=false)
108109
RBracket => {
109-
ctx.depth -= 1
110+
ctx.remaining_available_depth += 1
110111
Json::array(vec)
111112
}
112113
_ => abort("unreachable")

json/types.mbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ pub impl Show for ParseError with output(self, logger) {
5353
..write_object(line)
5454
..write_string(", column ")
5555
..write_object(column)
56-
DepthLimitExceeded => logger.write_string("Depth limit exceeded, please increase the max_nesting_depth parameter")
56+
DepthLimitExceeded =>
57+
logger.write_string(
58+
"Depth limit exceeded, please increase the max_nesting_depth parameter",
59+
)
5760
}
5861
}
5962

0 commit comments

Comments
 (0)