From ad72aac67ab84280cbd7e08b2668ef7fe5db046e Mon Sep 17 00:00:00 2001 From: pt300 Date: Sat, 1 Oct 2016 18:07:35 +0200 Subject: [PATCH 1/4] Partialy fixes zserge/jsmn#81 Still will report invalid amount if we fetch it with something like "{"key 1": 1234}}" --- jsmn.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/jsmn.c b/jsmn.c index e7765eb..da9bf21 100644 --- a/jsmn.c +++ b/jsmn.c @@ -198,6 +198,9 @@ int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, break; } if (token->parent == -1) { + if(token->type != type) { + return JSMN_ERROR_INVAL; + } break; } token = &tokens[token->parent]; From 4ce44040579e65755d14a3d7dabdae0913959a24 Mon Sep 17 00:00:00 2001 From: pt300 Date: Sat, 1 Oct 2016 18:19:20 +0200 Subject: [PATCH 2/4] Seems to actually fix zserge/jsmn#81 --- jsmn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsmn.c b/jsmn.c index da9bf21..bcd6392 100644 --- a/jsmn.c +++ b/jsmn.c @@ -198,7 +198,7 @@ int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, break; } if (token->parent == -1) { - if(token->type != type) { + if(token->type != type || parser->toksuper == -1) { return JSMN_ERROR_INVAL; } break; From a01d301373595892e2e5ff77dd4e7715f7897f11 Mon Sep 17 00:00:00 2001 From: zlolik Date: Sun, 2 Oct 2016 07:51:52 +0300 Subject: [PATCH 3/4] some tests for unmatched brackets added --- test/tests.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/tests.c b/test/tests.c index a72689e..4247035 100644 --- a/test/tests.c +++ b/test/tests.c @@ -357,6 +357,27 @@ int test_nonstrict(void) { return 0; } +int test_unmatched_brackets(void) { + const char *js; + js = "\"key 1\": 1234}"; + check(parse(js, JSMN_ERROR_INVAL, 2)); + js = "{\"key 1\": 1234"; + check(parse(js, JSMN_ERROR_PART, 3)); + js = "{\"key 1\": 1234}}"; + check(parse(js, JSMN_ERROR_INVAL, 3)); + js = "\"key 1\"}: 1234"; + check(parse(js, JSMN_ERROR_INVAL, 3)); + js = "\"key {1\": 1234"; + check(parse(js, 2, 2, + JSMN_PRIMITIVE, "\"key {1\"", + JSMN_PRIMITIVE, "1234")); + js = "\"key 1\": {1234}"; + check(parse(js, JSMN_ERROR_INVAL, 3)); + js = "{\"key 1\"}: 1234"; + check(parse(js, JSMN_ERROR_INVAL, 3)); + return 0; +} + int main(void) { test(test_empty, "test for a empty JSON objects/arrays"); test(test_object, "test for a JSON objects"); @@ -373,6 +394,7 @@ int main(void) { test(test_issue_27, "test issue #27"); test(test_count, "test tokens count estimation"); test(test_nonstrict, "test for non-strict mode"); + test(test_unmatched_brackets, "test for unmatched brackets"); printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed); return (test_failed > 0); } From c3131d05a6db72c1ddefb1ad4c95ddbc604d1fa6 Mon Sep 17 00:00:00 2001 From: pt300 Date: Sun, 2 Oct 2016 10:37:29 +0200 Subject: [PATCH 4/4] Changed unmatched bracket tests --- test/tests.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/tests.c b/test/tests.c index 4247035..d5f0c53 100644 --- a/test/tests.c +++ b/test/tests.c @@ -369,12 +369,10 @@ int test_unmatched_brackets(void) { check(parse(js, JSMN_ERROR_INVAL, 3)); js = "\"key {1\": 1234"; check(parse(js, 2, 2, - JSMN_PRIMITIVE, "\"key {1\"", + JSMN_STRING, "key {1", 1, JSMN_PRIMITIVE, "1234")); - js = "\"key 1\": {1234}"; - check(parse(js, JSMN_ERROR_INVAL, 3)); - js = "{\"key 1\"}: 1234"; - check(parse(js, JSMN_ERROR_INVAL, 3)); + js = "{{\"key 1\": 1234}"; + check(parse(js, JSMN_ERROR_PART, 4)); return 0; }