62 Commits

Author SHA1 Message Date
Eric Hawicz
fa7308b3de Run travis builds using iOS flags, to attempt to reproduce Issue #808. 2023-07-30 10:23:21 -04:00
Eric Hawicz
9b36c72945 Merge pull request #772 from cosmo-ray/color
add JSON_C_TO_STRING_COLOR option
2023-07-13 18:04:48 -04:00
Matthias Gatto
9803032b9d add JSON_C_TO_STRING_COLOR option
This option enable color in json_object_to_json_string_ext.
I've try to made something similar to jq output,
but I've color true/false and null in purple,
as it's what is common color scheme used in programing language in emacs.

also add a '-c' option into json_parser to test it.

note: that I could have done a color() function similar to
what is done with indent(), but as the code is pretty small
I've keep it as it. so if you want  me to use a subfunction
tell me and I'll do it.

Signed-off-by: Matthias Gatto <matthias.gatto@protonmail.com>
2023-07-10 20:45:20 +02:00
Eric Hawicz
bdfdb5fe10 Skip apps when we're included in someone else's build. Inspired by ssrlive in PR #813. 2023-07-06 20:56:49 -04:00
Eric Hawicz
11311ed2a3 Fix the -f option to apps/json_parse, add a -F <arg> option to specify arbitrary flags to pass to json_object_to_json_string_ext(). 2023-07-04 12:00:31 -04:00
Eric Hawicz
e9d3ab209a Merge pull request #759 from c3h2-ctf/truncation
json_tokener_parse_ex: handle out of memory errors
2023-07-04 11:45:57 -04:00
Eric Haszlakiewicz
4d529f92dc Issue #688: Only exclude generated doc/* files, keep the CMakeLists.txt, etc... so the nodoc tarball can still be built. 2023-07-01 18:02:00 +00:00
Eric Haszlakiewicz
d0f32a5a43 Stop linking a copy of _json_c_strerror() (strerror_override.c) into the tests.
That hasn't been needed since since commit 6068d3f, which changed that code to
 check an env var instead ("_JSON_C_STRERROR_ENABLE").
Fixes issue #812, about dup symbols in static builds with clang.
2023-03-28 23:08:39 +00:00
Eric Hawicz
efd536af48 Merge pull request #810 from dijonkitchen/patch-1
docs: update to Internet Standard reference
2023-03-10 14:54:44 -05:00
JC (Jonathan Chen)
4d5507f5dc docs: update to Internet Standard reference 2023-03-10 11:10:29 -05:00
Eric Haszlakiewicz
d1716fe431 Bump up the minimum cmake version to 3.9.
This gets us up to a version that supports features we're already using
(i.e. add_compile_options), but stops short of a cmake that requires
c++11, which some OSes still don't support.
Closes issue #774
2023-02-23 01:01:14 +00:00
Eric Haszlakiewicz
1741bcd3ea Issue #570: note brief instructions for building on Android. 2023-01-05 00:25:09 +00:00
Eric Hawicz
343f24f920 Merge pull request #804 from yrashk/cmp-0042
Problem: cmake 3.25.1 warns about CMP0042 not being set
2022-12-27 09:32:33 -05:00
Eric Hawicz
6ec5e5842d Merge pull request #803 from yrashk/patch-2
Problem: confusing error message in snprintf_compat.h
2022-12-27 09:26:30 -05:00
Eric Hawicz
b6ba9429d0 Merge pull request #802 from yrashk/patch-1
Problem: modern CMake warns about version 2.8
2022-12-27 09:25:58 -05:00
Yurii Rashkovskii
3cc0c47221 Problem: cmake 3.25.1 warns about CMP0042 not being set
Solution: set it explictly to OLD behavior
2022-12-23 15:05:46 -08:00
Yurii Rashkovskii
7b971b52aa Problem: confusing error message in snprintf_compat.h
Solution: fix it to reflect what's happening properly
2022-12-23 14:35:36 -08:00
Yurii Rashkovskii
debe8b70b7 Problem: modern CMake warns about version 2.8
The warning is as follows:

```
Compatibility with CMake < 2.8.12 will be removed from a future version of CMake.
```

Solution: set it at 2.8.12
2022-12-23 14:33:31 -08:00
Eric Hawicz
79c147203e Merge pull request #796 from fedefrancescon/test-add-int-get
Added Test for get int functions
2022-11-16 22:23:11 -05:00
Federico Francescon
1a2fdc49e7 Fixed test_int_get expected output 2022-11-16 01:33:02 +01:00
Federico Francescon
b1fb1508ee Added basic tests for json_object_get_int, json_object_get_int64, json_object_get_uint64 2022-11-16 00:49:18 +01: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
d6f46ae104 Explicitly check for integer overflow/underflow when parsing integers with JSON_TOKENER_STRICT. 2022-10-30 19:39:30 +00:00
Eric Haszlakiewicz
c50bf9df9c Apply same EINVAL handling to json_parse_uint64() as was done for json_parse_int64(). Document that overflow/underflow for these functions is not an error, but sets errno=ERANGE. 2022-10-30 03:25:32 +00:00
Eric Haszlakiewicz
57bef5edc4 Issue #792 - set errno=EINVAL if parsing the string in json_parse_int64 fails, to match the docs for json_object_get_int. 2022-10-26 02:19:38 +00:00
Eric Hawicz
777dd06be8 Merge pull request #790 from Sarcares/patch-1
Small update to README file
2022-09-13 22:15:48 -04:00
Luca Mannella
84248a7884 Small update to README file
it could be necessary to execute make install
2022-09-12 17:29:47 +02:00
Eric Hawicz
81f0807b63 Merge pull request #784 from rouault/get_time_seed_coverity_scan_silence
get_time_seed(): silence warning emitted by Coverity Scan static analyzer
2022-08-16 18:10:06 -04:00
Even Rouault
57ea393004 get_time_seed(): silence warning emitted by Coverity Scan static analyzer
It warns about the return of time() being truncated to 32 bit, which is
not an issue here.
(this warning was emitted because of the https://github.com/OSGeo/gdal
project embedding a copy of libjson-c and running Coverity Scan
analysis)
2022-08-16 11:11:58 +02:00
Eric Hawicz
9417f4e726 Merge pull request #783 from kraj/master
Fix build with clang-15+
2022-08-14 08:19:19 -04:00
Khem Raj
257b29c991 json_pointer.c: Move idx_val declaration to top of function
This helps compiling with MS compiler, error seems to be
due to defining a variable within the body of the function
its allowed in c99 but not in c89. This should fix build with
MSVC 16.0.40219.1 compiler from Visual Studio 14 2015

Signed-off-by: Khem Raj <raj.khem@gmail.com>
2022-08-14 00:46:28 -07:00
Khem Raj
d1deed499f json_inttypes.h: Define ssize_t on windows platforms
Signed-off-by: Khem Raj <raj.khem@gmail.com>
2022-08-13 21:45:11 -07:00
Khem Raj
6eca65617a Fix build with clang-15+
Fixes
json_util.c:63:35: error: a function declaration without a prototype is deprecated in all versions of C [-We
rror,-Wstrict-prototypes]
const char *json_util_get_last_err()
                                  ^
                                   void

Signed-off-by: Khem Raj <raj.khem@gmail.com>
2022-08-13 20:47:20 -07:00
Eric Hawicz
ac4dfa44cb Merge pull request #782 from DimitriPapadopoulos/codespell
Fix typos found by codespell
2022-08-13 09:43:50 -04:00
Dimitri Papadopoulos
9462c0a7b5 Fix typos found by codespell
Do not fix typos from past commits found in release notes.
2022-08-13 15:47:40 +03:00
Eric Haszlakiewicz
bdd5e03d6e Apply some of the fixes from PR #740, although by using size_t instead of castings. 2022-07-31 19:28:48 +00:00
Eric Hawicz
4b0c6de760 Merge pull request #757 from c3h2-ctf/big
json_object_from_fd_ex: fail if file is too large
2022-07-31 14:27:32 -04:00
Eric Haszlakiewicz
253a5fa99d Issue #705: disable locale handling when building for a uClibc system because its duplocale() function (intentionally) crashes. 2022-07-30 20:27:35 +00:00
Eric Haszlakiewicz
9749b0cb66 When serializing with JSON_C_TO_STRING_PRETTY set, keep the opening and closing curly or square braces on same line for empty objects or arrays. Issue #778. 2022-07-30 19:27:14 +00:00
Eric Haszlakiewicz
2e9b7456a5 Update Travis links to point at travis-ci.com instead of travis-ci.org 2022-07-26 23:47:58 +00:00
Eric Haszlakiewicz
5282e73600 Entirely drop mode bits from open(O_RDONLY) to avoid warnings on certain platforms. Fixes issue #779. 2022-07-26 23:43:30 +00:00
Eric Haszlakiewicz
8b35a78d29 Trim a few travis builds, update others to more recent toolchains. 2022-07-24 20:37:14 +00:00
Eric Haszlakiewicz
22773b1e51 Fix the expected output for test_set_serializer. 2022-07-24 19:00:27 +00:00
Eric Haszlakiewicz
16208fc01a Add test to check for the memory leak mentioned in issue #781 2022-07-24 18:59:26 +00:00
Eric Hawicz
2a2d861bc0 Merge pull request #781 from dddaniel/master
Fix memory leak with emtpy strings in json_object_set_string
2022-07-24 14:15:42 -04:00
Daniel Danzberger
213bb5caa1 Fix memory leak with emtpy strings in json_object_set_string
When a json string object is updated with a bigger string, a new
malloc'ed buffer is used to store the new string and it's size is made
negative to indicate that an external buffer is in use.

When that same json string object get's updated again with an empty
stirng (size = 0), the new external malloc'ed buffer is still used.
But the fact that the new size value is not negative removes the
indicator that the externally malloc'ed buffer is used.

This becomes a problem when the object get's updated again with any
other string, because a new buffer will be malloced and linked to the
object while to old one won't be free'd.

This causes a memory leak when updating a json string with
json_object_set_stirng() which has previously been updated
with an empty string.

Example:
--
obj = json_object_new_string("data");
json_object_set_string(obj, "more data");
json_object_set_string(obj, "");
json_object_set_string(obj, "other data"); /* leaks */
--

This commit fixes the issue by free'ing the external buffer when an
empty string is set and use the internal one again.

Signed-off-by: Daniel Danzberger <daniel@dd-wrt.com>
2022-07-24 19:03:49 +02:00
Eric Haszlakiewicz
d28ac67dde Fix issue #775 - use a loop instead of list(TRANSFORM ...) to support cmake < 3.12. 2022-06-27 02:15:24 +00:00
Eric Hawicz
5d98f7825a Merge pull request #776 from stoeckmann/typo
Fix typo
2022-06-21 21:39:00 -04:00
Tobias Stoeckmann
f63573460c Fix typo 2022-06-21 14:39:01 +02:00
Eric Haszlakiewicz
6ee0a35a20 Fix #771/#768 by marking usage() as "noreturn" instead of using "FALLTHRU" in the case statement where it'd called. 2022-06-13 01:02:53 +00:00
Eric Hawicz
11546bfd07 Merge pull request #769 from An7ar35/issue-768
Issue #768
2022-06-03 23:14:16 -04:00
An7ar35
5d00384e0d missing 'FALLTHRU' added to sort out error messages when using "-Werror=implicit-fallthrough" flag 2022-06-01 12:36:33 +02:00
Eric Haszlakiewicz
49c3721a5a Unset NDEBUG so assert() is enabled in all tests. One more fix there when building with -DCMAKE_BUILD_TYPE=release. 2022-05-30 15:39:54 +00:00
Eric Haszlakiewicz
2f0942bdd7 Specify dependent libraries, including -lbsd, in a more consistent way so linking against a static json-c works better. Related issue #766
Use target_link_libraries, plus fill in Libs.private in json-c.pc so pkg-config --static --libs works appropriately.
Also, only link against libbsd when arc4random is actually found there.
2022-05-30 15:30:11 +00:00
Eric Haszlakiewicz
6ba1adf8ef Include the tests and apps directories at the _end_, since order in the
cmake rules matters, and fix compile warnings now that we're building
those sources with all the regular flags.
2022-05-30 14:33:16 +00:00
Eric Haszlakiewicz
86b46cac07 Add --disable-static and --disable-dynamic options to the cmake-configure script. 2022-05-30 14:31:08 +00:00
Eric Haszlakiewicz
f2fc1ca00a Note the DSIABLE_JSON_POINTER and DISABLE_EXTRA_LIBS build options on the README 2022-04-14 12:57:43 +00:00
Eric Haszlakiewicz
acccefd770 Add a "Getting Help" section to the README. 2022-04-14 12:55:32 +00:00
Eric Haszlakiewicz
de5a64888a Update the master branch to version 16.99 2022-04-14 12:29:39 +00:00
Eric Haszlakiewicz
7e3eba72db Clarify how to set the uploaded release tarballs to be publically readable. 2022-04-14 12:17:59 +00: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
Tobias Stoeckmann
5accae04bb json_object_from_fd_ex: fail if file is too large
If the input file is too large to fit into a printbuf then return an
error value instead of silently truncating the parsed content.

This introduces errno handling into printbuf to distinguish between an
input file being too large and running out of memory.
2022-03-20 13:17:37 +01:00
59 changed files with 875 additions and 520 deletions

1
.gitignore vendored
View File

