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

Read JSON with options: Allow INF and NAN flag fails to read positive infinity values #64

Closed
dcascais opened this issue Feb 9, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@dcascais
Copy link

dcascais commented Feb 9, 2022

Description
When enabling the YYJSON_READ_ALLOW_INF_AND_NAN flag, the JSON read fails, returning an unexpected character error with code 6.

  • This happens with both array and object values.
  • This doesn't happen with negative infinity as those values are caught by the char_is_number(*cur) check, and handled accordingly.
  • Possible fix below.

Additional context

Running the code below results in these two errors:

  • Array JSON read error: unexpected character. Code: 6, at position: 28
  • Object JSON read error: unexpected character. Code: 6, at position: 78

How to reproduce

yyjson_read_flag flg = YYJSON_READ_ALLOW_INF_AND_NAN;

//
// Read array with nan and inf values
//
char arr_str[] = "[1, 123e999, nan, NaN, NAN, inf, infinity, Infinity, INF, -inf, -infinity, -Infinity, -INF]";
size_t arr_str_len = strlen(arr_str);
yyjson_read_err arr_err;

yyjson_doc *arr_doc = yyjson_read_opts(arr_str, arr_str_len, flg, NULL, &arr_err);
if (!arr_doc) {
    fprintf(stderr, "Array JSON read error: %s. Code: %u, at position: %ld\n", arr_err.msg, arr_err.code, arr_err.pos);
}
yyjson_doc_free(arr_doc);

//
// Read object with nan and inf values
//
char obj_str[] = "{\"small\": 1, \"large\": 123e999, \"nan1\": nan, \"nan2\": NaN, \"nan3\": NAN, \"inf1\": inf, \"inf2\": infinity, \"inf3\": Infinity, \"inf4\": INF, \"inf5\": -inf, \"inf6\": -infinity, \"inf7\": -Infinity, \"inf8\": -INF}";
size_t obj_str_len = strlen(obj_str);
yyjson_read_err obj_err;

yyjson_doc *obj_doc = yyjson_read_opts(obj_str, obj_str_len, flg, NULL, &obj_err);
if (!obj_doc) {
    fprintf(stderr, "Object JSON read error: %s. Code: %u, at position: %ld\n", obj_err.msg, obj_err.code, obj_err.pos);
}
yyjson_doc_free(obj_doc);

Possible fix
Replace all instances of:
if (has_flag(ALLOW_INF_AND_NAN) && *cur == 'N')
with:
if (has_flag(ALLOW_INF_AND_NAN) && *cur == 'i' || *cur == 'I' || *cur == 'N')

@ibireme ibireme added the bug Something isn't working label Feb 10, 2022
@ibireme
Copy link
Owner

ibireme commented Feb 10, 2022

Thanks, fixed: 84c683a

@ibireme ibireme closed this as completed Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants