Commits

Anonymous committed 09ad3d4

fixed: invalid object/arrays were passed successfully in tree mode; added tests for this case

  • Participants
  • Parent commits 6a3ecdc

Comments (0)

Files changed (2)

 				token = &tokens[parser->toknext - 1];
 				for (;;) {
 					if (token->start != -1 && token->end == -1) {
+						if (token->type != type) {
+							return JSMN_ERROR_INVAL;
+						}
 						token->end = parser->pos + 1;
 						parser->toksuper = token->parent;
 						break;
 	return 0;
 }
 
+int test_objects_arrays() {
+	int i;
+	int r;
+	jsmn_parser p;
+	jsmntok_t tokens[10];
+	const char *js;
+
+	js = "[10}";
+	jsmn_init(&p);
+	r = jsmn_parse(&p, js, tokens, 10);
+	check(r == JSMN_ERROR_INVAL);
+
+	js = "[10]";
+	jsmn_init(&p);
+	r = jsmn_parse(&p, js, tokens, 10);
+	check(r == JSMN_SUCCESS);
+
+	js = "{\"a\": 1]";
+	jsmn_init(&p);
+	r = jsmn_parse(&p, js, tokens, 10);
+	check(r == JSMN_ERROR_INVAL);
+
+	js = "{\"a\": 1}";
+	jsmn_init(&p);
+	r = jsmn_parse(&p, js, tokens, 10);
+	check(r == JSMN_SUCCESS);
+
+	return 0;
+}
+
 int main() {
 	test(test_empty, "general test for a empty JSON objects/arrays");
 	test(test_simple, "general test for a simple JSON string");
 	test(test_partial_array, "test partial array reading");
 	test(test_array_nomem, "test array reading with a smaller number of tokens");
 	test(test_unquoted_keys, "test unquoted keys (like in JavaScript)");
+	test(test_objects_arrays, "test objects and arrays");
 	printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed);
 	return 0;
 }