Skip to content

Commit 7fca133

Browse files
author
Alexey Botchkov
committed
MDEV-11463 Server crashes in mark_array upon JSON_VALID.
The depth of nested arrays should be controlled, as it's limited.
1 parent edc75c9 commit 7fca133

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

mysql-test/r/func_json.result

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ json_valid('{"key1":1, "key2":[2,3]}')
1010
select json_valid('[false, true, null]');
1111
json_valid('[false, true, null]')
1212
1
13+
select json_valid(repeat('[', 1000));
14+
json_valid(repeat('[', 1000))
15+
0
1316
select json_value('{"key1":123}', '$.key2');
1417
json_value('{"key1":123}', '$.key2')
1518
NULL

mysql-test/t/func_json.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ select json_valid('[1, 2]');
22
select json_valid('"string"}');
33
select json_valid('{"key1":1, "key2":[2,3]}');
44
select json_valid('[false, true, null]');
5+
select json_valid(repeat('[', 1000));
56

67
select json_value('{"key1":123}', '$.key2');
78
select json_value('{"key1":123}', '$.key1');

strings/json_lib.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ static int syntax_error(json_engine_t *j)
126126
static int mark_object(json_engine_t *j)
127127
{
128128
j->state= JST_OBJ_START;
129-
*(++j->stack_p)= JST_OBJ_CONT;
130-
return 0;
129+
if ((++j->stack_p) - j->stack < JSON_DEPTH_LIMIT)
130+
{
131+
*j->stack_p= JST_OBJ_CONT;
132+
return 0;
133+
}
134+
j->s.error= JE_DEPTH;
135+
return 1;
131136
}
132137

133138

@@ -137,18 +142,28 @@ static int read_obj(json_engine_t *j)
137142
j->state= JST_OBJ_START;
138143
j->value_type= JSON_VALUE_OBJECT;
139144
j->value= j->value_begin;
140-
*(++j->stack_p)= JST_OBJ_CONT;
141-
return 0;
145+
if ((++j->stack_p) - j->stack < JSON_DEPTH_LIMIT)
146+
{
147+
*j->stack_p= JST_OBJ_CONT;
148+
return 0;
149+
}
150+
j->s.error= JE_DEPTH;
151+
return 1;
142152
}
143153

144154

145155
/* Value of array. */
146156
static int mark_array(json_engine_t *j)
147157
{
148158
j->state= JST_ARRAY_START;
149-
*(++j->stack_p)= JST_ARRAY_CONT;
150-
j->value= j->value_begin;
151-
return 0;
159+
if ((++j->stack_p) - j->stack < JSON_DEPTH_LIMIT)
160+
{
161+
*j->stack_p= JST_ARRAY_CONT;
162+
j->value= j->value_begin;
163+
return 0;
164+
}
165+
j->s.error= JE_DEPTH;
166+
return 1;
152167
}
153168

154169
/* Read value of object. */
@@ -157,8 +172,13 @@ static int read_array(json_engine_t *j)
157172
j->state= JST_ARRAY_START;
158173
j->value_type= JSON_VALUE_ARRAY;
159174
j->value= j->value_begin;
160-
*(++j->stack_p)= JST_ARRAY_CONT;
161-
return 0;
175+
if ((++j->stack_p) - j->stack < JSON_DEPTH_LIMIT)
176+
{
177+
*j->stack_p= JST_ARRAY_CONT;
178+
return 0;
179+
}
180+
j->s.error= JE_DEPTH;
181+
return 1;
162182
}
163183

164184

0 commit comments

Comments
 (0)