zserge / jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
MIT License
3.68k stars 783 forks source link

Parser pass invalid JSON when PARENT_LINKS is enabled #81

Closed edsiper closed 8 years ago

edsiper commented 8 years ago

jsmn parser pass the following wrong JSON message when JSMN_PARENT_LINKS is enabled:

test case

int main()
{
  int ret;
  char *json = "\"key 1\": 1234}";
  jsmntok_t *tokens;
  jsmn_parser parser;

  jsmn_init(&parser);

  tokens = calloc(32, sizeof(jsmntok_t));
  ret = jsmn_parse(&parser, json, strlen(json), tokens, 32);
  free(tokens);
  printf("ret=%i\n", ret);

  return 0;
}

With JSMN_PARENT_LINKS set, the parser returns 2, when disabled it returns -2 (expected).

edsiper commented 8 years ago

any update on this ?

pt300 commented 8 years ago

Problem seems to be around #ifdef on line 186. Without JSMN_PARENT_LINKS function returns on line 218 which won't be executed with that define.

pt300 commented 8 years ago

It would be nice if somebody could test that fix.

zlolik commented 8 years ago

@pt300 I have added some tests and couple of them failed. zlolik/jsmn@a01d301373595892e2e5ff77dd4e7715f7897f11

pt300 commented 8 years ago

@zlolik I think you overdone these tests a bit. pt300/jsmn@c3131d0

zlolik commented 8 years ago

@pt300 You are right, with JSMN_STRING is my fault. And other cases are more about general json stricting, maybe #52. With some playing I have found one more invalid json interpreted as valid with PARENT_LINKS:

js = "{\"key 1\": 5678}: 1234}";

And this one return different error with or without JSMN_STRICT - this may be a feature:

js = "{\"key 1\": { b1234 } : c4321}";

New tests are here: zlolik/jsmn@29c999cd94bab9b7c6c0da4b04cd0df1508e97c1

edsiper commented 8 years ago

thanks for reviewing this. After the latest changes, is it safe to use the current version in GIT master or is there anything pending ?

pt300 commented 8 years ago

From what I know my pull request wasn't accepted so to benefit that fix you'd have to use my fork