extmod/modjson: Detect unterminated composite entities.
This commit makes the JSON parser raise an exception when handling objects or arrays whose declaration is incomplete, as in missing the closing marker (brace or bracket) and if the missing marker would have been the last non-whitespace character in the incoming string. Since CPython's JSON parser would raise an exception in such a case, unlike MicroPython's, this commit aligns MicroPython's behaviour with CPython. This commit fixes issue #17141. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit is contained in:
@@ -160,7 +160,8 @@ static mp_obj_t mod_json_load(mp_obj_t stream_obj) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
cont:
|
cont:
|
||||||
if (S_END(s)) {
|
if (S_END(s)) {
|
||||||
break;
|
// Input finished abruptly in the middle of a composite entity.
|
||||||
|
goto fail;
|
||||||
}
|
}
|
||||||
mp_obj_t next = MP_OBJ_NULL;
|
mp_obj_t next = MP_OBJ_NULL;
|
||||||
bool enter = false;
|
bool enter = false;
|
||||||
|
|||||||
@@ -71,3 +71,27 @@ try:
|
|||||||
my_print(json.loads("[null] a"))
|
my_print(json.loads("[null] a"))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("ValueError")
|
print("ValueError")
|
||||||
|
|
||||||
|
# incomplete object declaration
|
||||||
|
try:
|
||||||
|
my_print(json.loads('{"a":0,'))
|
||||||
|
except ValueError:
|
||||||
|
print("ValueError")
|
||||||
|
|
||||||
|
# incomplete nested array declaration
|
||||||
|
try:
|
||||||
|
my_print(json.loads('{"a":0, ['))
|
||||||
|
except ValueError:
|
||||||
|
print("ValueError")
|
||||||
|
|
||||||
|
# incomplete array declaration
|
||||||
|
try:
|
||||||
|
my_print(json.loads('[0,'))
|
||||||
|
except ValueError:
|
||||||
|
print("ValueError")
|
||||||
|
|
||||||
|
# incomplete nested object declaration
|
||||||
|
try:
|
||||||
|
my_print(json.loads('[0, {"a":0, '))
|
||||||
|
except ValueError:
|
||||||
|
print("ValueError")
|
||||||
|
|||||||
Reference in New Issue
Block a user