38 Commits
Author SHA1 Message Date
Denis Gregor e222189360 Check the whole RFC 8259 number grammar in strict mode
Strict mode rejected leading zeros but nothing else about the shape of a
number, so several malformed numbers parsed and were then written back out
verbatim, producing JSON that other parsers reject:

    $ echo '[2.e3]' | ./json_parse -s -
    [ 2.e3 ]

RFC 8259 requires a mandatory integer part and at least one digit in both the
fraction and the exponent:

    number = [ minus ] int [ frac ] [ exp ]
    int    = zero / ( digit1-9 *DIGIT )
    frac   = decimal-point 1*DIGIT
    exp    = e [ minus / plus ] 1*DIGIT

so "1.", "-2.", "2.e3", "2.e+3", "0.e1", "-.123", "1e" and "1e+" are all
invalid. Walk the accumulated text against that grammar instead. The leading
zero rule from the previous check is part of the same walk rather than a
separate test, and its cases keep their coverage.

Only JSON_TOKENER_STRICT is affected; the default tokener stays as lenient as
it was. Measured against JSONTestSuite (318 files): strict mode went from 44
to 37 files accepted that the suite says must be rejected, default mode stayed
at 71, and nothing that must be accepted regressed in either mode.
2026-08-16 20:26:15 +03:00
Javid Khan ff18c75286 reject leading zeros in strict-mode number parsing 2026-07-28 13:27:25 +05:30
Shane F. Carr 7974657c56 Fix code and update tests 2025-07-30 17:40:56 -07:00
Shane F. Carr dbefb48948 Add tests with current behavior 2025-07-30 17:39:47 -07:00
Eric Hawicz 7cee5237dc Issue #867 - also disallow control characters in keys 2025-04-03 21:16:29 -04:00
Eric Hawicz 565f181f65 Fix issue #875: cast to unsigned char so bytes above 0x7f aren't interpreted as negative, which was causing the strict-mode control characters check to incorrectly trigger. 2024-11-08 22:20:40 -05:00
Eric Hawicz 87900c0a2e Fix the expected output for test_parse 2024-09-15 11:59:05 -04:00
Eric Haszlakiewicz bc35549f90 Update expected output for test_parse, missed in the previous commit. 2022-10-30 19:42:42 +00:00
Eric Haszlakiewicz f23486a321 In the json_tokener_state_number case, explicitly adjust what "number" characters are allowed based on the exact micro-state that we're in, and check for invalid following characters in a different way, to allow a valid json_type_number object to be returned at the top level.
This causes previously failing strings like "123-456" to return a valid json_object with the appropriate value.  If you care about the trailing content, call json_tokener_parse_ex() and check the parse end point with json_tokener_get_parse_end().
2020-06-29 02:14:26 +00:00
Eric Haszlakiewicz 6eac6986c9 Fix incremental parsing of invalid numbers with exponents, such as "0e+-" and "12.3E12E12", while still allowing "0e+" in non-strict mode.
Deprecate the json_parse_double() function from json_util.h
2020-06-27 15:35:04 +00:00
Eric Haszlakiewicz a68566bf6a Issue #616: Change the parsing of surrogate pairs in unicode escapes so it uses a couple of additional states instead of assuming the low surrogate is already present, to ensure that we correctly handle various cases of incremental parsing. 2020-06-21 18:29:57 +00:00
Eric Haszlakiewicz 197e372464 In test_parse, fix lengths passed during a couple of incremental tests.
Add an optional way to use an incremental chunk size ($TEST_PARSE_CHUNKSIZE)
for all of the single_basic_parse tests, in additional to the regular way.
2020-06-21 17:36:38 +00:00
dota17 c1b872d817 fix issue 616: support the surrogate pair in split file. 2020-06-08 17:19:32 +08:00
Eric Haszlakiewicz f6f76f9430 Add a JSON_TOKENER_ALLOW_TRAILING_CHARS flag for json_tokener_set_flags() to allow multiple objects to be parsed from input even when JSON_TOKENER_STRICT is set. 2020-04-21 03:53:44 +00:00
dota17 3c3b5920f7 add uint64 data to json-c 2020-02-25 14:51:35 +08:00
dota17 787a8b3f1c update code 2020-01-20 10:41:24 +08:00
dota17 eca74dcccf test utf8 2020-01-10 18:33:14 +08:00
dota17 5fe4448541 update tsetcase for tokener_c 2020-01-02 19:19:12 +08:00
chenguoping 1446572997 add testcases of object and token 2019-12-27 15:07:00 +08:00
Eric Haszlakiewicz da4b34355d Add an parse test for content starting with a UTF8 BOM, which is _not_ a valid start to a JSON message. 2018-03-25 18:23:42 -04:00
Eric Haszlakiewicz 9c9529f798 Update test_parse.expected to match the code. 2017-11-27 08:48:08 -05:00
Eric Haszlakiewicz ba11d334db Save proper expected output for test_parse from the previous commit. 2017-11-27 00:08:59 -05:00
Eric Haszlakiewicz 4d1e0b4409 Issue #371: fix parsing of "-Infinity" (although in a somewhat different location than PR#372 used), and add a case to test_parse to check for this. 2017-11-26 22:44:39 -05:00
Eric Haszlakiewicz 2a22858fe7 Add a few extra cases to test_parse to show how trailing bytes are handled, especially for parsing "Infinity". 2017-11-26 22:34:43 -05:00
Eric Haszlakiewicz b8738dd623 Rewrite test_basic_parse() to factor out a single_basic_parse() to avoid lots of duplicate boiler plate code.
Also, emit the input string in the output so it's easier to see what's going on.
2017-11-26 22:33:36 -05:00
Eric Haszlakiewicz 8c86207258 Add a few more cases to test_parse to provide some examples of how parsing works; should help address the questions raised in Issue #302. 2017-02-26 16:22:45 +00:00
Jan Gerhards ca7a1973e2 bugfix: floating point representaion without fractional part
closes https://github.com/json-c/json-c/issues/278
2016-11-27 11:47:00 +01:00
Eric Haszlakiewicz 33339ae595 Use json_visit() to clear the serializer data set when parsing so the output from test_parse reflects the actual values parsed. 2016-10-29 15:13:16 -04:00
Eric Haszlakiewicz 36a28fcb0c Issue #275: fix out of bounds read when handling unicode surrogate pairs. 2016-10-05 23:15:51 -04:00
Eric Haszlakiewicz 316da85818 Fix issue #201: add a JSON_C_TO_STRING_NOSLASHESCAPE flag to turn off escaping of forward slashes. 2015-11-28 20:00:30 -06:00
chl 99d8fc975e Tightening the number parsing algorithm
Some badly formated "numbers" could get partly parsed,
resulting in truncated results instead of raising an
error.

