Skip to content

Commit 003f60f

Browse files
authored
x.json2: fix nest level check (#10584)
1 parent cdb3111 commit 003f60f

File tree

2 files changed

+31
-50
lines changed

2 files changed

+31
-50
lines changed

vlib/x/json2/decoder.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,9 @@ fn (mut p Parser) decode() ?Any {
9292
}
9393

9494
fn (mut p Parser) decode_value() ?Any {
95-
if p.n_level == 500 {
95+
if p.n_level + 1 == 500 {
9696
return error(p.emit_error('reached maximum nesting level of 500'))
9797
}
98-
if (p.tok.kind == .lsbr && p.n_tok.kind == .lcbr)
99-
|| (p.p_tok.kind == p.tok.kind && p.tok.kind == .lsbr) {
100-
p.n_level++
101-
}
10298
match p.tok.kind {
10399
.lsbr {
104100
return p.decode_array()
@@ -150,6 +146,7 @@ fn (mut p Parser) decode_value() ?Any {
150146
fn (mut p Parser) decode_array() ?Any {
151147
mut items := []Any{}
152148
p.next_with_err() ?
149+
p.n_level++
153150
for p.tok.kind != .rsbr {
154151
item := p.decode_value() ?
155152
items << item
@@ -169,12 +166,14 @@ fn (mut p Parser) decode_array() ?Any {
169166
}
170167
}
171168
p.next_with_err() ?
169+
p.n_level--
172170
return Any(items)
173171
}
174172

175173
fn (mut p Parser) decode_object() ?Any {
176174
mut fields := map[string]Any{}
177175
p.next_with_err() ?
176+
p.n_level++
178177
for p.tok.kind != .rcbr {
179178
is_key := p.tok.kind == .str_ && p.n_tok.kind == .colon
180179
if !is_key {
@@ -196,5 +195,6 @@ fn (mut p Parser) decode_object() ?Any {
196195
}
197196
}
198197
p.next_with_err() ?
198+
p.n_level--
199199
return Any(fields)
200200
}

vlib/x/json2/decoder_test.v

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,61 @@
1-
import x.json2
1+
module json2
22

3-
fn test_raw_decode_string() {
4-
str := json2.raw_decode('"Hello!"') or {
5-
assert false
6-
json2.Any(json2.null)
7-
}
3+
fn test_raw_decode_string() ? {
4+
str := raw_decode('"Hello!"') ?
85
assert str.str() == 'Hello!'
96
}
107

11-
fn test_raw_decode_number() {
12-
num := json2.raw_decode('123') or {
13-
assert false
14-
json2.Any(json2.null)
15-
}
8+
fn test_raw_decode_number() ? {
9+
num := raw_decode('123') ?
1610
assert num.int() == 123
1711
}
1812

19-
fn test_raw_decode_array() {
20-
raw_arr := json2.raw_decode('["Foo", 1]') or {
21-
assert false
22-
json2.Any(json2.null)
23-
}
13+
fn test_raw_decode_array() ? {
14+
raw_arr := raw_decode('["Foo", 1]') ?
2415
arr := raw_arr.arr()
2516
assert arr[0].str() == 'Foo'
2617
assert arr[1].int() == 1
2718
}
2819

29-
fn test_raw_decode_bool() {
30-
bol := json2.raw_decode('false') or {
31-
assert false
32-
json2.Any(json2.null)
33-
}
20+
fn test_raw_decode_bool() ? {
21+
bol := raw_decode('false') ?
3422
assert bol.bool() == false
3523
}
3624

37-
fn test_raw_decode_map() {
38-
raw_mp := json2.raw_decode('{"name":"Bob","age":20}') or {
39-
assert false
40-
json2.Any(json2.null)
41-
}
25+
fn test_raw_decode_map() ? {
26+
raw_mp := raw_decode('{"name":"Bob","age":20}') ?
4227
mp := raw_mp.as_map()
4328
assert mp['name'].str() == 'Bob'
4429
assert mp['age'].int() == 20
4530
}
4631

47-
fn test_raw_decode_null() {
48-
nul := json2.raw_decode('null') or {
49-
assert false
50-
json2.Any(json2.null)
51-
}
52-
assert nul is json2.Null
32+
fn test_raw_decode_null() ? {
33+
nul := raw_decode('null') ?
34+
assert nul is Null
5335
}
5436

55-
fn test_raw_decode_invalid() {
56-
json2.raw_decode('1z') or {
37+
fn test_raw_decode_invalid() ? {
38+
raw_decode('1z') or {
5739
assert err.msg == '[x.json2] invalid token `z` (0:17)'
5840
return
5941
}
6042
assert false
6143
}
6244

63-
fn test_raw_decode_string_with_dollarsign() {
64-
str := json2.raw_decode(r'"Hello $world"') or {
65-
assert false
66-
json2.Any(json2.null)
67-
}
45+
fn test_raw_decode_string_with_dollarsign() ? {
46+
str := raw_decode(r'"Hello $world"') ?
6847
assert str.str() == r'Hello $world'
6948
}
7049

71-
fn test_raw_decode_map_with_whitespaces() {
72-
raw_mp := json2.raw_decode(' \n\t{"name":"Bob","age":20}\n\t') or {
73-
eprintln(err.msg)
74-
assert false
75-
json2.Any(json2.null)
76-
}
50+
fn test_raw_decode_map_with_whitespaces() ? {
51+
raw_mp := raw_decode(' \n\t{"name":"Bob","age":20}\n\t') ?
7752
mp := raw_mp.as_map()
7853
assert mp['name'].str() == 'Bob'
7954
assert mp['age'].int() == 20
8055
}
56+
57+
fn test_nested_array_object() ? {
58+
mut parser := new_parser(r'[[[[[],[],[]]]],{"Test":{}},[[]]]', false)
59+
decoded := parser.decode() ?
60+
assert parser.n_level == 0
61+
}

0 commit comments

Comments
 (0)