@@ -27,6 +27,7 @@
/tests/test_double_serializer
/tests/test_float
/tests/test_int_add
/tests/test_int_get
/tests/test_json_pointer
/tests/test_locale
/tests/test_null

View File

@@ -1,153 +1,23 @@
language: cpp
matrix:
include:
# gcc
# xenial
# gcc 5 is the default on xenial
- os: linux
dist: xenial
compiler: gcc
addons:
apt:
packages:
- valgrind
- cppcheck
- doxygen
- cmake
env: CHECK="true"
# bionic
- os: linux
dist: bionic
compiler: gcc
env: MATRIX_EVAL="CC=gcc-6 && CXX=g++-6"
# gcc 7 is the default on bionic
- os: linux
dist: bionic
compiler: gcc
env: CHECK="true"
- os: linux
dist: bionic
compiler: gcc
env: MATRIX_EVAL="CC=gcc-8 && CXX=g++-8"
# clang
# xenial
- os: linux
dist: xenial
compiler: clang
addons:
apt:
sources:
- llvm-toolchain-xenial-5.0
packages:
- clang-5.0
- cmake
env: MATRIX_EVAL="CC=clang-5.0 && CXX=clang++-5.0"
- os: linux
dist: xenial
compiler: clang
addons:
apt:
sources:
- llvm-toolchain-xenial-6.0
packages:
- clang-6.0
- cmake
env: MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0"
# clang-7 is the default on xenial and bionic
- os: linux
dist: xenial
compiler: clang
addons:
apt:
packages:
- valgrind
- cppcheck
- doxygen
- cmake
env: CHECK="true"
# bionic
- os: linux
dist: bionic
compiler: clang
env: CHECK="true"
# osx
- os: osx
osx_image: xcode9.4
env: XCODE="true"
- os: osx
osx_image: xcode12.5
osx_image: xcode13.4
env: XCODE="true" CHECK="true"
# run coveralls
- os: linux
dist: xenial
compiler: gcc
addons:
apt:
packages:
- lcov
env: CHECK="true"
before_install:
- sudo gem install coveralls-lcov
- echo $CC
- echo $LANG
- echo $LC_ALL
- set -e
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then
eval "${MATRIX_EVAL}";
if [ -n "$MATRIX_EVAL" ] && [ "$TRAVIS_COMPILER" != "clang" ]; then
sudo apt-get install -y $CC;
fi;
fi
before_script:
- export CFLAGS="-fprofile-arcs -ftest-coverage"
- mkdir build && cd build && cmake ..
script:
- make
- make test
after_success:
- cd ..
- lcov -d build/ -b . -c -o build/all_coverage.info
- lcov -r build/all_coverage.info '/usr/*' '*CMakeFiles*' '*fuzz*' '*test*' -o build/coverage.info
- coveralls-lcov --verbose build/coverage.info
# allow_failures:
# - os: osx
before_install:
- echo $CC
- echo $LANG
- echo $LC_ALL
- set -e
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then
eval "${MATRIX_EVAL}";
if [ -n "$MATRIX_EVAL" ] && [ "$TRAVIS_COMPILER" != "clang" ]; then
sudo apt-get install -y $CC;
fi;
fi
before_script:
# XXX osx on travis doesn't work w/ set -e, so turn it off :(
- set +e
- mkdir -p build || echo "Failed to mkdir build"
- cd build || echo "Failed to cd build"
- cmake .. || echo "Failed to run cmake"
- cmake -B_builds -GXcode -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_DEPLOYMENT_TARGET=13.0 -DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO -DCMAKE_IOS_INSTALL_COMBINED=YES .. || { echo "Failed to run cmake" ; exit 1 ; }
script:
- make
# when using bionic, Travis seems to ignore the "addons" section, so installing the packages with apt-get...
- if [ -n "$CHECK" ]; then
if [ "$TRAVIS_OS_NAME" = "osx" ]; then
brew install doxygen;
else
if [ "$TRAVIS_DIST" = "bionic" ]; then
sudo apt-get install -y valgrind cppcheck doxygen;
fi;
fi;
make distcheck;
if type cppcheck &> /dev/null ; then cppcheck --error-exitcode=1 --quiet *.h *.c tests/ ; fi;
fi
- cmake --build _builds --config Release --target json-c-static -- -sdk iphonesimulator

View File

