44 Commits

Author SHA1 Message Date
Eric Hawicz
ff8ed0f094 Issue #881: don't allow json_tokener_new_ex() with a depth < 1 2024-11-17 22:11:24 -05:00
Alan Urmancheev
0741ce3918 json_tokener.h: json_tokener_parse_verbose: fix typo 2024-04-16 14:56:00 +00:00
Eric Hawicz
ad8b8afa7d Take 2 fixing the placement of json_tokener_error_memory in the enum. (json_tokener_error_size is an actual error, *not* a measure of the size of the enum!) 2023-09-22 22:26:21 -04:00
Eric Hawicz
3dad6941da Move the json_tokener_error_memory entry to the end of enum json_tokener_error to restore binary compatibility with 0.16 2023-08-12 13:54:50 -04:00
Tobias Stoeckmann
9e6acc9a4e json_tokener_parse_ex: handle out of memory errors
Do not silently truncate values or skip entries if out of memory errors
occur.

Proof of Concept:

- Create poc.c, a program which creates an eight megabyte large json
  object with key "A" and a lot of "B"s as value, one of them is
  UTF-formatted:

```c
 #include <err.h>
 #include <stdio.h>
 #include <string.h>

 #include "json.h"

 #define STR_LEN (8 * 1024 * 1024)
 #define STR_PREFIX "{ \"A\": \""
 #define STR_SUFFIX "\\u0042\" }"

int main(void) {
  char *str;
  struct json_tokener *tok;
  struct json_object *obj;

  if ((tok = json_tokener_new()) == NULL)
    errx(1, "json_tokener_new");

  if ((str = malloc(STR_LEN)) == NULL)
    err(1, "malloc");
  memset(str, 'B', STR_LEN);
  memcpy(str, STR_PREFIX, sizeof(STR_PREFIX) - 1);
  memcpy(str + STR_LEN - sizeof(STR_SUFFIX), STR_SUFFIX, sizeof(STR_SUFFIX));

  obj = json_tokener_parse(str);
  free(str);

  printf("%p\n", obj);
  if (obj != NULL) {
    printf("%.*s\n", 50, json_object_to_json_string(obj));
    json_object_put(obj);
  }

  json_tokener_free(tok);
  return 0;
}
```
- Compile and run poc, assuming you have enough free heap space:
```
gcc $(pkg-config --cflags --libs) -o poc poc.c
./poc
0x559421e15de0
{ "A": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
```
- Reduce available heap and run again, which leads to truncation:
```
ulimit -d 10000
./poc
0x555a5b453de0
{ "A": "B" }
```
- Compile json-c with this change and run with reduced heap again:
```
ulimit -d 10000
./poc
(nil)
```

The output is limited to 70 characters, i.e. json-c parses the 8 MB
string correctly but the poc does not print all of them to the screen.

The truncation occurs because the parser tries to add all chars up
to the UTF-8 formatted 'B' at once. Since memory is limited to 10 MB
there is not enough for this operation. The parser does not fail but
continues normally.

Another possibility is to create a json file close to 2 GB and run a
program on a system with limited amount of RAM, i.e. around 3 GB. But
ulimit restrictions are much easier for proof of concepts.

Treat memory errors correctly and abort operations.
2022-03-20 17:42:47 +01:00
Eric Haszlakiewicz
10a9ac245e Issue #642: improve docs for json_tokener.h and json_object_object_add(). 2020-07-11 04:04:58 +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
36118b681e Rearrange the json_tokener_state_escape_unicode case in json_tokener to simplify the code slightly and make it a bit easier to understand.
While here, drop the utf8_replacement_char that is unnecesarily added if we run out of input in the middle of a unicode escape.  No other functional changes (yet).
2020-06-21 03:10:55 +00:00
Eric Haszlakiewicz
50179fb09f Update the json_tokener_parse_ex() docs to clarify that the final '\0' character is to be included in length passed in. 2020-06-21 02:35:42 +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
Eric Haszlakiewicz
0a95f98b8d Explicitly mark several things in json_tokener deprecated. 2020-04-18 02:42:46 +00:00
Eric Haszlakiewicz
5cc11289b4 Make json_tokener_validate_utf8() internal to json_tokener.c, and improve the docs a bit. 2020-04-18 02:02:06 +00:00
dota17
8b162c4b89 clang-format the files 2020-04-03 11:39:30 +08:00
dota17
c117d8a8a8 add the disabling formatting coments and adjust the partial code manuly 2020-04-03 11:28:04 +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
Eric Haszlakiewicz
eae040a84a Issue #488: use JSON_EXPORT on functions so they are properly exported on Windows. 2019-09-08 22:42:36 -04:00
Eric Haszlakiewicz
05b41b159e Add a json_tokener_get_parse_end() function to replace direct access of tok->char_offset. 2019-09-08 21:35:37 -04:00
andy5995
506a32d4ab json_object.c:set errno in json_object_get_double()
closes #422
2018-11-26 21:12:06 -06:00
Christopher Head
31d4d3dee7 Document refcount of json_tokener_parse_ex return 2018-08-30 09:18:29 -07:00
Eric Haszlakiewicz
f2f103b986 Add a brief overview of each file to the docs. 2017-12-06 00:20:59 -05:00
Eric Haszlakiewicz
11ab365324 Deprecated a few typedefs, add docs on json_object_iter, move json_number_chars and json_hex-chars to json_object_private.h. 2017-12-05 09:20:59 -05:00
David Henot
a9f265d166 Fix library build with Visual Studio 2017-04-26 14:11:17 +02:00
Leon M. George
482e771af1 fix doc in tokener header file 2017-02-20 04:41:55 +01:00
Michael Vetter
fcf5ad1bd6 Remove trailing whitespace 2014-08-26 14:48:59 +02:00
Michael Clark
64e36901a0 Patch to address the following issues:
* CVE-2013-6371: hash collision denial of service
* CVE-2013-6370: buffer overflow if size_t is larger than int
2014-04-09 13:48:21 +08:00
Eric Haszlakiewicz
f9136f6852 Make the json_tokener_errors array local. It has been deprecated for a while, and json_tokener_error_desc() should be used instead. 2014-03-22 21:41:24 -04:00
Eric Haszlakiewicz
05da316b9c Issue #103: allow Infinity and -Infinity to be parsed. 2014-03-22 17:28:40 -04: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
Remi Collet
197cb1d1c1 Make maximum recursion depth a runtime option 2012-11-27 09:01:45 +01:00
Eric Haszlakiewicz
1489b081a7 For the prototype for json_tokener_error_desc(). 2012-03-31 22:49:58 -05:00
Eric Haszlakiewicz
2f9091f559 Add json_tokener_get_error() and json_tokener_error_desc() to better encapsulate the process of retrieving errors while parsing.
Add documentation for the json_tokener_parse_ex() function.
2012-02-22 08:24:40 -06:00
Jehiah Czebotar
a503ee8217 add json_tokener_parse_verbose, and return NULL on parser errors
git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@62 327403b1-1117-474d-bef2-5cb71233fd97
2010-12-08 03:52:07 +00:00
Christopher Watford
b1a22ac85f Add const qualifier to the json_tokener_parse functions
Eric Haszlakiewicz, EHASZLA at transunion dot com



