Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow using an object or an array as a name of an object member. #143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion jsmn.c
Expand Up @@ -170,7 +170,14 @@ int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
if (token == NULL)
return JSMN_ERROR_NOMEM;
if (parser->toksuper != -1) {
tokens[parser->toksuper].size++;
jsmntok_t *t = &tokens[parser->toksuper];
#ifdef JSMN_STRICT
/* In strict mode an object or array can't become a key */
if (t->type == JSMN_OBJECT) {
return JSMN_ERROR_INVAL;
}
#endif
t->size++;
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
Expand Down
27 changes: 25 additions & 2 deletions test/tests.c
Expand Up @@ -380,8 +380,30 @@ int test_unmatched_brackets(void) {
JSMN_OBJECT, 0, 16, 1,
JSMN_STRING, "key {1", 1,
JSMN_PRIMITIVE, "1234"));
js = "{{\"key 1\": 1234}";
check(parse(js, JSMN_ERROR_PART, 4));
js = "{\"key 1\":{\"key 2\": 1234}";
check(parse(js, JSMN_ERROR_PART, 5));
return 0;
}

int test_object_key(void) {
const char *js;

js = "{\"key\": 1}";
check(parse(js, 3, 3,
JSMN_OBJECT, 0, 10, 1,
JSMN_STRING, "key", 1,
JSMN_PRIMITIVE, "1"));
#ifdef JSMN_STRICT
js = "{true: 1}";
check(parse(js, JSMN_ERROR_INVAL, 3));
js = "{1: 1}";
check(parse(js, JSMN_ERROR_INVAL, 3));
js = "{{\"key\": 1}: 2}";
check(parse(js, JSMN_ERROR_INVAL, 5));
js = "{[1,2]: 2}";
check(parse(js, JSMN_ERROR_INVAL, 5));
#endif

return 0;
}

Expand All @@ -402,6 +424,7 @@ int main(void) {
test(test_count, "test tokens count estimation");
test(test_nonstrict, "test for non-strict mode");
test(test_unmatched_brackets, "test for unmatched brackets");
test(test_object_key, "test for key type");
printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed);
return (test_failed > 0);
}