@@ -1,31 +1,26 @@
# Many projects still are stuck using CMake 2.8 is several places so it's good to provide backward support too. This is
# specially true in old embedded systems (OpenWRT and friends) where CMake isn't necessarily upgraded.
cmake_minimum_required(VERSION 2.8)
# CMake 3.9 was released in 2017/07
# As of 2023, many versions of Linux, NetBSD and FreeBSD provide,
# and many OpenWRT packages require, much newer CMake packages.
# We're stopping before 3.10 because that version starts requiring
# c++11, which isn't available on e.g HPUX.
cmake_minimum_required(VERSION 3.9)
if(POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif()
# The project() command manages VERSION variables.
cmake_policy(SET CMP0048 NEW)
# JSON-C library is C only project.
if (CMAKE_VERSION VERSION_LESS 3.0)
project(json-c)
set(PROJECT_VERSION_MAJOR "0")
set(PROJECT_VERSION_MINOR "15")
set(PROJECT_VERSION_PATCH "99")
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
else()
project(json-c LANGUAGES C VERSION 0.15.99)
endif()
# PROJECT_VERSION{,_MAJOR,_MINOR,_PATCH} set by project():
project(json-c LANGUAGES C VERSION 0.16.99)
# If we've got 3.0 then it's good, let's provide support. Otherwise, leave it be.
if(POLICY CMP0038)
# Policy CMP0038 introduced was in CMake 3.0
cmake_policy(SET CMP0038 NEW)
endif()
# Targets may not link directly to themselves.
cmake_policy(SET CMP0038 NEW)
if(POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
# MACOSX_RPATH is enabled by default.
# We set it explicitly to avoid the warning
cmake_policy(SET CMP0042 NEW)
# Only interpret if() arguments as variables or keywords when unquoted.
cmake_policy(SET CMP0054 NEW)
# set default build type if not specified by user
if(NOT CMAKE_BUILD_TYPE)
@@ -36,22 +31,13 @@ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
# Include file check macros honor CMAKE_REQUIRED_LIBRARIES
# i.e. the check_include_file() calls will include -lm when checking.
# New in version 3.12.
if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW)
endif()
include(CTest)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
(NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
)
add_subdirectory(tests)
endif()
if (NOT MSVC) # cmd line apps don't built on Windows currently.
add_subdirectory(apps)
endif()
# Set some packaging variables.
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
@@ -183,10 +169,12 @@ check_symbol_exists(arc4random "stdlib.h" HAVE_ARC4RANDOM)
if (NOT HAVE_ARC4RANDOM AND DISABLE_EXTRA_LIBS STREQUAL "OFF")
check_include_file(bsd/stdlib.h HAVE_BSD_STDLIB_H)
if (HAVE_BSD_STDLIB_H)
list(APPEND CMAKE_REQUIRED_LIBRARIES "-lbsd")
link_libraries(bsd)
list(APPEND CMAKE_REQUIRED_LIBRARIES "bsd")
unset(HAVE_ARC4RANDOM CACHE)
check_symbol_exists(arc4random "bsd/stdlib.h" HAVE_ARC4RANDOM)
if (NOT HAVE_ARC4RANDOM)
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "bsd")
endif()
endif()
endif()
@@ -200,6 +188,18 @@ if (HAVE_LOCALE_H)
check_symbol_exists(setlocale "locale.h" HAVE_SETLOCALE)
check_symbol_exists(uselocale "locale.h" HAVE_USELOCALE)
endif()
# uClibc *intentionally* crashes in duplocale(), at least as of:
# https://github.com/ffainelli/uClibc/blob/266bdc1/libc/misc/locale/locale.c#L1322
# So, if it looks like we're compiling for a system like that just disable
# locale handling entirely.
exec_program(${CMAKE_C_COMPILER} ARGS -dumpmachine OUTPUT_VARIABLE CMAKE_GNU_C_MACHINE)
if (CMAKE_GNU_C_MACHINE MATCHES "uclibc")
message(STATUS "Detected uClibc compiler, disabling locale handling")
set(HAVE_SETLOCALE 0)
set(HAVE_USELOCALE 0)
endif()
if (HAVE_STRINGS_H)
check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
@@ -398,7 +398,6 @@ set(JSON_C_HEADERS
${PROJECT_SOURCE_DIR}/json_object_private.h
${PROJECT_SOURCE_DIR}/random_seed.h
${PROJECT_SOURCE_DIR}/strerror_override.h
${PROJECT_SOURCE_DIR}/strerror_override_private.h
${PROJECT_SOURCE_DIR}/math_compat.h
${PROJECT_SOURCE_DIR}/snprintf_compat.h
${PROJECT_SOURCE_DIR}/strdup_compat.h
@@ -454,7 +453,7 @@ add_library(${PROJECT_NAME}
${JSON_C_HEADERS}
)
set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION 5.1.0
VERSION 5.2.0
SOVERSION 5)
list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
# If json-c is used as subroject it set to target correct interface -I flags and allow
@@ -465,6 +464,8 @@ target_include_directories(${PROJECT_NAME}
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
)
target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
# Allow to build static and shared libraries at the same time
if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
set(STATIC_LIB ${PROJECT_NAME}-static)
@@ -478,6 +479,8 @@ if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
)
target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
# rename the static library
if (NOT MSVC)
set_target_properties(${STATIC_LIB} PROPERTIES
@@ -531,6 +534,23 @@ if (UNIX OR MINGW OR CYGWIN)
SET(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
SET(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
SET(VERSION ${PROJECT_VERSION})
# Linking against the static json-c requires
# dependent packages to include additional libs:
SET(LIBS_LIST ${CMAKE_REQUIRED_LIBRARIES})
# Note: We would need cmake >= 3.12 in order to use list(TRANSFORM ...)
function(list_transform_prepend var prefix)
set(temp "")
foreach(f ${${var}})
list(APPEND temp "${prefix}${f}")
endforeach()
set(${var} "${temp}" PARENT_SCOPE)
endfunction()
list_transform_prepend(LIBS_LIST "-l")
string(REPLACE ";" " " LIBS "${LIBS_LIST}")
configure_file(json-c.pc.in json-c.pc @ONLY)
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
install(FILES ${PROJECT_BINARY_DIR}/json-c.pc DESTINATION "${INSTALL_PKGCONFIG_DIR}")
@@ -538,3 +558,16 @@ endif ()
install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/json-c)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
(NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
)
add_subdirectory(tests)
endif()
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# skip apps when we're included in someone else's build
if (NOT MSVC) # cmd line apps don't built on Windows currently.
add_subdirectory(apps)
endif()
endif()

View File

@@ -1,4 +1,26 @@
0.17 (future release)
========================================
Deprecated and removed features:
--------------------------------
* ...
New features
------------
* ...
Significant changes and bug fixes
---------------------------------
* When serializing with JSON_C_TO_STRING_PRETTY set, keep the opening and
closing curly or square braces on same line for empty objects or arrays.
* Disable locale handling when targeting a uClibc system due to problems
with its duplocale() function.
* When parsing with JSON_TOKENER_STRICT set, integer overflow/underflow
now result in a json_tokener_error_parse_number. Without that flag
values are capped at INT64_MIN/UINT64_MAX.
0.16 (up to commit 66dcdf5, 2022-04-13)
========================================

View File

@@ -4,14 +4,16 @@
========
1. [Overview and Build Status](#overview)
2. [Building on Unix](#buildunix)
2. [Getting Help](#gettinghelp)
3. [Building on Unix](#buildunix)
* [Prerequisites](#installprereq)
* [Build commands](#buildcmds)
3. [CMake options](#CMake)
4. [Testing](#testing)
5. [Building with `vcpkg`](#buildvcpkg)
6. [Linking to libjson-c](#linking)
7. [Using json-c](#using)
4. [CMake options](#CMake)
5. [Testing](#testing)
6. [Building with `vcpkg`](#buildvcpkg)
7. [Building for Android](#android)
7. [Linking to libjson-c](#linking)
8. [Using json-c](#using)
JSON-C - A JSON implementation in C <a name="overview"></a>
-----------------------------------
@@ -19,7 +21,7 @@ JSON-C - A JSON implementation in C <a name="overview"></a>
JSON-C implements a reference counting object model that allows you to easily
construct JSON objects in C, output them as JSON formatted strings and parse
JSON formatted strings back into the C representation of JSON objects.
It aims to conform to [RFC 7159](https://tools.ietf.org/html/rfc7159).
It aims to conform to [RFC 8259](https://www.rfc-editor.org/rfc/rfc8259).
Skip down to [Using json-c](#using)
or check out the [API docs](https://json-c.github.io/json-c/),
@@ -27,12 +29,23 @@ if you already have json-c installed and ready to use.
Home page for json-c: https://github.com/json-c/json-c/wiki
Build Status
* [AppVeyor Build](https://ci.appveyor.com/project/hawicz/json-c) ![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/json-c/json-c?branch=master&svg=true)
* [Travis Build](https://travis-ci.org/json-c/json-c) ![Travis Build Status](https://travis-ci.org/json-c/json-c.svg?branch=master)
Getting Help <a name="gettinghelp"></a>
------------
If you have questions about using json-c, please start a thread on
our forums at: https://groups.google.com/forum/#!forum/json-c
If you believe you've discovered a bug, report it at
(https://github.com/json-c/json-c/issues). Please be sure to include
the version of json-c you're using, the OS you're running on, and any
other relevant details. Fully reproducible test cases and/or patches
to fix problems are greatly appreciated.
Fixes for bugs, or small new features can be directly submitted as a
[pull request](https://github.com/json-c/json-c/pulls). For major new
features or large changes of any kind, please first start a discussion
on the [forums](https://groups.google.com/forum/#!forum/json-c).
Test Status
* [Coveralls](https://coveralls.io/github/json-c/json-c?branch=master) [![Coverage Status](https://coveralls.io/repos/github/json-c/json-c/badge.svg?branch=master)](https://coveralls.io/github/json-c/json-c?branch=master)
Building on Unix with `git`, `gcc` and `cmake` <a name="buildunix"></a>
--------------------------------------------------
@@ -40,6 +53,13 @@ Building on Unix with `git`, `gcc` and `cmake` <a name="buildunix"></a>
If you already have json-c installed, see [Linking to `libjson-c`](#linking)
for how to build and link your program against it.
Build Status
* [AppVeyor Build](https://ci.appveyor.com/project/hawicz/json-c) ![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/json-c/json-c?branch=master&svg=true)
* [Travis Build](https://app.travis-ci.com/github/json-c/json-c) ![Travis Build Status](https://api.travis-ci.com/json-c/json-c.svg?branch=master)
Test Status
* [Coveralls](https://coveralls.io/github/json-c/json-c?branch=master) [![Coverage Status](https://coveralls.io/repos/github/json-c/json-c/badge.svg?branch=master)](https://coveralls.io/github/json-c/json-c?branch=master)
### Prerequisites: <a name="installprereq"></a>
- `gcc`, `clang`, or another C compiler
@@ -81,7 +101,7 @@ Then:
$ make
$ make test
$ make USE_VALGRIND=0 test # optionally skip using valgrind
$ make install
$ sudo make install # it could be necessary to execute make install
```
@@ -112,6 +132,8 @@ DISABLE_STATIC_FPIC | Bool | The default builds position independent
DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions.
DISABLE_THREAD_LOCAL_STORAGE | Bool | Disable use of Thread-Local Storage (HAVE___THREAD).
DISABLE_WERROR | Bool | Disable use of -Werror.
DISABLE_EXTRA_LIBS | Bool | Disable use of extra libraries, libbsd
DISABLE_JSON_POINTER | Bool | Omit json_pointer support from the build.
ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed.
ENABLE_THREADING | Bool | Enable partial threading support.
OVERRIDE_GET_RANDOM_SEED | String | A block of code to use instead of the default implementation of json_c_get_random_seed(), e.g. on embedded platforms where not even the fallback to time() works. Must be a single line.
@@ -215,6 +237,29 @@ You can download and install JSON-C using the [vcpkg](https://github.com/Microso
The JSON-C port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
Building for Android <a name="android">
----------------------
Building on Android is now particularly well supported, but there
have been some reports of success using
https://developer.android.com/ndk/guides/cmake
```
mkdir json-c-build
cd json-c-build/
export NDK_HOME=~/Library/Android/sdk/ndk/22.1.7171670/
cmake \
--toolchain=$NDK_HOME/build/cmake/android.toolchain.cmake \
-DANDROID_STL=none \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-29 \
-DANDROID_LD=lld \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DCMAKE_INSTALL_PREFIX=<install prefix> \
-DENABLE_THREADING=true \
..
make install
```
Linking to `libjson-c` <a name="linking">
----------------------

View File

@@ -94,7 +94,11 @@ Create the release tarballs:
echo .git > excludes
tar -czf json-c-${release}.tar.gz -X excludes json-c-${release}
echo 'doc/*' >> excludes
echo 'doc/*.cmake' >> excludes
echo 'doc/CMakeFiles' >> excludes
echo 'doc/Makefile' >> excludes
echo 'doc/Doxyfile' >> excludes
echo 'doc/html' >> excludes
tar -czf json-c-${release}-nodoc.tar.gz -X excludes json-c-${release}
------------
@@ -113,7 +117,8 @@ Go to Amazon S3 service at:
https://console.aws.amazon.com/s3/
Upload the two tarballs in the json-c_releases/releases folder.
When uploading, use "Standard" storage class, and make the uploaded files publicly accessible.
* Expand "Permissions", pick "Grant public-read access"
* Expand "Properties", ensure "Standard" storage class is picked.
Logout of Amazon S3, and verify that the files are visible.
https://s3.amazonaws.com/json-c_releases/releases/index.html

View File

@@ -22,16 +22,30 @@
#include <sys/time.h>
#endif
static int formatted_output = 0;
#ifndef JSON_NORETURN
#if defined(_MSC_VER)
#define JSON_NORETURN __declspec(noreturn)
#elif defined(__OS400__)
#define JSON_NORETURN
#else
/* 'cold' attribute is for optimization, telling the computer this code
* path is unlikely.
*/
#define JSON_NORETURN __attribute__((noreturn, cold))
#endif
#endif
static int formatted_output = JSON_C_TO_STRING_SPACED;
static int show_output = 1;
static int strict_mode = 0;
static int color = 0;
static const char *fname = NULL;
#ifndef HAVE_JSON_TOKENER_GET_PARSE_END
#define json_tokener_get_parse_end(tok) ((tok)->char_offset)
#endif
static void usage(const char *argv0, int exitval, const char *errmsg);
JSON_NORETURN static void usage(const char *argv0, int exitval, const char *errmsg);
static void showmem(void);
static int parseit(int fd, int (*callback)(struct json_object *));
static int showobj(struct json_object *new_obj);
@@ -42,7 +56,7 @@ static void showmem(void)
struct rusage rusage;
memset(&rusage, 0, sizeof(rusage));
getrusage(RUSAGE_SELF, &rusage);
printf("maxrss: %ld KB\n", rusage.ru_maxrss);
fprintf(stderr, "maxrss: %ld KB\n", rusage.ru_maxrss);
#endif
}
@@ -50,7 +64,7 @@ static int parseit(int fd, int (*callback)(struct json_object *))
{
struct json_object *obj;
char buf[32768];
int ret;
ssize_t ret;
int depth = JSON_TOKENER_DEFAULT_DEPTH;
json_tokener *tok;
@@ -73,20 +87,21 @@ static int parseit(int fd, int (*callback)(struct json_object *))
size_t total_read = 0;
while ((ret = read(fd, buf, sizeof(buf))) > 0)
{
total_read += ret;
int start_pos = 0;
while (start_pos != ret)
size_t retu = (size_t)ret; // We know it's positive
total_read += retu;
size_t start_pos = 0;
while (start_pos != retu)
{
obj = json_tokener_parse_ex(tok, &buf[start_pos], ret - start_pos);
obj = json_tokener_parse_ex(tok, &buf[start_pos], retu - start_pos);
enum json_tokener_error jerr = json_tokener_get_error(tok);
int parse_end = json_tokener_get_parse_end(tok);
size_t parse_end = json_tokener_get_parse_end(tok);
if (obj == NULL && jerr != json_tokener_continue)
{
char *aterr = (start_pos + parse_end < sizeof(buf)) ?
const char *aterr = (start_pos + parse_end < (int)sizeof(buf)) ?
&buf[start_pos + parse_end] : "";
fflush(stdout);
int fail_offset = total_read - ret + start_pos + parse_end;
fprintf(stderr, "Failed at offset %d: %s %c\n", fail_offset,
size_t fail_offset = total_read - retu + start_pos + parse_end;
fprintf(stderr, "Failed at offset %lu: %s %c\n", (unsigned long)fail_offset,
json_tokener_error_desc(jerr), aterr[0]);
json_tokener_free(tok);
return 1;
@@ -102,7 +117,7 @@ static int parseit(int fd, int (*callback)(struct json_object *))
}
}
start_pos += json_tokener_get_parse_end(tok);
assert(start_pos <= ret);
assert(start_pos <= retu);
}
}
if (ret < 0)
@@ -122,15 +137,12 @@ static int showobj(struct json_object *new_obj)
return 1;
}
printf("Successfully parsed object from %s\n", fname);
fprintf(stderr, "Successfully parsed object from %s\n", fname);
if (show_output)
{
const char *output;
if (formatted_output)
output = json_object_to_json_string(new_obj);
else
output = json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_PRETTY);
output = json_object_to_json_string_ext(new_obj, formatted_output | color);
printf("%s\n", output);
}
@@ -145,11 +157,14 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
fp = stderr;
if (errmsg != NULL)
fprintf(fp, "ERROR: %s\n\n", errmsg);
fprintf(fp, "Usage: %s [-f] [-n] [-s]\n", argv0);
fprintf(fp, " -f - Format the output with JSON_C_TO_STRING_PRETTY\n");
fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s]\n", argv0);
fprintf(fp, " -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
fprintf(fp, " -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
fprintf(fp, " -n - No output\n");
fprintf(fp, " -c - color\n");
fprintf(fp, " -s - Parse in strict mode, flags:\n");
fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
fprintf(fp, " Diagnostic information will be emitted to stderr\n");
fprintf(fp, "\nWARNING WARNING WARNING\n");
fprintf(fp, "This is a prototype, it may change or be removed at any time!\n");
@@ -158,16 +173,17 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
int main(int argc, char **argv)
{
json_object *new_obj;
int opt;
while ((opt = getopt(argc, argv, "fhns")) != -1)
while ((opt = getopt(argc, argv, "fF:hnsc")) != -1)
{
switch (opt)
{
case 'f': formatted_output = 1; break;
case 'f': formatted_output = JSON_C_TO_STRING_PRETTY; break;
case 'F': formatted_output = atoi(optarg); break;
case 'n': show_output = 0; break;
case 's': strict_mode = 1; break;
case 'c': color = JSON_C_TO_STRING_COLOR; break;
case 'h': usage(argv[0], 0, NULL);
default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
}

View File

@@ -65,9 +65,15 @@ while [ $# -gt 0 ] ; do
--enable-shared)
FLAGS+=(-DBUILD_SHARED_LIBS=ON)
;;
--disable-shared)
FLAGS+=(-DBUILD_SHARED_LIBS=OFF)
;;
--enable-static)
FLAGS+=(-DBUILD_STATIC_LIBS=ON)
;;
--disable-static)
FLAGS+=(-DBUILD_STATIC_LIBS=OFF)
;;
--disable-Bsymbolic)
FLAGS+=(-DDISABLE_BSYMBOLIC=ON)
;;

View File

@@ -1103,7 +1103,7 @@ HTML_STYLESHEET =
# cascading style sheets that are included after the standard style sheets
# created by doxygen. Using this option one can overrule certain style aspects.
# This is preferred over using HTML_STYLESHEET since it does not replace the
# standard style sheet and is therefor more robust against future updates.
# standard style sheet and is therefore more robust against future updates.
# Doxygen will copy the style sheet files to the output directory.
# Note: The order of the extra stylesheet files is of importance (e.g. the last
# stylesheet in the list overrules the setting of the previous ones in the

View File

@@ -41,6 +41,8 @@ JSONC_PRIVATE {
printbuf_new;
printbuf_reset;
sprintbuf;
# Used by tests:
_json_c_strerror;
};
JSONC_0.14 {
@@ -168,3 +170,8 @@ JSONC_0.16 {
# global:
# ...new symbols here...
} JSONC_0.15;
JSONC_0.17 {
# global:
# ...new symbols here...
} JSONC_0.16;

View File

@@ -17,11 +17,11 @@ extern "C" {
#endif
#define JSON_C_MAJOR_VERSION 0
#define JSON_C_MINOR_VERSION 15
#define JSON_C_MINOR_VERSION 16
#define JSON_C_MICRO_VERSION 99
#define JSON_C_VERSION_NUM \
((JSON_C_MAJOR_VERSION << 16) | (JSON_C_MINOR_VERSION << 8) | JSON_C_MICRO_VERSION)
#define JSON_C_VERSION "0.15.99"
#define JSON_C_VERSION "0.16.99"
#ifndef JSON_EXPORT
#if defined(_MSC_VER) && defined(JSON_C_DLL)

View File

@@ -21,4 +21,9 @@
#endif
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
#endif

View File

@@ -64,6 +64,12 @@ static void json_object_generic_delete(struct json_object *jso);
#define inline
#endif
/* define colors */
#define ANSI_COLOR_RESET "\033[0m"
#define ANSI_COLOR_FG_GREEN "\033[0;32m"
#define ANSI_COLOR_FG_BLUE "\033[0;34m"
#define ANSI_COLOR_FG_MAGENTA "\033[0;35m"
/*
* Helper functions to more safely cast to a particular type of json_object
*/
@@ -460,35 +466,45 @@ static int json_object_object_to_json_string(struct json_object *jso, struct pri
struct json_object_iter iter;
printbuf_strappend(pb, "{" /*}*/);
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
json_object_object_foreachC(jso, iter)
{
if (had_children)
{
printbuf_strappend(pb, ",");
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
}
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
had_children = 1;
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
printbuf_strappend(pb, " ");
indent(pb, level + 1, flags);
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_BLUE);
printbuf_strappend(pb, "\"");
json_escape_str(pb, iter.key, strlen(iter.key), flags);
printbuf_strappend(pb, "\"");
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_RESET);
if (flags & JSON_C_TO_STRING_SPACED)
printbuf_strappend(pb, "\": ");
printbuf_strappend(pb, ": ");
else
printbuf_strappend(pb, "\":");
if (iter.val == NULL)
printbuf_strappend(pb, ":");
if (iter.val == NULL) {
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
printbuf_strappend(pb, "null");
else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_RESET);
} else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
return -1;
}
if (flags & JSON_C_TO_STRING_PRETTY)
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
{
if (had_children)
printbuf_strappend(pb, "\n");
printbuf_strappend(pb, "\n");
indent(pb, level, flags);
}
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
@@ -629,9 +645,18 @@ void json_object_object_del(struct json_object *jso, const char *key)
static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
int level, int flags)
{
int ret;
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
if (JC_BOOL(jso)->c_boolean)
return printbuf_strappend(pb, "true");
return printbuf_strappend(pb, "false");
ret = printbuf_strappend(pb, "true");
else
ret = printbuf_strappend(pb, "false");
if (ret > -1 && flags & JSON_C_TO_STRING_COLOR)
return printbuf_strappend(pb, ANSI_COLOR_RESET);
return ret;
}
struct json_object *json_object_new_boolean(json_bool b)
@@ -1220,9 +1245,13 @@ static int json_object_string_to_json_string(struct json_object *jso, struct pri
int level, int flags)
{
ssize_t len = JC_STRING(jso)->len;
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_GREEN);
printbuf_strappend(pb, "\"");
json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags);
printbuf_strappend(pb, "\"");
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_RESET);
return 0;
}
@@ -1323,11 +1352,18 @@ static int _json_object_set_string_len(json_object *jso, const char *s, size_t l
// length as int, cap length at INT_MAX.
return 0;
dstbuf = get_string_component_mutable(jso);
curlen = JC_STRING(jso)->len;
if (curlen < 0)
curlen = -curlen;
if (curlen < 0) {
if (len == 0) {
free(JC_STRING(jso)->c_string.pdata);
JC_STRING(jso)->len = curlen = 0;
} else {
curlen = -curlen;
}
}
newlen = len;
dstbuf = get_string_component_mutable(jso);
if ((ssize_t)len > curlen)
{
@@ -1374,31 +1410,34 @@ static int json_object_array_to_json_string(struct json_object *jso, struct prin
size_t ii;
printbuf_strappend(pb, "[");
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
for (ii = 0; ii < json_object_array_length(jso); ii++)
{
struct json_object *val;
if (had_children)
{
printbuf_strappend(pb, ",");
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
}
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
had_children = 1;
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
printbuf_strappend(pb, " ");
indent(pb, level + 1, flags);
val = json_object_array_get_idx(jso, ii);
if (val == NULL)
if (val == NULL) {
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_FG_MAGENTA);
printbuf_strappend(pb, "null");
else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
if (flags & JSON_C_TO_STRING_COLOR)
printbuf_strappend(pb, ANSI_COLOR_RESET);
} else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
return -1;
}
if (flags & JSON_C_TO_STRING_PRETTY)
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
{
if (had_children)
printbuf_strappend(pb, "\n");
printbuf_strappend(pb, "\n");
indent(pb, level, flags);
}

View File

@@ -74,6 +74,15 @@ extern "C" {
*/
#define JSON_C_TO_STRING_NOSLASHESCAPE (1 << 4)
/**
* A flag for the json_object_to_json_string_ext() and
* json_object_to_file_ext() functions which causes
* the output to be formatted.
*
* Use color for printing json.
*/
#define JSON_C_TO_STRING_COLOR (1 << 5)
/**
* A flag for the json_object_object_add_ex function which
* causes the value to be added without a check if it already exists.

View File

@@ -29,8 +29,8 @@
static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
{
int slen = strlen(s);
int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
size_t slen = strlen(s);
size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
char *p = s;
while ((p = strstr(p, occur)))
{
@@ -41,9 +41,10 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur,
}
}
static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx)
static int is_valid_index(struct json_object *jo, const char *path, size_t *idx)
{
int i, len = strlen(path);
size_t i, len = strlen(path);
long int idx_val = -1;
/* this code-path optimizes a bit, for when we reference the 0-9 index range
* in a JSON array and because leading zeros not allowed
*/
@@ -73,12 +74,14 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx
}
}
*idx = strtol(path, NULL, 10);
if (*idx < 0)
idx_val = strtol(path, NULL, 10);
if (idx_val < 0)
{
errno = EINVAL;
return 0;
}
*idx = idx_val;
check_oob:
len = json_object_array_length(jo);
if (*idx >= len)
@@ -95,7 +98,7 @@ static int json_pointer_get_single_path(struct json_object *obj, char *path,
{
if (json_object_is_type(obj, json_type_array))
{
int32_t idx;
size_t idx;
if (!is_valid_index(obj, path, &idx))
return -1;
obj = json_object_array_get_idx(obj, idx);
@@ -128,7 +131,7 @@ static int json_pointer_set_single_path(struct json_object *parent, const char *
{
if (json_object_is_type(parent, json_type_array))
{
int32_t idx;
size_t idx;
/* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
if (path[0] == '-' && path[1] == '\0')
return json_object_array_add(parent, value);

View File

@@ -17,6 +17,7 @@
#include "math_compat.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stddef.h>
@@ -103,6 +104,7 @@ static const char *json_tokener_errors[] = {
"success",
"continue",
"nesting too deep",
"out of memory",
"unexpected end of data",
"unexpected character",
"null expected",
@@ -284,11 +286,24 @@ struct json_object *json_tokener_parse_verbose(const char *str, enum json_tokene
/* ADVANCE_CHAR() macro:
* Increments str & tok->char_offset.
* For convenience of existing conditionals, returns the old value of c (0 on eof)
* For convenience of existing conditionals, returns the old value of c (0 on eof).
* Implicit inputs: c var
*/
#define ADVANCE_CHAR(str, tok) (++(str), ((tok)->char_offset)++, c)
/* printbuf_memappend_checked(p, s, l) macro:
* Add string s of length l to printbuffer p.
* If operation fails abort parse operation with memory error.
*/
#define printbuf_memappend_checked(p, s, l) \
do { \
if (printbuf_memappend((p), (s), (l)) < 0) \
{ \
tok->err = json_tokener_error_memory; \
goto out; \
} \
} while (0)
/* End optimization macro defs */
struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
@@ -336,7 +351,11 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
char *tmplocale;
tmplocale = setlocale(LC_NUMERIC, NULL);
if (tmplocale)
{
oldlocale = strdup(tmplocale);
if (oldlocale == NULL)
return NULL;
}
setlocale(LC_NUMERIC, "C");
}
#endif
@@ -358,7 +377,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
if (c == '/' && !(tok->flags & JSON_TOKENER_STRICT))
{
printbuf_reset(tok->pb);
printbuf_memappend_fast(tok->pb, &c, 1);
printbuf_memappend_checked(tok->pb, &c, 1);
state = json_tokener_state_comment_start;
}
else
@@ -376,14 +395,20 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
saved_state = json_tokener_state_object_field_start;
current = json_object_new_object();
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
break;
case '[':
state = json_tokener_state_eatws;
saved_state = json_tokener_state_array;
current = json_object_new_array();
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
break;
case 'I':
case 'i':
@@ -486,7 +511,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
}
current = json_object_new_double(is_negative ? -INFINITY : INFINITY);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws;
goto redo_char;
@@ -496,7 +524,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
int size;
int size_nan;
printbuf_memappend_fast(tok->pb, &c, 1);
printbuf_memappend_checked(tok->pb, &c, 1);
size = json_min(tok->st_pos + 1, json_null_str_len);
size_nan = json_min(tok->st_pos + 1, json_nan_str_len);
if ((!(tok->flags & JSON_TOKENER_STRICT) &&
@@ -519,7 +547,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
current = json_object_new_double(NAN);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws;
goto redo_char;
@@ -548,7 +579,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
tok->err = json_tokener_error_parse_comment;
goto out;
}
printbuf_memappend_fast(tok->pb, &c, 1);
printbuf_memappend_checked(tok->pb, &c, 1);
break;
case json_tokener_state_comment:
@@ -559,12 +590,12 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
goto out;
}
}
printbuf_memappend_fast(tok->pb, case_start, 1 + str - case_start);
printbuf_memappend_checked(tok->pb, case_start, 1 + str - case_start);
state = json_tokener_state_comment_end;
}
break;
@@ -577,19 +608,19 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
goto out;
}
}
printbuf_memappend_fast(tok->pb, case_start, str - case_start);
printbuf_memappend_checked(tok->pb, case_start, str - case_start);
MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
state = json_tokener_state_eatws;
}
break;
case json_tokener_state_comment_end:
printbuf_memappend_fast(tok->pb, &c, 1);
printbuf_memappend_checked(tok->pb, &c, 1);
if (c == '/')
{
MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
@@ -609,28 +640,31 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
if (c == tok->quote_char)
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
current =
json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws;
break;
}
else if (c == '\\')
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
saved_state = json_tokener_state_string;
state = json_tokener_state_string_escape;
break;
}
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
goto out;
}
}
@@ -643,7 +677,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
case '"':
case '\\':
case '/':
printbuf_memappend_fast(tok->pb, &c, 1);
printbuf_memappend_checked(tok->pb, &c, 1);
state = saved_state;
break;
case 'b':
@@ -652,15 +686,15 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
case 't':
case 'f':
if (c == 'b')
printbuf_memappend_fast(tok->pb, "\b", 1);
printbuf_memappend_checked(tok->pb, "\b", 1);
else if (c == 'n')
printbuf_memappend_fast(tok->pb, "\n", 1);
printbuf_memappend_checked(tok->pb, "\n", 1);
else if (c == 'r')
printbuf_memappend_fast(tok->pb, "\r", 1);
printbuf_memappend_checked(tok->pb, "\r", 1);
else if (c == 't')
printbuf_memappend_fast(tok->pb, "\t", 1);
printbuf_memappend_checked(tok->pb, "\t", 1);
else if (c == 'f')
printbuf_memappend_fast(tok->pb, "\f", 1);
printbuf_memappend_checked(tok->pb, "\f", 1);
state = saved_state;
break;
case 'u':
@@ -720,8 +754,8 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
/* High surrogate was not followed by a low surrogate
* Replace the high and process the rest normally
*/
printbuf_memappend_fast(tok->pb,
(char *)utf8_replacement_char, 3);
printbuf_memappend_checked(tok->pb,
(char *)utf8_replacement_char, 3);
}
tok->high_surrogate = 0;
}
@@ -730,14 +764,14 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
unsigned char unescaped_utf[1];
unescaped_utf[0] = tok->ucs_char;
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 1);
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 1);
}
else if (tok->ucs_char < 0x800)
{
unsigned char unescaped_utf[2];
unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6);
unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f);
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 2);
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 2);
}
else if (IS_HIGH_SURROGATE(tok->ucs_char))
{
@@ -763,7 +797,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
else if (IS_LOW_SURROGATE(tok->ucs_char))
{
/* Got a low surrogate not preceded by a high */
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
}
else if (tok->ucs_char < 0x10000)
{
@@ -771,7 +805,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12);
unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f);
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 3);
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 3);
}
else if (tok->ucs_char < 0x110000)
{
@@ -780,12 +814,12 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f);
unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f);
printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 4);
printbuf_memappend_checked(tok->pb, (char *)unescaped_utf, 4);
}
else
{
/* Don't know what we got--insert the replacement char */
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
}
state = saved_state; // i.e. _state_string or _state_object_field
}
@@ -800,7 +834,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
* it. Put a replacement char in for the high surrogate
* and pop back up to _state_string or _state_object_field.
*/
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
tok->high_surrogate = 0;
tok->ucs_char = 0;
tok->st_pos = 0;
@@ -819,7 +853,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
* Put a replacement char in for the high surrogate
* and handle the escape sequence normally.
*/
printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
printbuf_memappend_checked(tok->pb, (char *)utf8_replacement_char, 3);
tok->high_surrogate = 0;
tok->ucs_char = 0;
tok->st_pos = 0;
@@ -834,7 +868,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
case json_tokener_state_boolean:
{
int size1, size2;
printbuf_memappend_fast(tok->pb, &c, 1);
printbuf_memappend_checked(tok->pb, &c, 1);
size1 = json_min(tok->st_pos + 1, json_true_str_len);
size2 = json_min(tok->st_pos + 1, json_false_str_len);
if ((!(tok->flags & JSON_TOKENER_STRICT) &&
@@ -845,7 +879,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
current = json_object_new_boolean(1);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws;
goto redo_char;
@@ -859,7 +896,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
current = json_object_new_boolean(0);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws;
goto redo_char;
@@ -939,7 +979,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
{
printbuf_memappend_fast(tok->pb, case_start, case_len);
printbuf_memappend_checked(tok->pb, case_start, case_len);
goto out;
}
}
@@ -948,7 +988,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
it might have been intended to be, and return a potentially
more understandable error right away.
However, if we're at the top-level, use the number as-is
because c can be part of a new object to parse on the
because c can be part of a new object to parse on the
next call to json_tokener_parse().
*/
if (tok->depth > 0 && c != ',' && c != ']' && c != '}' && c != '/' &&
@@ -958,7 +998,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
goto out;
}
if (case_len > 0)
printbuf_memappend_fast(tok->pb, case_start, case_len);
printbuf_memappend_checked(tok->pb, case_start, case_len);
// Check for -Infinity
if (tok->pb->buf[0] == '-' && case_len <= 1 && (c == 'i' || c == 'I'))
@@ -991,13 +1031,26 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
if (!tok->is_double && tok->pb->buf[0] == '-' &&
json_parse_int64(tok->pb->buf, &num64) == 0)
{
if (errno == ERANGE && (tok->flags & JSON_TOKENER_STRICT))
{
tok->err = json_tokener_error_parse_number;
goto out;
}
current = json_object_new_int64(num64);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
}
else if (!tok->is_double && tok->pb->buf[0] != '-' &&
json_parse_uint64(tok->pb->buf, &numuint64) == 0)
{
if (errno == ERANGE && (tok->flags & JSON_TOKENER_STRICT))
{
tok->err = json_tokener_error_parse_number;
goto out;
}
if (numuint64 && tok->pb->buf[0] == '0' &&
(tok->flags & JSON_TOKENER_STRICT))
{
@@ -1009,13 +1062,19 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
num64 = (uint64_t)numuint64;
current = json_object_new_int64(num64);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
}
else
{
current = json_object_new_uint64(numuint64);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
}
}
else if (tok->is_double &&
@@ -1024,7 +1083,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
current = json_object_new_double_s(numd, tok->pb->buf);
if (current == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
}
else
{
@@ -1069,7 +1131,10 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
case json_tokener_state_array_add:
if (json_object_array_add(current, obj) != 0)
{
tok->err = json_tokener_error_memory;
goto out;
}
saved_state = json_tokener_state_array_sep;
state = json_tokener_state_eatws;
goto redo_char;
@@ -1129,25 +1194,30 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
{
if (c == tok->quote_char)
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
obj_field_name = strdup(tok->pb->buf);
if (obj_field_name == NULL)
{
tok->err = json_tokener_error_memory;
goto out;
}
saved_state = json_tokener_state_object_field_end;
state = json_tokener_state_eatws;
break;
}
else if (c == '\\')
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
saved_state = json_tokener_state_object_field;
state = json_tokener_state_string_escape;
break;
}
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
{
printbuf_memappend_fast(tok->pb, case_start,
str - case_start);
printbuf_memappend_checked(tok->pb, case_start,
str - case_start);
goto out;
}
}

View File

@@ -28,6 +28,7 @@ enum json_tokener_error
json_tokener_success,
json_tokener_continue,
json_tokener_error_depth,
json_tokener_error_memory,
json_tokener_error_parse_eof,
json_tokener_error_parse_unexpected,
json_tokener_error_parse_null,

View File

@@ -60,7 +60,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
static char _last_err[256] = "";
const char *json_util_get_last_err()
const char *json_util_get_last_err(void)
{
if (_last_err[0] == '\0')
return NULL;
@@ -85,7 +85,7 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth)
struct printbuf *pb;
struct json_object *obj;
char buf[JSON_FILE_BUF_SIZE];
int ret;
ssize_t ret;
int depth = JSON_TOKENER_DEFAULT_DEPTH;
json_tokener *tok;
@@ -101,15 +101,25 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth)
if (!tok)
{
_json_c_set_last_err(
"json_object_from_fd_ex: unable to allocate json_tokener(depth=%d): %s\n", depth,
strerror(errno));
"json_object_from_fd_ex: unable to allocate json_tokener(depth=%d): %s\n",
depth, strerror(errno));
printbuf_free(pb);
return NULL;
}
while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
while ((ret = read(fd, buf, sizeof(buf))) > 0)
{
printbuf_memappend(pb, buf, ret);
if (printbuf_memappend(pb, buf, ret) < 0)
{
#if JSON_FILE_BUF_SIZE > INT_MAX
#error "Can't append more than INT_MAX bytes at a time"
#endif
_json_c_set_last_err(
"json_object_from_fd_ex: failed to printbuf_memappend after reading %d+%d bytes: %s", printbuf_length(pb), (int)ret, strerror(errno));
json_tokener_free(tok);
printbuf_free(pb);
return NULL;
}
}
if (ret < 0)
{
@@ -184,9 +194,9 @@ int json_object_to_fd(int fd, struct json_object *obj, int flags)
}
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
{
int ret;
ssize_t ret;
const char *json_str;
unsigned int wpos, wsize;
size_t wpos, wsize;
filename = filename ? filename : "(fd)";
@@ -195,8 +205,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
return -1;
}
/* CAW: probably unnecessary, but the most 64bit safe */
wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
wsize = strlen(json_str);
wpos = 0;
while (wpos < wsize)
{
@@ -208,7 +217,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
}
/* because of the above check for ret < 0, we can safely cast and add */
wpos += (unsigned int)ret;
wpos += (size_t)ret;
}
return 0;
@@ -238,7 +247,12 @@ int json_parse_int64(const char *buf, int64_t *retval)
val = strtoll(buf, &end, 10);
if (end != buf)
*retval = val;
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
if ((val == 0 && errno != 0) || (end == buf))
{
errno = EINVAL;
return 1;
}
return 0;
}
int json_parse_uint64(const char *buf, uint64_t *retval)
@@ -255,7 +269,12 @@ int json_parse_uint64(const char *buf, uint64_t *retval)
val = strtoull(buf, &end, 10);
if (end != buf)
*retval = val;
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
if ((val == 0 && errno != 0) || (end == buf))
{
errno = EINVAL;
return 1;
}
return 0;
}
#ifndef HAVE_REALLOC

View File

@@ -100,8 +100,17 @@ JSON_EXPORT int json_object_to_fd(int fd, struct json_object *obj, int flags);
*/
JSON_EXPORT const char *json_util_get_last_err(void);
/* these parsing helpers return zero on success */
/**
* A parsing helper for integer values. Returns 0 on success,
* with the parsed value assigned to *retval. Overflow/underflow
* are NOT considered errors, but errno will be set to ERANGE,
* just like the strtol/strtoll functions do.
*/
JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval);
/**
* A parsing help for integer values, providing one extra bit of
* magnitude beyond json_parse_int64().
*/
JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval);
/**
* @deprecated

View File

@@ -15,6 +15,7 @@
#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -56,6 +57,8 @@ struct printbuf *printbuf_new(void)
*
* If the current size is large enough, nothing is changed.
*
* If extension failed, errno is set to indicate the error.
*
* Note: this does not check the available space! The caller
* is responsible for performing those calculations.
*/
@@ -68,7 +71,10 @@ static int printbuf_extend(struct printbuf *p, int min_size)
return 0;
/* Prevent signed integer overflows with large buffers. */
if (min_size > INT_MAX - 8)
{
errno = EFBIG;
return -1;
}
if (p->size > INT_MAX / 2)
new_size = min_size + 8;
else {
@@ -77,7 +83,7 @@ static int printbuf_extend(struct printbuf *p, int min_size)
new_size = min_size + 8;
}
#ifdef PRINTBUF_DEBUG
MC_DEBUG("printbuf_memappend: realloc "
MC_DEBUG("printbuf_extend: realloc "
"bpos=%d min_size=%d old_size=%d new_size=%d\n",
p->bpos, min_size, p->size, new_size);
#endif /* PRINTBUF_DEBUG */
@@ -92,7 +98,10 @@ int printbuf_memappend(struct printbuf *p, const char *buf, int size)
{
/* Prevent signed integer overflows with large buffers. */
if (size < 0 || size > INT_MAX - p->bpos - 1)
{
errno = EFBIG;
return -1;
}
if (p->size <= p->bpos + size + 1)
{
if (printbuf_extend(p, p->bpos + size + 1) < 0)
@@ -112,7 +121,10 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
offset = pb->bpos;
/* Prevent signed integer overflows with large buffers. */
if (len < 0 || offset < -1 || len > INT_MAX - offset)
{
errno = EFBIG;
return -1;
}
size_needed = offset + len;
if (pb->size < size_needed)
{

View File

@@ -310,6 +310,7 @@ static int get_time_seed(void)
{
DEBUG_SEED("get_time_seed");
/* coverity[store_truncates_time_t] */
return (unsigned)time(NULL) * 433494437;
}
#endif

View File

@@ -35,7 +35,7 @@ static int json_c_snprintf(char *str, size_t size, const char *format, ...)
#define snprintf json_c_snprintf
#elif !defined(HAVE_SNPRINTF) /* !HAVE_SNPRINTF */
#error Need vsnprintf!
#error snprintf is required but was not found
#endif /* !HAVE_SNPRINTF && defined(WIN32) */
#endif /* __snprintf_compat_h */

View File

@@ -1,14 +0,0 @@
#ifndef __json_strerror_override_private_h__
#define __json_strerror_override_private_h__
/**
* @file
* @brief Do not use, json-c internal, may be changed or removed at any time.
*/
#include "json_types.h"
/* Used by tests to get consistent output */
JSON_EXPORT int _json_c_strerror_enable;
#endif

View File

@@ -24,6 +24,7 @@ set(ALL_TEST_NAMES
test_double_serializer
test_float
test_int_add
test_int_get
test_locale
test_null
test_parse
@@ -43,10 +44,6 @@ endif()
foreach(TESTNAME ${ALL_TEST_NAMES})
add_executable(${TESTNAME} ${TESTNAME}.c)
if(${TESTNAME} STREQUAL test_strerror OR ${TESTNAME} STREQUAL test_util_file)
# For output consistency, we need _json_c_strerror() in some tests:
target_sources(${TESTNAME} PRIVATE ../strerror_override.c)
endif()
add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test)
# XXX using the non-target_ versions of these doesn't work :(

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <limits.h>
#include <stddef.h>
@@ -58,7 +61,7 @@ static const char *to_json_string(json_object *obj, int flags)
#endif
json_object *make_array(void);
json_object *make_array()
json_object *make_array(void)
{
json_object *my_array;
@@ -74,7 +77,7 @@ json_object *make_array()
}
void test_array_del_idx(void);
void test_array_del_idx()
void test_array_del_idx(void)
{
int rc;
size_t ii;
@@ -140,7 +143,7 @@ void test_array_del_idx()
}
void test_array_list_expand_internal(void);
void test_array_list_expand_internal()
void test_array_list_expand_internal(void)
{
int rc;
size_t ii;
@@ -308,6 +311,11 @@ int main(int argc, char **argv)
{
printf("\t%s: %s\n", key, json_object_to_json_string(val));
}
json_object *empty_array = json_object_new_array();
json_object *empty_obj = json_object_new_object();
json_object_object_add(my_object, "empty_array", empty_array);
json_object_object_add(my_object, "empty_obj", empty_obj);
printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
json_object_put(my_array);

View File

@@ -75,4 +75,4 @@ my_object=
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }

View File

@@ -75,4 +75,4 @@ my_object=
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true}
my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true,"empty_array":[],"empty_obj":{}}

View File

@@ -97,5 +97,7 @@ my_object.to_string()={
"abc":12,
"foo":"bar",
"bool0":false,
"bool1":true
"bool1":true,
"empty_array":[],
"empty_obj":{}
}

View File

@@ -75,4 +75,4 @@ my_object=
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }

View File

@@ -97,5 +97,7 @@ my_object.to_string()={
"abc": 12,
"foo": "bar",
"bool0": false,
"bool1": true
"bool1": true,
"empty_array": [],
"empty_obj": {}
}

View File

@@ -97,5 +97,7 @@ my_object.to_string()={
"abc": 12,
"foo": "bar",
"bool0": false,
"bool1": true
"bool1": true,
"empty_array": [],
"empty_obj": {}
}

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

View File

@@ -2,6 +2,9 @@
* gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <assert.h>
#include <stdio.h>
@@ -28,7 +31,7 @@ void print_hex(const char *s)
}
static void test_lot_of_adds(void);
static void test_lot_of_adds()
static void test_lot_of_adds(void)
{
int ii;
char key[50];

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

View File

@@ -3,6 +3,9 @@
* Also checks the json_object_get_type and json_object_is_type functions.
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
@@ -94,7 +97,7 @@ static void getit(struct json_object *new_obj, const char *field)
printf("new_obj.%s json_object_get_double()=%f\n", field, json_object_get_double(o));
}
static void checktype_header()
static void checktype_header(void)
{
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n", json_type_to_name(json_type_null),
json_type_to_name(json_type_boolean), json_type_to_name(json_type_double),

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
@@ -19,7 +22,7 @@ int main(int argc, char **argv)
}
/* make sure only lowercase forms are parsed in strict mode */
static void test_case_parse()
static void test_case_parse(void)
{
struct json_tokener *tok;
json_object *new_obj;

View File

@@ -2,6 +2,9 @@
* Tests if json_object_equal behaves correct.
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <string.h>
@@ -9,7 +12,7 @@
#include "json_inttypes.h"
#include "json_object.h"
int main()
int main(int argc, char **argv)
{
/* integer tests */
struct json_object *int1 = json_object_new_int(0);

View File

@@ -22,7 +22,7 @@ static const char *json_str1 =
" \"number\": 16446744073709551615,"
" \"title\": \"S\","
" \"null_obj\": null, "
" \"exixt\": false,"
" \"exist\": false,"
" \"quantity\":20,"
" \"univalent\":19.8,"
" \"GlossList\": {"
@@ -136,7 +136,7 @@ int main(int argc, char **argv)
assert(0 == json_object_deep_copy(src2, &dst2, NULL));
assert(0 == json_object_deep_copy(src3, &dst3, NULL));
printf("PASSED - all json_object_deep_copy() returned succesful\n");
printf("PASSED - all json_object_deep_copy() returned successful\n");
assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
assert(errno == EINVAL);
@@ -151,7 +151,7 @@ int main(int argc, char **argv)
assert(1 == json_object_equal(src2, dst2));
assert(1 == json_object_equal(src3, dst3));
printf("PASSED - all json_object_equal() tests returned succesful\n");
printf("PASSED - all json_object_equal() tests returned successful\n");
assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));

View File

@@ -1,7 +1,7 @@
PASSED - loaded input data
PASSED - all json_object_deep_copy() returned succesful
PASSED - all json_object_deep_copy() returned successful
PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer
PASSED - all json_object_equal() tests returned succesful
PASSED - all json_object_equal() tests returned successful
PASSED - comparison of string output
PASSED - trying to overrwrite an object that has refcount > 1
Printing JSON objects for visual inspection
@@ -14,7 +14,7 @@ Printing JSON objects for visual inspection
"number":16446744073709551615,
"title":"S",
"null_obj":null,
"exixt":false,
"exist":false,
"quantity":20,
"univalent":19.8,
"GlossList":{

View File

@@ -2,6 +2,9 @@
* Tests if the format string for double serialization is handled correctly
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
@@ -11,7 +14,7 @@
/* Avoid compiler warnings about diving by constant zero */
double zero_dot_zero = 0.0;
int main()
int main(int argc, char **argv)
{
struct json_object *obj = json_object_new_double(0.5);
char udata[] = "test";

View File

@@ -1,5 +1,8 @@
/* Copyright (C) 2016 by Rainer Gerhards
* Released under ASL 2.0 */
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include "json_object.h"
#include "json_tokener.h"

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>

65
tests/test_int_get.c Normal file
View File

@@ -0,0 +1,65 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include "json.h"
#define I64_MAX_S "9223372036854775807"
#define I64_OVER "9223372036854775808"
#define I64_MIN_S "-9223372036854775808"
#define I64_UNDER "-9223372036854775809"
#define U64_MAX_S "18446744073709551615"
#define U64_OUT_S "18446744073709551616"
#define CHECK_GET(GET_F, J, EXPECTED) { struct json_object *jtmp = J; errno = 0; assert(GET_F(jtmp) == EXPECTED); json_object_put(jtmp); }
#define CHECK_GET_INT(J, EXPECTED) CHECK_GET(json_object_get_int, J, EXPECTED)
#define CHECK_GET_INT64(J, EXPECTED) CHECK_GET(json_object_get_int64, J, EXPECTED)
#define CHECK_GET_UINT64(J, EXPECTED) CHECK_GET(json_object_get_uint64, J, EXPECTED)
#define CHECK_BASE(J, EXPECTED) CHECK_GET_INT(J, EXPECTED); CHECK_GET_INT64(J, EXPECTED); CHECK_GET_UINT64(J, EXPECTED)
#define N_INT json_object_new_int
#define N_I64 json_object_new_int64
#define N_U64 json_object_new_uint64
#define N_STR json_object_new_string
int main(int argc, char **argv)
{
CHECK_BASE(N_INT(5), 5);
CHECK_BASE(N_INT(0), 0);
CHECK_BASE(N_STR("0"), 0);
CHECK_BASE(N_STR("00000"), 0);
CHECK_BASE(N_STR("000004568789"), 4568789);
CHECK_BASE(N_STR("0xFF"), 0 && errno == 0); // Hex-string values being parsed as 0 is the intended behavior
CHECK_BASE(N_STR("333this_seems_a_valid_string"), 333);
CHECK_BASE(N_STR("this_is_not_a_number"), 0 && errno == EINVAL);
CHECK_BASE(N_STR("B0"), 0 && errno == EINVAL);
printf("BASE CHECK PASSED\n");
CHECK_GET_INT(N_I64(INT32_MAX), INT32_MAX && errno == 0);
CHECK_GET_INT(N_I64(INT32_MIN), INT32_MIN && errno == 0);
CHECK_GET_INT(N_I64(INT64_MAX), INT32_MAX && errno == 0);
CHECK_GET_INT(N_I64(INT64_MIN), INT32_MIN && errno == 0);
CHECK_GET_INT(N_STR(I64_MAX_S), INT32_MAX && errno == 0);
CHECK_GET_INT(N_STR(I64_MIN_S), INT32_MIN && errno == 0);
printf("INT GET PASSED\n");
CHECK_GET_INT64(N_I64(INT64_MAX), INT64_MAX && errno == 0);
CHECK_GET_INT64(N_I64(INT64_MIN), INT64_MIN && errno == 0);
CHECK_GET_INT64(N_STR(I64_MAX_S), INT64_MAX && errno == 0);
CHECK_GET_INT64(N_STR(I64_MIN_S), INT64_MIN && errno == 0);
CHECK_GET_INT64(N_STR(I64_OVER), INT64_MAX && errno == ERANGE);
CHECK_GET_INT64(N_STR(I64_UNDER), INT64_MIN && errno == ERANGE);
printf("INT64 GET PASSED\n");
CHECK_GET_UINT64(N_U64(UINT64_MAX), UINT64_MAX && errno == 0);
CHECK_GET_UINT64(N_U64(-1), UINT64_MAX && errno == 0);
CHECK_GET_UINT64(N_STR(U64_OUT_S), UINT64_MAX && errno == ERANGE);
printf("UINT64 GET PASSED\n");
printf("PASSED\n");
return 0;
}

View File

@@ -0,0 +1,5 @@
BASE CHECK PASSED
INT GET PASSED
INT64 GET PASSED
UINT64 GET PASSED
PASSED

1
tests/test_int_get.test Symbolic link
View File

@@ -0,0 +1 @@
test_basic.test

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
@@ -21,8 +24,6 @@ int main(int argc, char **argv)
json_object *new_obj;
#ifdef HAVE_SETLOCALE
setlocale(LC_NUMERIC, "de_DE");
#else
printf("No locale\n");
#endif
char buf1[10], buf2[10];

View File

@@ -2,6 +2,9 @@
* Tests if binary strings are supported.
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <string.h>

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <stdlib.h>

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
@@ -35,13 +38,14 @@ static void do_clear_serializer(json_object *jso);
static void single_incremental_parse(const char *test_string, int clear_serializer)
{
int ii;
size_t ii;
int chunksize = atoi(getenv("TEST_PARSE_CHUNKSIZE"));
struct json_tokener *tok;
enum json_tokener_error jerr;
json_object *all_at_once_obj, *new_obj;
const char *all_at_once_str, *new_str;
new_obj = NULL;
assert(chunksize > 0);
all_at_once_obj = json_tokener_parse(test_string);
if (clear_serializer)
@@ -49,7 +53,7 @@ static void single_incremental_parse(const char *test_string, int clear_serializ
all_at_once_str = json_object_to_json_string(all_at_once_obj);
tok = json_tokener_new();
int test_string_len = strlen(test_string) + 1; // Including '\0' !
size_t test_string_len = strlen(test_string) + 1; // Including '\0' !
for (ii = 0; ii < test_string_len; ii += chunksize)
{
int len_to_parse = chunksize;
@@ -92,7 +96,7 @@ static void single_basic_parse(const char *test_string, int clear_serializer)
if (getenv("TEST_PARSE_CHUNKSIZE") != NULL)
single_incremental_parse(test_string, clear_serializer);
}
static void test_basic_parse()
static void test_basic_parse(void)
{
single_basic_parse("\"\003\"", 0);
single_basic_parse("/* hello */\"foo\"", 0);
@@ -195,13 +199,13 @@ static void test_basic_parse()
single_basic_parse("[18446744073709551616]", 1);
}
static void test_utf8_parse()
static void test_utf8_parse(void)
{
// json_tokener_parse doesn't support checking for byte order marks.
// It's the responsibility of the caller to detect and skip a BOM.
// Both of these checks return null.
char *utf8_bom = "\xEF\xBB\xBF";
char *utf8_bom_and_chars = "\xEF\xBB\xBF{}";
const char *utf8_bom = "\xEF\xBB\xBF";
const char *utf8_bom_and_chars = "\xEF\xBB\xBF{}";
single_basic_parse(utf8_bom, 0);
single_basic_parse(utf8_bom_and_chars, 0);
}
@@ -222,7 +226,7 @@ static int clear_serializer(json_object *jso, int flags, json_object *parent_jso
return JSON_C_VISIT_RETURN_CONTINUE;
}
static void test_verbose_parse()
static void test_verbose_parse(void)
{
json_object *new_obj;
enum json_tokener_error error = json_tokener_success;
@@ -256,24 +260,24 @@ struct incremental_step
} incremental_steps[] = {
/* Check that full json messages can be parsed, both w/ and w/o a reset */
{"{ \"foo\": 123 }", -1, -1, json_tokener_success, 0},
{"{ \"foo\": 456 }", -1, -1, json_tokener_success, 1},
{"{ \"foo\": 789 }", -1, -1, json_tokener_success, 1},
{"{ \"foo\": 123 }", -1, -1, json_tokener_success, 0, 0},
{"{ \"foo\": 456 }", -1, -1, json_tokener_success, 1, 0},
{"{ \"foo\": 789 }", -1, -1, json_tokener_success, 1, 0},
/* Check the comment parse*/
{"/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0},
{"/* hello */:/* hello */", -1, -1, json_tokener_continue, 0},
{"\"bar\"/* hello */", -1, -1, json_tokener_continue, 0},
{"}/* hello */", -1, -1, json_tokener_success, 1},
{"/ hello ", -1, 1, json_tokener_error_parse_comment, 1},
{"/* hello\"foo\"", -1, -1, json_tokener_continue, 1},
{"/* hello*\"foo\"", -1, -1, json_tokener_continue, 1},
{"// hello\"foo\"", -1, -1, json_tokener_continue, 1},
{"/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0, 0},
{"/* hello */:/* hello */", -1, -1, json_tokener_continue, 0, 0},
{"\"bar\"/* hello */", -1, -1, json_tokener_continue, 0, 0},
{"}/* hello */", -1, -1, json_tokener_success, 1, 0},
{"/ hello ", -1, 1, json_tokener_error_parse_comment, 1, 0},
{"/* hello\"foo\"", -1, -1, json_tokener_continue, 1, 0},
{"/* hello*\"foo\"", -1, -1, json_tokener_continue, 1, 0},
{"// hello\"foo\"", -1, -1, json_tokener_continue, 1, 0},
/* Check a basic incremental parse */
{"{ \"foo", -1, -1, json_tokener_continue, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0},
{"\":13}}", -1, -1, json_tokener_success, 1},
{"{ \"foo", -1, -1, json_tokener_continue, 0, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0, 0},
{"\":13}}", -1, -1, json_tokener_success, 1, 0},
/* Check the UTF-16 surrogate pair handling in various ways.
* Note: \ud843\udd1e is u+1D11E, Musical Symbol G Clef
@@ -281,56 +285,56 @@ struct incremental_step
* PuTTY doesn't currently show this character.
*/
/* parse one char at every time */
{"\"\\", -1, -1, json_tokener_continue, 0},
{"u", -1, -1, json_tokener_continue, 0},
{"d", -1, -1, json_tokener_continue, 0},
{"8", -1, -1, json_tokener_continue, 0},
{"3", -1, -1, json_tokener_continue, 0},
{"4", -1, -1, json_tokener_continue, 0},
{"\\", -1, -1, json_tokener_continue, 0},
{"u", -1, -1, json_tokener_continue, 0},
{"d", -1, -1, json_tokener_continue, 0},
{"d", -1, -1, json_tokener_continue, 0},
{"1", -1, -1, json_tokener_continue, 0},
{"e\"", -1, -1, json_tokener_success, 1},
{"\"\\", -1, -1, json_tokener_continue, 0, 0},
{"u", -1, -1, json_tokener_continue, 0, 0},
{"d", -1, -1, json_tokener_continue, 0, 0},
{"8", -1, -1, json_tokener_continue, 0, 0},
{"3", -1, -1, json_tokener_continue, 0, 0},
{"4", -1, -1, json_tokener_continue, 0, 0},
{"\\", -1, -1, json_tokener_continue, 0, 0},
{"u", -1, -1, json_tokener_continue, 0, 0},
{"d", -1, -1, json_tokener_continue, 0, 0},
{"d", -1, -1, json_tokener_continue, 0, 0},
{"1", -1, -1, json_tokener_continue, 0, 0},
{"e\"", -1, -1, json_tokener_success, 1, 0},
/* parse two char at every time */
{"\"\\u", -1, -1, json_tokener_continue, 0},
{"d8", -1, -1, json_tokener_continue, 0},
{"34", -1, -1, json_tokener_continue, 0},
{"\\u", -1, -1, json_tokener_continue, 0},
{"dd", -1, -1, json_tokener_continue, 0},
{"1e\"", -1, -1, json_tokener_success, 1},
{"\"\\u", -1, -1, json_tokener_continue, 0, 0},
{"d8", -1, -1, json_tokener_continue, 0, 0},
{"34", -1, -1, json_tokener_continue, 0, 0},
{"\\u", -1, -1, json_tokener_continue, 0, 0},
{"dd", -1, -1, json_tokener_continue, 0, 0},
{"1e\"", -1, -1, json_tokener_success, 1, 0},
/* check the low surrogate pair */
{"\"\\ud834", -1, -1, json_tokener_continue, 0},
{"\\udd1e\"", -1, -1, json_tokener_success, 1},
{"\"\\ud834\\", -1, -1, json_tokener_continue, 0},
{"udd1e\"", -1, -1, json_tokener_success, 1},
{"\"\\ud834\\u", -1, -1, json_tokener_continue, 0},
{"dd1e\"", -1, -1, json_tokener_success, 1},
{"\"fff \\ud834\\ud", -1, -1, json_tokener_continue, 0},
{"d1e bar\"", -1, -1, json_tokener_success, 1},
{"\"fff \\ud834\\udd", -1, -1, json_tokener_continue, 0},
{"1e bar\"", -1, -1, json_tokener_success, 1},
{"\"\\ud834", -1, -1, json_tokener_continue, 0, 0},
{"\\udd1e\"", -1, -1, json_tokener_success, 1, 0},
{"\"\\ud834\\", -1, -1, json_tokener_continue, 0, 0},
{"udd1e\"", -1, -1, json_tokener_success, 1, 0},
{"\"\\ud834\\u", -1, -1, json_tokener_continue, 0, 0},
{"dd1e\"", -1, -1, json_tokener_success, 1, 0},
{"\"fff \\ud834\\ud", -1, -1, json_tokener_continue, 0, 0},
{"d1e bar\"", -1, -1, json_tokener_success, 1, 0},
{"\"fff \\ud834\\udd", -1, -1, json_tokener_continue, 0, 0},
{"1e bar\"", -1, -1, json_tokener_success, 1, 0},
/* \ud83d\ude00 is U+1F600, Grinning Face
* Displays fine in PuTTY, though you may need "less -r"
*/
{"\"fff \\ud83d\\ude", -1, -1, json_tokener_continue, 0},
{"00 bar\"", -1, -1, json_tokener_success, 1},
{"\"fff \\ud83d\\ude", -1, -1, json_tokener_continue, 0, 0},
{"00 bar\"", -1, -1, json_tokener_success, 1, 0},
/* Check that json_tokener_reset actually resets */
{"{ \"foo", -1, -1, json_tokener_continue, 1},
{": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1},
{"{ \"foo", -1, -1, json_tokener_continue, 1, 0},
{": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1, 0},
/* Check incremental parsing with trailing characters */
{"{ \"foo", -1, -1, json_tokener_continue, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0},
{"\":13}}XXXX", 10, 6, json_tokener_success, 0},
{"XXXX", 4, 0, json_tokener_error_parse_unexpected, 1},
{"{ \"foo", -1, -1, json_tokener_continue, 0, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0, 0},
{"\":13}}XXXX", 10, 6, json_tokener_success, 0, 0},
{"XXXX", 4, 0, json_tokener_error_parse_unexpected, 1, 0},
/* Check that trailing characters can change w/o a reset */
{"{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0},
{"\"Y\"", -1, -1, json_tokener_success, 1},
{"{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0, 0},
{"\"Y\"", -1, -1, json_tokener_success, 1, 0},
/* Trailing characters should cause a failure in strict mode */
{"{\"foo\":9}{\"bar\":8}", -1, 9, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
@@ -342,116 +346,148 @@ struct incremental_step
JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS},
/* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
{"1", 1, 1, json_tokener_continue, 0},
{"1", 1, 1, json_tokener_continue, 0, 0},
/* This should parse as the number 12, since it continues the "1" */
{"2", 2, 1, json_tokener_success, 0},
{"12{", 3, 2, json_tokener_success, 1},
{"2", 2, 1, json_tokener_success, 0, 0},
{"12{", 3, 2, json_tokener_success, 1, 0},
/* Parse number in strict mode */
{"[02]", -1, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"0e+0", 5, 4, json_tokener_success, 1},
{"[0e+0]", -1, -1, json_tokener_success, 1},
{"0e+0", 5, 4, json_tokener_success, 1, 0},
{"[0e+0]", -1, -1, json_tokener_success, 1, 0},
/* The behavior when missing the exponent varies slightly */
{"0e", 2, 2, json_tokener_continue, 1},
{"0e", 3, 2, json_tokener_success, 1},
{"0e", 2, 2, json_tokener_continue, 1, 0},
{"0e", 3, 2, json_tokener_success, 1, 0},
{"0e", 3, 2, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
{"[0e]", -1, -1, json_tokener_success, 1},
{"[0e]", -1, -1, json_tokener_success, 1, 0},
{"[0e]", -1, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"0e+", 3, 3, json_tokener_continue, 1},
{"0e+", 4, 3, json_tokener_success, 1},
{"0e+", 3, 3, json_tokener_continue, 1, 0},
{"0e+", 4, 3, json_tokener_success, 1, 0},
{"0e+", 4, 3, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
{"[0e+]", -1, -1, json_tokener_success, 1},
{"[0e+]", -1, -1, json_tokener_success, 1, 0},
{"[0e+]", -1, 4, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"0e-", 3, 3, json_tokener_continue, 1},
{"0e-", 4, 3, json_tokener_success, 1},
{"0e-", 3, 3, json_tokener_continue, 1, 0},
{"0e-", 4, 3, json_tokener_success, 1, 0},
{"0e-", 4, 3, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
{"[0e-]", -1, -1, json_tokener_success, 1},
{"[0e-]", -1, -1, json_tokener_success, 1, 0},
{"[0e-]", -1, 4, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
/* You might expect this to fail, but it won't because
it's a valid partial parse; note the char_offset: */
{"0e+-", 5, 3, json_tokener_success, 1},
{"0e+-", 5, 3, json_tokener_success, 1, 0},
{"0e+-", 5, 3, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
{"[0e+-]", -1, 4, json_tokener_error_parse_number, 1},
{"[0e+-]", -1, 4, json_tokener_error_parse_number, 1, 0},
/* Similar tests for other kinds of objects: */
/* These could all return success immediately, since regardless of
what follows the false/true/null token we *will* return a json object,
but it currently doesn't work that way. hmm... */
{"false", 5, 5, json_tokener_continue, 1},
{"false", 6, 5, json_tokener_success, 1},
{"true", 4, 4, json_tokener_continue, 1},
{"true", 5, 4, json_tokener_success, 1},
{"null", 4, 4, json_tokener_continue, 1},
{"null", 5, 4, json_tokener_success, 1},
{"false", 5, 5, json_tokener_continue, 1, 0},
{"false", 6, 5, json_tokener_success, 1, 0},
{"true", 4, 4, json_tokener_continue, 1, 0},
{"true", 5, 4, json_tokener_success, 1, 0},
{"null", 4, 4, json_tokener_continue, 1, 0},
{"null", 5, 4, json_tokener_success, 1, 0},
{"Infinity", 9, 8, json_tokener_success, 1},
{"infinity", 9, 8, json_tokener_success, 1},
{"-infinity", 10, 9, json_tokener_success, 1},
{"Infinity", 9, 8, json_tokener_success, 1, 0},
{"infinity", 9, 8, json_tokener_success, 1, 0},
{"-infinity", 10, 9, json_tokener_success, 1, 0},
{"infinity", 9, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
{"-infinity", 10, 1, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
{"inf", 3, 3, json_tokener_continue, 0},
{"inity", 6, 5, json_tokener_success, 1},
{"-inf", 4, 4, json_tokener_continue, 0},
{"inity", 6, 5, json_tokener_success, 1},
{"inf", 3, 3, json_tokener_continue, 0, 0},
{"inity", 6, 5, json_tokener_success, 1, 0},
{"-inf", 4, 4, json_tokener_continue, 0, 0},
{"inity", 6, 5, json_tokener_success, 1, 0},
{"i", 1, 1, json_tokener_continue, 0},
{"n", 1, 1, json_tokener_continue, 0},
{"f", 1, 1, json_tokener_continue, 0},
{"i", 1, 1, json_tokener_continue, 0},
{"n", 1, 1, json_tokener_continue, 0},
{"i", 1, 1, json_tokener_continue, 0},
{"t", 1, 1, json_tokener_continue, 0},
{"y", 1, 1, json_tokener_continue, 0},
{"", 1, 0, json_tokener_success, 1},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"n", 1, 1, json_tokener_continue, 0, 0},
{"f", 1, 1, json_tokener_continue, 0, 0},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"n", 1, 1, json_tokener_continue, 0, 0},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"t", 1, 1, json_tokener_continue, 0, 0},
{"y", 1, 1, json_tokener_continue, 0, 0},
{"", 1, 0, json_tokener_success, 1, 0},
{"-", 1, 1, json_tokener_continue, 0},
{"inf", 3, 3, json_tokener_continue, 0},
{"ini", 3, 3, json_tokener_continue, 0},
{"ty", 3, 2, json_tokener_success, 1},
{"-", 1, 1, json_tokener_continue, 0, 0},
{"inf", 3, 3, json_tokener_continue, 0, 0},
{"ini", 3, 3, json_tokener_continue, 0, 0},
{"ty", 3, 2, json_tokener_success, 1, 0},
{"-", 1, 1, json_tokener_continue, 0},
{"i", 1, 1, json_tokener_continue, 0},
{"nfini", 5, 5, json_tokener_continue, 0},
{"ty", 3, 2, json_tokener_success, 1},
{"-", 1, 1, json_tokener_continue, 0, 0},
{"i", 1, 1, json_tokener_continue, 0, 0},
{"nfini", 5, 5, json_tokener_continue, 0, 0},
{"ty", 3, 2, json_tokener_success, 1, 0},
{"-i", 2, 2, json_tokener_continue, 0},
{"nfinity", 8, 7, json_tokener_success, 1},
{"-i", 2, 2, json_tokener_continue, 0, 0},
{"nfinity", 8, 7, json_tokener_success, 1, 0},
{"InfinityX", 10, 8, json_tokener_success, 0},
{"X", 1, 0, json_tokener_error_parse_unexpected, 1},
{"InfinityX", 10, 8, json_tokener_success, 0, 0},
{"X", 1, 0, json_tokener_error_parse_unexpected, 1, 0},
{"Infinity1234", 13, 8, json_tokener_success, 0},
{"1234", 5, 4, json_tokener_success, 1},
{"Infinity1234", 13, 8, json_tokener_success, 0, 0},
{"1234", 5, 4, json_tokener_success, 1, 0},
{"Infinity9999", 8, 8, json_tokener_continue, 0},
{"Infinity9999", 8, 8, json_tokener_continue, 0, 0},
/* returns the Infinity loaded up by the previous call: */
{"1234", 5, 0, json_tokener_success, 0},
{"1234", 5, 4, json_tokener_success, 1},
{"1234", 5, 0, json_tokener_success, 0, 0},
{"1234", 5, 4, json_tokener_success, 1, 0},
/* INT64_MAX */
{"[9223372036854775807]", 22, 21, json_tokener_success, 1, 0},
/* INT64_MAX+1 => parsed as uint64 */
{"[9223372036854775808]", 22, 21, json_tokener_success, 1, 0},
/* INT64_MIN */
{"[-9223372036854775808]", 23, 22, json_tokener_success, 1, 0},
/* INT64_MIN-1 => success, but value ends up capped */
{"[-9223372036854775809]", 23, 22, json_tokener_success, 1, 0},
/* INT64_MIN-1 => failure due to underflow detected */
{"[-9223372036854775809]", 23, 21, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
/* UINT64_MAX */
{"[18446744073709551615]", 23, 22, json_tokener_success, 1, 0},
/* UINT64_MAX+1 => success, but value ends up capped */
{"[18446744073709551616]", 23, 22, json_tokener_success, 1, 0},
/* UINT64_MAX+1 => failure due to overflow detected */
{"[18446744073709551616]", 23, 21, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT},
/* XXX this seems like a bug, should fail with _error_parse_number instead */
{"18446744073709551616", 21, 20, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT},
/* Exceeding integer limits as double parse OK */
{"[9223372036854775808.0]", 24, 23, json_tokener_success, 1, 0},
{"[-9223372036854775809.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT},
{"[18446744073709551615.0]", 25, 24, json_tokener_success, 1, 0},
{"[18446744073709551616.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT},
/* offset=1 because "n" is the start of "null". hmm... */
{"noodle", 7, 1, json_tokener_error_parse_null, 1},
{"noodle", 7, 1, json_tokener_error_parse_null, 1, 0},
/* offset=2 because "na" is the start of "nan". hmm... */
{"naodle", 7, 2, json_tokener_error_parse_null, 1},
{"naodle", 7, 2, json_tokener_error_parse_null, 1, 0},
/* offset=2 because "tr" is the start of "true". hmm... */
{"track", 6, 2, json_tokener_error_parse_boolean, 1},
{"fail", 5, 2, json_tokener_error_parse_boolean, 1},
{"track", 6, 2, json_tokener_error_parse_boolean, 1, 0},
{"fail", 5, 2, json_tokener_error_parse_boolean, 1, 0},
/* Although they may initially look like they should fail,
* the next few tests check that parsing multiple sequential
* json objects in the input works as expected
*/
{"null123", 8, 4, json_tokener_success, 0},
{&"null123"[4], 4, 3, json_tokener_success, 1},
{"nullx", 6, 4, json_tokener_success, 0},
{&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1},
{"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0},
{&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1},
{"null123", 8, 4, json_tokener_success, 0, 0},
{&"null123"[4], 4, 3, json_tokener_success, 1, 0},
{"nullx", 6, 4, json_tokener_success, 0, 0},
{&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1, 0},
{"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0, 0},
{&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1, 0},
/*
* Though this may seem invalid at first glance, it
@@ -463,52 +499,52 @@ struct incremental_step
* or json_type_double objects without some other delimiter.
* e.g. whitespace
*/
{&"2015-01-15"[0], 11, 4, json_tokener_success, 1},
{&"2015-01-15"[4], 7, 3, json_tokener_success, 1},
{&"2015-01-15"[7], 4, 3, json_tokener_success, 1},
{&"2015 01 15"[0], 11, 5, json_tokener_success, 1},
{&"2015 01 15"[4], 7, 4, json_tokener_success, 1},
{&"2015 01 15"[7], 4, 3, json_tokener_success, 1},
{&"2015-01-15"[0], 11, 4, json_tokener_success, 1, 0},
{&"2015-01-15"[4], 7, 3, json_tokener_success, 1, 0},
{&"2015-01-15"[7], 4, 3, json_tokener_success, 1, 0},
{&"2015 01 15"[0], 11, 5, json_tokener_success, 1, 0},
{&"2015 01 15"[4], 7, 4, json_tokener_success, 1, 0},
{&"2015 01 15"[7], 4, 3, json_tokener_success, 1, 0},
/* Strings have a well defined end point, so we can stop at the quote */
{"\"blue\"", -1, -1, json_tokener_success, 0},
{"\"blue\"", -1, -1, json_tokener_success, 0, 0},
/* Check each of the escape sequences defined by the spec */
{"\"\\\"\"", -1, -1, json_tokener_success, 0},
{"\"\\\\\"", -1, -1, json_tokener_success, 0},
{"\"\\b\"", -1, -1, json_tokener_success, 0},
{"\"\\f\"", -1, -1, json_tokener_success, 0},
{"\"\\n\"", -1, -1, json_tokener_success, 0},
{"\"\\r\"", -1, -1, json_tokener_success, 0},
{"\"\\t\"", -1, -1, json_tokener_success, 0},
{"\"\\/\"", -1, -1, json_tokener_success, 0},
{"\"\\\"\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\\\\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\b\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\f\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\n\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\r\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\t\"", -1, -1, json_tokener_success, 0, 0},
{"\"\\/\"", -1, -1, json_tokener_success, 0, 0},
// Escaping a forward slash is optional
{"\"/\"", -1, -1, json_tokener_success, 0},
{"\"/\"", -1, -1, json_tokener_success, 0, 0},
/* Check wrong escape sequences */
{"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1},
{"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1, 0},
/* Check '\'' in strict model */
{"\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
/* Parse array/object */
{"[1,2,3]", -1, -1, json_tokener_success, 0},
{"[1,2,3}", -1, 6, json_tokener_error_parse_array, 1},
{"{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1},
{"{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1},
{"{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1},
{"{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1},
{"{\"a\":1,\"a\":2}", -1, -1, json_tokener_success, 1},
{"\"a\":1}", -1, 3, json_tokener_success, 1},
{"{\"a\":1", -1, -1, json_tokener_continue, 1},
{"[,]", -1, 1, json_tokener_error_parse_unexpected, 1},
{"[,1]", -1, 1, json_tokener_error_parse_unexpected, 1},
{"[1,2,3]", -1, -1, json_tokener_success, 0, 0},
{"[1,2,3}", -1, 6, json_tokener_error_parse_array, 1, 0},
{"{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1, 0},
{"{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1, 0},
{"{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1, 0},
{"{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1, 0},
{"{\"a\":1,\"a\":2}", -1, -1, json_tokener_success, 1, 0},
{"\"a\":1}", -1, 3, json_tokener_success, 1, 0},
{"{\"a\":1", -1, -1, json_tokener_continue, 1, 0},
{"[,]", -1, 1, json_tokener_error_parse_unexpected, 1, 0},
{"[,1]", -1, 1, json_tokener_error_parse_unexpected, 1, 0},
/* This behaviour doesn't entirely follow the json spec, but until we have
* a way to specify how strict to be we follow Postel's Law and be liberal
* in what we accept (up to a point).
*/
{"[1,2,3,]", -1, -1, json_tokener_success, 0},
{"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0},
{"[1,2,3,]", -1, -1, json_tokener_success, 0, 0},
{"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0, 0},
{"[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
{"{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT},
@@ -517,24 +553,24 @@ struct incremental_step
// acsll encoding
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1},
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1, 0},
// utf-8 encoding
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xe4\xb8", -1, 3, json_tokener_error_parse_utf8_string, 0, JSON_TOKENER_VALIDATE_UTF8},
{"\x96\xe7\x95\x8c\x22", -1, 0, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1},
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1, 0},
{"\x22\xcf\x80\xcf\x86\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xf0\xa5\x91\x95\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
// wrong utf-8 encoding
{"\x22\xe6\x9d\x4e\x22", -1, 3, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xe6\x9d\x4e\x22", -1, 5, json_tokener_success, 1},
{"\x22\xe6\x9d\x4e\x22", -1, 5, json_tokener_success, 1, 0},
// GBK encoding
{"\x22\xc0\xee\xc5\xf4\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{"\x22\xc0\xee\xc5\xf4\x22", -1, 6, json_tokener_success, 1},
{"\x22\xc0\xee\xc5\xf4\x22", -1, 6, json_tokener_success, 1, 0},
// char after space
{"\x20\x20\x22\xe4\xb8\x96\x22", -1, -1, json_tokener_success, 1, JSON_TOKENER_VALIDATE_UTF8},
{"\x20\x20\x81\x22\xe4\xb8\x96\x22", -1, 2, json_tokener_error_parse_utf8_string, 1,
@@ -542,7 +578,7 @@ struct incremental_step
{"\x5b\x20\x81\x31\x5d", -1, 2, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
// char in state inf
{"\x49\x6e\x66\x69\x6e\x69\x74\x79", 9, 8, json_tokener_success, 1},
{"\x49\x6e\x66\x69\x6e\x69\x74\x79", 9, 8, json_tokener_success, 1, 0},
{"\x49\x6e\x66\x81\x6e\x69\x74\x79", -1, 3, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
// char in escape unicode
@@ -559,10 +595,10 @@ struct incremental_step
{"\x7b\x22\x31\x81\x22\x3a\x31\x7d", -1, 3, json_tokener_error_parse_utf8_string, 1,
JSON_TOKENER_VALIDATE_UTF8},
{NULL, -1, -1, json_tokener_success, 0},
{NULL, -1, -1, json_tokener_success, 0, 0},
};
static void test_incremental_parse()
static void test_incremental_parse(void)
{
json_object *new_obj;
enum json_tokener_error jerr;

View File

@@ -210,6 +210,19 @@ json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [int]:
json_tokener_parse_ex(tok, Infinity9999, 8) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [double]: Infinity
json_tokener_parse_ex(tok, 1234 , 5) ... OK: got object of type [int]: 1234
json_tokener_parse_ex(tok, [9223372036854775807], 22) ... OK: got object of type [array]: [ 9223372036854775807 ]
json_tokener_parse_ex(tok, [9223372036854775808], 22) ... OK: got object of type [array]: [ 9223372036854775808 ]
json_tokener_parse_ex(tok, [-9223372036854775808], 23) ... OK: got object of type [array]: [ -9223372036854775808 ]
json_tokener_parse_ex(tok, [-9223372036854775809], 23) ... OK: got object of type [array]: [ -9223372036854775808 ]
json_tokener_parse_ex(tok, [-9223372036854775809], 23) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, [18446744073709551615], 23) ... OK: got object of type [array]: [ 18446744073709551615 ]
json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got object of type [array]: [ 18446744073709551615 ]
json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got correct error: number expected
json_tokener_parse_ex(tok, 18446744073709551616, 21) ... OK: got correct error: unexpected end of data
json_tokener_parse_ex(tok, [9223372036854775808.0], 24) ... OK: got object of type [array]: [ 9223372036854775808.0 ]
json_tokener_parse_ex(tok, [-9223372036854775809.0], 25) ... OK: got object of type [array]: [ -9223372036854775809.0 ]
json_tokener_parse_ex(tok, [18446744073709551615.0], 25) ... OK: got object of type [array]: [ 18446744073709551615.0 ]
json_tokener_parse_ex(tok, [18446744073709551616.0], 25) ... OK: got object of type [array]: [ 18446744073709551616.0 ]
json_tokener_parse_ex(tok, noodle , 7) ... OK: got correct error: null expected
json_tokener_parse_ex(tok, naodle , 7) ... OK: got correct error: null expected
json_tokener_parse_ex(tok, track , 6) ... OK: got correct error: boolean expected
@@ -275,5 +288,5 @@ json_tokener_parse_ex(tok, "\ud855
json_tokener_parse_ex(tok, "\ud0031<33>" , 10) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, 11<31>11 , 5) ... OK: got correct error: invalid utf-8 string
json_tokener_parse_ex(tok, {"1<>":1} , 8) ... OK: got correct error: invalid utf-8 string
End Incremental Tests OK=185 ERROR=0
End Incremental Tests OK=198 ERROR=0
==================================

View File

@@ -1,4 +1,7 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <stdio.h>
#include <string.h>
@@ -31,7 +34,7 @@ void checkit_uint(const char *buf)
* This always exits with a 0 exit value. The output should be compared
* against previously saved expected output.
*/
int main()
int main(int argc, char **argv)
{
char buf[100];

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <limits.h>
#include <stddef.h>
@@ -16,7 +19,7 @@ static void test_printbuf_memset_length(void);
#define __func__ __FUNCTION__
#endif
static void test_basic_printbuf_memset()
static void test_basic_printbuf_memset(void)
{
struct printbuf *pb;
@@ -29,7 +32,7 @@ static void test_basic_printbuf_memset()
printf("%s: end test\n", __func__);
}
static void test_printbuf_memset_length()
static void test_printbuf_memset_length(void)
{
struct printbuf *pb;

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
@@ -73,7 +76,8 @@ int main(int argc, char **argv)
my_sub_object = json_object_new_double(1.0);
json_object_object_add(my_object, "double", my_sub_object);
printf("Check that the custom serializer does not include nul byte:\n");
json_object_set_serializer(my_sub_object, json_object_double_to_json_string, "%125.0f,", NULL);
#define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
json_object_set_serializer(my_sub_object, json_object_double_to_json_string, UNCONST("%125.0f"), NULL);
printf("my_object.to_string(custom serializer)=%s\n",
json_object_to_json_string_ext(my_object, JSON_C_TO_STRING_NOZERO));

View File

@@ -9,4 +9,4 @@ my_object.to_string(custom serializer)=Custom Output
Next line of output should be from the custom freeit function:
freeit, value=123
Check that the custom serializer does not include nul byte:
my_object.to_string(custom serializer)={"double": 1.}
my_object.to_string(custom serializer)={"double": 1}

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
@@ -68,6 +71,14 @@ int main(int argc, char **argv)
json_object_set_string(tmp, SHORT);
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
assert(strcmp(json_object_to_json_string(tmp), "\"" SHORT "\"") == 0);
// Set an empty string a couple times to try to trigger
// a case that used to leak memory.
json_object_set_string(tmp, "");
json_object_set_string(tmp, HUGE);
json_object_set_string(tmp, "");
json_object_set_string(tmp, HUGE);
json_object_put(tmp);
printf("STRING PASSED\n");

View File

@@ -1,5 +1,7 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "strerror_override.h"
#include "strerror_override_private.h"
#include <stdio.h>

View File

@@ -1,5 +1,7 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "strerror_override.h"
#include "strerror_override_private.h"
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <io.h>
@@ -35,7 +37,7 @@ static void test_read_fd_equal(const char *testdir);
#define PATH_MAX 256
#endif
static void test_write_to_file()
static void test_write_to_file(void)
{
json_object *jso;
@@ -90,7 +92,7 @@ static void test_write_to_file()
static void stat_and_cat(const char *file)
{
struct stat sb;
int d = open(file, O_RDONLY, 0600);
int d = open(file, O_RDONLY);
if (d < 0)
{
printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
@@ -168,7 +170,7 @@ static void test_read_valid_with_fd(const char *testdir)
char filename[PATH_MAX];
(void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
int d = open(filename, O_RDONLY, 0);
int d = open(filename, O_RDONLY);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
@@ -193,7 +195,7 @@ static void test_read_valid_nested_with_fd(const char *testdir)
char filename[PATH_MAX];
(void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
int d = open(filename, O_RDONLY, 0);
int d = open(filename, O_RDONLY);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
@@ -231,7 +233,7 @@ static void test_read_valid_nested_with_fd(const char *testdir)
close(d);
}
static void test_read_nonexistant()
static void test_read_nonexistant(void)
{
const char *filename = "./not_present.json";
@@ -249,10 +251,10 @@ static void test_read_nonexistant()
}
}
static void test_read_closed()
static void test_read_closed(void)
{
// Test reading from a closed fd
int d = open("/dev/null", O_RDONLY, 0);
int d = open("/dev/null", O_RDONLY);
if (d < 0)
{
puts("FAIL: unable to open");
@@ -287,7 +289,7 @@ static void test_read_fd_equal(const char *testdir)
json_object *jso = json_object_from_file(filename);
int d = open(filename, O_RDONLY, 0);
int d = open(filename, O_RDONLY);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));

View File

@@ -1,3 +1,6 @@
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>