Skip to content
Myr

Parse nan in JSON as NaN instead of triggering a parse error (#2712)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Fix memory leak for has(nan) jv_array_get() used to be responsible of freeing the input array, but since b5c4c3d67decec22d34f494a200af59bbcadcc80, it is no longer called if the key is nan. We need to free it manually to avoid leaking the array. * Parse nan in JSON as NaN instead of triggering a parse error Fixes #2021

authored by Emanuele Torre · committed by GitHub · parent 092fef7 · Browse files · 3 files+18 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+18 added−1 removed

  • tests:+12 added−0 removed
  • other:+6 added−1 removed

3 files+18 added−1 removed

modified:src/jv_aux.c+1 added
213 unmodified lines
214 jv_free(elem);215 } else if (jv_get_kind(t) == JV_KIND_ARRAY &&216 jv_get_kind(k) == JV_KIND_NUMBER) {217 if (jvp_number_is_nan(k)) {218 jv_free(t);219 ret = jv_false();220 } else {221 jv elem = jv_array_get(t, (int)jv_number_value(k));222 ret = jv_bool(jv_is_valid(elem));
469 unmodified lines
modified:src/jv_parse.c+5 added-1 removed
488 unmodified lines
489 jv v;490 switch (p->tokenbuf[0]) {491 case 't': pattern = "true"; plen = 4; v = jv_true(); break;492 case 'f': pattern = "false"; plen = 5; v = jv_false(); break;493 case 'n': pattern = "null"; plen = 4; v = jv_null(); break;493 case 'n':494 // if it starts with 'n', it could be a literal "nan"495 if (p->tokenpos != 3) {496 pattern = "null"; plen = 4; v = jv_null();497 }498 }499 if (pattern) {500 if (p->tokenpos != plen) return "Invalid literal";501 for (int i=0; i<plen; i++)
379 unmodified lines
modified:tests/jq.test+12 added
1797 unmodified lines
1798[10,19,27,34]179918001801# { $__loc__ } works18021803{ a, $__loc__, c }1804{"a":[1,2,3],"b":"foo","c":{"hi":"hey"}}1805{"a":[1,2,3],"__loc__":{"file":"<top-level>","line":1},"c":{"hi":"hey"}}180618071808# nan is parsed as a valid NaN value from JSON18091810fromjson | isnan1811"nan"1812true18131814tojson | fromjson1815{"a":nan}1816{"a":null}