git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@42 327403b1-1117-474d-bef2-5cb71233fd97
2009-07-08 04:02:05 +00:00
Michael Clark
aaec1ef3c5 * Don't use this as a variable, so we can compile with a C++ compiler
* Add casts from void* to type of assignment when using malloc 
  * Add #ifdef __cplusplus guards to all of the headers
  * Add typedefs for json_object, json_tokener, array_list, printbuf, lh_table
    Michael Clark, <michael@metaparadigm.com>


git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@33 327403b1-1117-474d-bef2-5cb71233fd97
2009-02-25 02:31:32 +00:00
Michael Clark
a3272546b1 fix to allow compile with -Werror
-- Geoffrey Young, geoff at modperlcookbook dot org



git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@21 327403b1-1117-474d-bef2-5cb71233fd97
2008-01-26 01:10:47 +00:00
Michael Clark
14862b1ef3 Patch allows for json-c compile with -Werror and not fail due to
-Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
Geoffrey Young, geoff at modperlcookbook dot org



git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@20 327403b1-1117-474d-bef2-5cb71233fd97
2007-12-07 02:50:42 +00:00
Michael Clark
c8f4a6e7de * Fix bug with use of capital E in numbers with exponents
Mateusz Loskot, mateusz at loskot dot net
  * Add stddef.h include



git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@19 327403b1-1117-474d-bef2-5cb71233fd97
2007-12-07 02:44:24 +00:00
Michael Clark
a850f8e29e * Add escaping of backslash to json output
* Add escaping of foward slash on tokenizing and output
  * Changes to internal tokenizer from using recursion to
    using a depth state structure to allow incremental parsing


git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@14 327403b1-1117-474d-bef2-5cb71233fd97
2007-03-13 08:26:26 +00:00
Michael Clark
837240f75f * Fix bug in escaping of control characters
Johan Bj�rklund, johbjo09 at kth dot se
  * Remove include "config.h" from headers (should only
    be included from .c files)
    Michael Clark <michael@metaparadigm.com>


git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@12 327403b1-1117-474d-bef2-5cb71233fd97
2007-03-13 08:26:25 +00:00
Michael Clark
f6a6e486ff * Make headers C++ compatible by change *this to *obj
* Add ifdef C++ extern "C" to headers
  * Use simpler definition of min and max in bits.h
    Larry Lansing, llansing at fuzzynerd dot com

  * Remove automake 1.6 requirement
  * Move autogen commands into autogen.sh. Update README
  * Remove error pointer special case for Windows
  * Change license from LGPL to MIT
    Michael Clark <michael@metaparadigm.com>


git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@10 327403b1-1117-474d-bef2-5cb71233fd97
2007-03-13 08:26:23 +00:00
Michael Clark
4504df7117 * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
Added a Win32/Win64 compliant implementation of vasprintf
  * debug.c - C. Watford (christopher dot watford at gmail dot com)
    Removed usage of vsyslog on Win32/Win64 systems, needs to be handled
    by a configure script
  * json_object.c - C. Watford (christopher dot watford at gmail dot com)
    Added scope operator to wrap usage of json_object_object_foreach, this
    needs to be rethought to be more ANSI C friendly
  * json_object.h - C. Watford (christopher dot watford at gmail dot com)
    Added Microsoft C friendly version of json_object_object_foreach
  * json_tokener.c - C. Watford (christopher dot watford at gmail dot com)
    Added a Win32/Win64 compliant implementation of strndup
  * json_util.c - C. Watford (christopher dot watford at gmail dot com)
    Added cast and mask to suffice size_t v. unsigned int conversion
    correctness
  * json_tokener.c - sign reversal issue on error info for nested object parse
    spotted by Johan Bj�rklund (johbjo09 at kth.se)
  * json_object.c - escape " in json_escape_str
  * Change to automake and libtool to build shared and static library
    Michael Clark <michael@metaparadigm.com>


git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@4 327403b1-1117-474d-bef2-5cb71233fd97
2007-03-13 08:26:20 +00:00
Michael Clark
f0d08887b8 import of version 0.1
git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@2 327403b1-1117-474d-bef2-5cb71233fd97
2007-03-13 08:26:18 +00:00