Examples :
 '1.2.3'      -> (double)1.2
 '2015-01-15' -> (int)2015

This patch is not perfect (ex: input can still end with a 'E', which
is forbidden by json.org doc) but should avoid non-sensically
formated input.

Tests added.
2015-02-05 01:50:37 +01:00
Eric Haszlakiewicz 05da316b9c Issue #103: allow Infinity and -Infinity to be parsed. 2014-03-22 17:28:40 -04:00
Eric Haszlakiewicz 0eedf3802f Issue#102 - add support for parsing "NaN". 2014-03-09 16:41:33 -04:00
Eric Haszlakiewicz 51993c28c2 Added a json_object_new_double_s() convenience function to allow an exact string representation of a double to be specified when creating the object and use it in json_tokener_parse_ex() so a re-serialized object more exactly matches the input.
Add json_object_free_userdata() and json_object_userdata_to_json_string() too.
2013-09-11 20:27:39 -05:00
Eric Haszlakiewicz e8161a11bb Issue #15: add a way to set a JSON_TOKENER_STRICT flag to forbid commas at the end of arrays and objects. 2013-03-31 20:05:36 -05:00
Iskren Chernev 78b089bc1e Fixed test_parse for \f 2013-01-28 19:06:49 -08:00
Eric Haszlakiewicz 92f31bd99a Handle the \f escape sequence (the two characters: backslash followed by an f, not a literal formfeed) and extend the test_parse test to check all valid escape sequences. 2012-07-29 12:31:07 -05:00
Eric Haszlakiewicz c1b8891a13 Move the rest of the tests into the tests subdirectory. 2012-04-22 10:33:41 -05:00