Compare commits
28 Commits
long-long-
...
context
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b0d37faf3 | ||
|
|
464c9b544c | ||
|
|
a0aa2df75a | ||
|
|
ba81437601 | ||
|
|
d664199036 | ||
|
|
7e4ac634c6 | ||
|
|
1d9d2e8673 | ||
|
|
c4c52cfe58 | ||
|
|
0474d4d85f | ||
|
|
95d333b5cf | ||
|
|
d67ddd5c62 | ||
|
|
440ba84d08 | ||
|
|
649af9c2c3 | ||
|
|
69f8bb7778 | ||
|
|
99ad8cc64b | ||
|
|
47f4337604 | ||
|
|
772376ed92 | ||
|
|
04137f4ed1 | ||
|
|
479909d0a6 | ||
|
|
b277cd6a24 | ||
|
|
e82f32b359 | ||
|
|
afc246f1d5 | ||
|
|
85f76baf0e | ||
|
|
3a36ce6d32 | ||
|
|
deca87b071 | ||
|
|
5af3271726 | ||
|
|
02641037b7 | ||
|
|
1723a94b53 |
42
CHANGELOG.md
42
CHANGELOG.md
@@ -1,45 +1,3 @@
|
||||
1.7.7
|
||||
=====
|
||||
Fixes:
|
||||
------
|
||||
* Fix a memory leak when realloc fails (see #267), thanks @AlfieDeng for reporting
|
||||
* Fix a typo in the header file (see #266), thanks @zhaozhixu
|
||||
|
||||
1.7.6
|
||||
=====
|
||||
Fixes:
|
||||
------
|
||||
* Add `SONAME` to the ELF files built by the Makefile (see #252), thanks @YanhaoMo for reporting
|
||||
* Add include guards and `extern "C"` to `cJSON_Utils.h` (see #256), thanks @daschfg for reporting
|
||||
|
||||
Other changes:
|
||||
--------------
|
||||
* Mark the Makefile as deprecated in the README.
|
||||
|
||||
1.7.5
|
||||
=====
|
||||
Fixes:
|
||||
------
|
||||
* Fix a bug in the JSON Patch implementation of `cJSON Utils` (see #251), thanks @bobkocisko.
|
||||
|
||||
1.7.4
|
||||
=====
|
||||
Fixes:
|
||||
------
|
||||
* Fix potential use after free if the `string` parameter to `cJSON_AddItemToObject` is an alias of the `string` property of the object that is added (#248). Thanks @hhallen for reporting.
|
||||
|
||||
1.7.3
|
||||
=====
|
||||
Fixes:
|
||||
------
|
||||
* Fix potential double free, thanks @projectgus for reporting (see #241)
|
||||
|
||||
1.7.2
|
||||
=====
|
||||
Fixes:
|
||||
------
|
||||
* Fix the use of GNUInstallDirs variables and the pkgconfig file. Thanks @zeerd for reporting (see #240)
|
||||
|
||||
1.7.1
|
||||
=====
|
||||
Fixes:
|
||||
|
||||
@@ -7,7 +7,7 @@ include(GNUInstallDirs)
|
||||
|
||||
set(PROJECT_VERSION_MAJOR 1)
|
||||
set(PROJECT_VERSION_MINOR 7)
|
||||
set(PROJECT_VERSION_PATCH 7)
|
||||
set(PROJECT_VERSION_PATCH 1)
|
||||
set(CJSON_VERSION_SO 1)
|
||||
set(CJSON_UTILS_VERSION_SO 1)
|
||||
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
|
||||
@@ -64,7 +64,6 @@ if (ENABLE_SANITIZERS)
|
||||
-fno-omit-frame-pointer
|
||||
-fsanitize=address
|
||||
-fsanitize=undefined
|
||||
-fsanitize=float-divide-by-zero
|
||||
-fsanitize=float-cast-overflow
|
||||
-fsanitize-address-use-after-scope
|
||||
-fsanitize=integer
|
||||
@@ -107,6 +106,12 @@ endforeach()
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}")
|
||||
|
||||
#variables for pkg-config
|
||||
set(prefix "${CMAKE_INSTALL_PREFIX}")
|
||||
set(libdir "${CMAKE_INSTALL_LIBDIR}")
|
||||
set(version "${PROJECT_VERSION}")
|
||||
set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
|
||||
|
||||
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
|
||||
option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON)
|
||||
|
||||
@@ -143,15 +148,15 @@ endif()
|
||||
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY)
|
||||
|
||||
install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
|
||||
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
|
||||
install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" EXPORT "${CJSON_LIB}")
|
||||
install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
|
||||
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
||||
install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_LIB}")
|
||||
if (BUILD_SHARED_AND_STATIC_LIBS)
|
||||
install(TARGETS "${CJSON_LIB}-static" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}")
|
||||
install(TARGETS "${CJSON_LIB}-static" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
|
||||
endif()
|
||||
if(ENABLE_TARGET_EXPORT)
|
||||
# export library information for CMake projects
|
||||
install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
|
||||
install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
|
||||
endif()
|
||||
|
||||
set_target_properties("${CJSON_LIB}"
|
||||
@@ -182,15 +187,15 @@ if(ENABLE_CJSON_UTILS)
|
||||
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY)
|
||||
|
||||
install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
|
||||
install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}")
|
||||
if (BUILD_SHARED_AND_STATIC_LIBS)
|
||||
install(TARGETS "${CJSON_UTILS_LIB}-static" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}")
|
||||
install(TARGETS "${CJSON_UTILS_LIB}-static" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
|
||||
endif()
|
||||
install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson")
|
||||
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
|
||||
install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson")
|
||||
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
||||
if(ENABLE_TARGET_EXPORT)
|
||||
# export library information for CMake projects
|
||||
install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
|
||||
install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
|
||||
endif()
|
||||
|
||||
set_target_properties("${CJSON_UTILS_LIB}"
|
||||
@@ -210,7 +215,7 @@ configure_file(
|
||||
# Install package config files
|
||||
install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake
|
||||
${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake
|
||||
DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON")
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON")
|
||||
|
||||
option(ENABLE_CJSON_TEST "Enable building cJSON test" ON)
|
||||
if(ENABLE_CJSON_TEST)
|
||||
|
||||
@@ -7,7 +7,6 @@ Current Maintainer: [Max Bruckner](https://github.com/FSMaxB)
|
||||
* [Ajay Bhargav](https://github.com/ajaybhargav)
|
||||
* [Alper Akcan](https://github.com/alperakcan)
|
||||
* [Anton Sergeev](https://github.com/anton-sergeev)
|
||||
* [Bob Kocisko](https://github.com/bobkocisko)
|
||||
* [Christian Schulze](https://github.com/ChristianSch)
|
||||
* [Casperinous](https://github.com/Casperinous)
|
||||
* [Debora Grosse](https://github.com/DeboraG)
|
||||
@@ -41,8 +40,5 @@ Current Maintainer: [Max Bruckner](https://github.com/FSMaxB)
|
||||
* [Stephan Gatzka](https://github.com/gatzka)
|
||||
* [Weston Schmidt](https://github.com/schmidtw)
|
||||
* [yangfl](https://github.com/yangfl)
|
||||
* [Zhao Zhixu](https://github.com/zhaozhixu)
|
||||
|
||||
And probably more people on [SourceForge](https://sourceforge.net/p/cjson/bugs/search/?q=status%3Aclosed-rejected+or+status%3Aclosed-out-of-date+or+status%3Awont-fix+or+status%3Aclosed-fixed+or+status%3Aclosed&page=0)
|
||||
|
||||
Also thanks to all the people who reported bugs and suggested new features.
|
||||
|
||||
13
Makefile
13
Makefile
@@ -8,13 +8,10 @@ CJSON_TEST_SRC = cJSON.c test.c
|
||||
|
||||
LDLIBS = -lm
|
||||
|
||||
LIBVERSION = 1.7.7
|
||||
LIBVERSION = 1.7.1
|
||||
CJSON_SOVERSION = 1
|
||||
UTILS_SOVERSION = 1
|
||||
|
||||
CJSON_SO_LDFLAG=-Wl,-soname=$(CJSON_LIBNAME).so.$(CJSON_SOVERSION)
|
||||
UTILS_SO_LDFLAG=-Wl,-soname=$(UTILS_LIBNAME).so.$(UTILS_SOVERSION)
|
||||
|
||||
PREFIX ?= /usr/local
|
||||
INCLUDE_PATH ?= include/cjson
|
||||
LIBRARY_PATH ?= lib
|
||||
@@ -26,7 +23,7 @@ INSTALL ?= cp -a
|
||||
|
||||
# validate gcc version for use fstack-protector-strong
|
||||
MIN_GCC_VERSION = "4.9"
|
||||
GCC_VERSION := "`$(CC) -dumpversion`"
|
||||
GCC_VERSION := "`gcc -dumpversion`"
|
||||
IS_GCC_ABOVE_MIN_VERSION := $(shell expr "$(GCC_VERSION)" ">=" "$(MIN_GCC_VERSION)")
|
||||
ifeq "$(IS_GCC_ABOVE_MIN_VERSION)" "1"
|
||||
CFLAGS += -fstack-protector-strong
|
||||
@@ -45,8 +42,6 @@ STATIC = a
|
||||
## create dynamic (shared) library on Darwin (base OS for MacOSX and IOS)
|
||||
ifeq (Darwin, $(uname))
|
||||
SHARED = dylib
|
||||
CJSON_SO_LDFLAG = ""
|
||||
UTILS_SO_LDFLAG = ""
|
||||
endif
|
||||
|
||||
#cJSON library names
|
||||
@@ -95,10 +90,10 @@ $(UTILS_STATIC): $(UTILS_OBJ)
|
||||
#shared libraries .so.1.0.0
|
||||
#cJSON
|
||||
$(CJSON_SHARED_VERSION): $(CJSON_OBJ)
|
||||
$(CC) -shared -o $@ $< $(CJSON_SO_LDFLAG) $(LDFLAGS)
|
||||
$(CC) -shared -o $@ $< $(LDFLAGS)
|
||||
#cJSON_Utils
|
||||
$(UTILS_SHARED_VERSION): $(UTILS_OBJ)
|
||||
$(CC) -shared -o $@ $< $(UTILS_SO_LDFLAG) $(LDFLAGS)
|
||||
$(CC) -shared -o $@ $< $(LDFLAGS)
|
||||
|
||||
#objects
|
||||
#cJSON
|
||||
|
||||
@@ -127,11 +127,9 @@ make DESTDIR=$pkgdir install
|
||||
On Windows CMake is usually used to create a Visual Studio solution file by running it inside the Developer Command Prompt for Visual Studio, for exact steps follow the official documentation from CMake and Microsoft and use the online search engine of your choice. The descriptions of the the options above still generally apply, although not all of them work on Windows.
|
||||
|
||||
#### Makefile
|
||||
**NOTE:** This Method is deprecated. Use CMake if at all possible. Makefile support is limited to fixing bugs.
|
||||
|
||||
If you don't have CMake available, but still have GNU make. You can use the makefile to build cJSON:
|
||||
|
||||
Run this command in the directory with the source code and it will automatically compile static and shared libraries and a little test program (not the full test suite).
|
||||
Run this command in the directory with the source code and it will automatically compile static and shared libraries and a little test program.
|
||||
|
||||
```
|
||||
make all
|
||||
|
||||
57
cJSON.h
57
cJSON.h
@@ -31,7 +31,7 @@ extern "C"
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 7
|
||||
#define CJSON_VERSION_PATCH 1
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
@@ -78,6 +78,14 @@ typedef struct cJSON_Hooks
|
||||
void (*free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
/* new style allocators with userdata (e.g. for pool allocators) */
|
||||
typedef struct cJSON_Allocators
|
||||
{
|
||||
void *(*allocate)(size_t size, void *userdata);
|
||||
void (*deallocate)(void *pointer, void *userdata);
|
||||
void *(*reallocate)(void *pointer, size_t size, void *userdata); /* optional */
|
||||
} cJSON_Allocators;
|
||||
|
||||
typedef int cJSON_bool;
|
||||
|
||||
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
||||
@@ -132,31 +140,66 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ
|
||||
/* returns the version of cJSON as a string */
|
||||
CJSON_PUBLIC(const char*) cJSON_Version(void);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
typedef void* cJSON_Context;
|
||||
/*
|
||||
* Create a context object that can be passed to several functions
|
||||
* to configure their behavior and/or take their output. It will be set to the default values
|
||||
* initially, they can be changed later using the builder pattern by passing it to functions
|
||||
* that change one setting.
|
||||
*
|
||||
* A cJSON_Context object is dynamically allocated and you are responsible to free it
|
||||
* after use.
|
||||
*
|
||||
* If allocators is a NULL pointer, malloc and free are used.
|
||||
*
|
||||
* allocator_userdata can be used to pass custom data to your allocator (e.g. for pool allocators).
|
||||
* */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_CreateContext(const cJSON_Allocators * const allocators, void *allocator_userdata);
|
||||
/* Create a copy of an existing context */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_DuplicateContext(const cJSON_Context, const cJSON_Allocators * const allocators, void *allocator_userdata);
|
||||
|
||||
/* The following functions work on a context in order to set and retrieve data: */
|
||||
/* Change the allocators of a cJSON_Context and reset the userdata */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_SetAllocators(cJSON_Context context, const cJSON_Allocators allocators);
|
||||
/* Change the allocator userdata attached to a cJSON_Context */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_SetUserdata(cJSON_Context context, void *userdata);
|
||||
/* Get the position relative to the JSON where the parser stopped, return 0 if invalid. */
|
||||
CJSON_PUBLIC(size_t) cJSON_GetParseEnd(cJSON_Context context);
|
||||
/* Set how many bytes should be initially allocated for printing */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_SetPrebufferSize(cJSON_Context context, const size_t buffer_size);
|
||||
typedef enum { CJSON_FORMAT_MINIFIED = 0, CJSON_FORMAT_DEFAULT = 1 } cJSON_Format;
|
||||
/* Change the format for printing */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_SetFormat(cJSON_Context context, cJSON_Format format);
|
||||
/* Change the case sensitivity */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_MakeCaseSensitive(cJSON_Context context, cJSON_bool case_sensitive);
|
||||
/* Change if data is allowed after the JSON */
|
||||
CJSON_PUBLIC(cJSON_Context) cJSON_AllowDataAfterJson(cJSON_Context context, cJSON_bool allow_data_after_json);
|
||||
|
||||
/* Supply malloc and free functions to cJSON globally */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *json);
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
|
||||
/* Render a cJSON entity to text for transfer/storage. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool format);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
|
||||
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
||||
|
||||
@@ -988,12 +988,6 @@ static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_
|
||||
cJSON_AddItemToObject(parent, (char*)child_pointer, value);
|
||||
value = NULL;
|
||||
}
|
||||
else /* parent is not an object */
|
||||
{
|
||||
/* Couldn't find object to add to. */
|
||||
status = 9;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (value != NULL)
|
||||
|
||||
@@ -20,14 +20,6 @@
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON_Utils__h
|
||||
#define cJSON_Utils__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
/* Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. */
|
||||
@@ -80,9 +72,3 @@ CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const obje
|
||||
/* Sorts the members of the object into alphabetical order. */
|
||||
CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object);
|
||||
CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
set(CJSON_UTILS_FOUND @ENABLE_CJSON_UTILS@)
|
||||
|
||||
# The include directories used by cJSON
|
||||
set(CJSON_INCLUDE_DIRS "@CMAKE_INSTALL_FULL_INCLUDEDIR@")
|
||||
set(CJSON_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@")
|
||||
set(CJSON_INCLUDE_DIRS "@prefix@/@includedir@")
|
||||
set(CJSON_INCLUDE_DIR "@prefix@/@includedir@")
|
||||
|
||||
get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
|
||||
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
|
||||
prefix=@prefix@
|
||||
libdir=${prefix}/@libdir@
|
||||
includedir=${prefix}/@includedir@
|
||||
|
||||
Name: libcjson
|
||||
Version: @PROJECT_VERSION@
|
||||
Version: @version@
|
||||
Description: Ultralightweight JSON parser in ANSI C
|
||||
URL: https://github.com/DaveGamble/cJSON
|
||||
Libs: -L${libdir} -lcjson
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
|
||||
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
|
||||
prefix=@prefix@
|
||||
libdir=${prefix}/@libdir@
|
||||
includedir=${prefix}/@includedir@
|
||||
|
||||
Name: libcjson_utils
|
||||
Version: @PROJECT_VERSION@
|
||||
Version: @version@
|
||||
Description: An implementation of JSON Pointer, Patch and Merge Patch based on cJSON.
|
||||
URL: https://github.com/DaveGamble/cJSON
|
||||
Libs: -L${libdir} -lcjson_utils
|
||||
|
||||
@@ -57,6 +57,7 @@ if(ENABLE_CJSON_TEST)
|
||||
compare_tests
|
||||
cjson_add
|
||||
readme_examples
|
||||
context_tests
|
||||
)
|
||||
|
||||
option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.")
|
||||
|
||||
@@ -33,11 +33,11 @@ void reset(cJSON *item) {
|
||||
}
|
||||
if ((item->valuestring != NULL) && !(item->type & cJSON_IsReference))
|
||||
{
|
||||
global_hooks.deallocate(item->valuestring);
|
||||
global_context.allocators.deallocate(item->valuestring, global_context.userdata);
|
||||
}
|
||||
if ((item->string != NULL) && !(item->type & cJSON_StringIsConst))
|
||||
{
|
||||
global_hooks.deallocate(item->string);
|
||||
global_context.allocators.deallocate(item->string, global_context.userdata);
|
||||
}
|
||||
|
||||
memset(item, 0, sizeof(cJSON));
|
||||
|
||||
264
tests/context_tests.c
Normal file
264
tests/context_tests.c
Normal file
@@ -0,0 +1,264 @@
|
||||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "unity/examples/unity_config.h"
|
||||
#include "unity/src/unity.h"
|
||||
#include "common.h"
|
||||
|
||||
static void create_context_should_create_a_context(void)
|
||||
{
|
||||
internal_context *context = NULL;
|
||||
|
||||
context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
TEST_ASSERT_EQUAL_MESSAGE(context->buffer_size, 256, "buffer_size has an incorrect value.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(context->format, "format has an incorrect value.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(context->case_sensitive, "case_sensitive has an incorrect value.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(context->allow_data_after_json, "allow_data_after_json has an incorrect value.");
|
||||
TEST_ASSERT_NULL_MESSAGE(context->userdata, "Userdata should be NULL");
|
||||
TEST_ASSERT_TRUE_MESSAGE(malloc_wrapper == context->allocators.allocate, "Wrong malloc.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(realloc_wrapper == context->allocators.reallocate, "Wrong realloc.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(free_wrapper == context->allocators.deallocate, "Wrong free.");
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void* custom_allocator(size_t size, void *userdata)
|
||||
{
|
||||
*((size_t*)userdata) = size;
|
||||
return malloc(size);
|
||||
}
|
||||
static void custom_deallocator(void *pointer, void *userdata)
|
||||
{
|
||||
*((size_t*)userdata) = (size_t)pointer;
|
||||
free(pointer);
|
||||
}
|
||||
|
||||
static void create_context_should_take_custom_allocators(void)
|
||||
{
|
||||
internal_context *context = NULL;
|
||||
cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
|
||||
size_t userdata = 0;
|
||||
|
||||
context = (internal_context*)cJSON_CreateContext(&allocators, &userdata);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
TEST_ASSERT_EQUAL_MESSAGE(userdata, sizeof(internal_context), "custom allocator wasn't run properly.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(global_default_context.allocators.allocate == context->allocators.allocate, "Wrong allocator.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(global_default_context.allocators.deallocate == context->allocators.deallocate, "Wrong deallocator.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(global_default_context.allocators.reallocate == context->allocators.reallocate, "Wrong reallocator.");
|
||||
|
||||
custom_deallocator(context, &userdata);
|
||||
}
|
||||
|
||||
static void create_context_should_not_take_incomplete_allocators(void)
|
||||
{
|
||||
cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
|
||||
cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
|
||||
size_t userdata = 0;
|
||||
|
||||
TEST_ASSERT_NULL(cJSON_CreateContext(&allocators1, &userdata));
|
||||
TEST_ASSERT_NULL(cJSON_CreateContext(&allocators2, &userdata));
|
||||
}
|
||||
|
||||
static void duplicate_context_should_duplicate_a_context(void)
|
||||
{
|
||||
internal_context *context = NULL;
|
||||
|
||||
context = (internal_context*)cJSON_DuplicateContext(&global_context, NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
TEST_ASSERT_EQUAL_MEMORY(&global_context, context, sizeof(internal_context));
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void duplicate_context_should_take_custom_allocators(void)
|
||||
{
|
||||
internal_context *context = NULL;
|
||||
cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
|
||||
size_t userdata = 0;
|
||||
|
||||
context = (internal_context*)cJSON_DuplicateContext(&global_context, &allocators, &userdata);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
TEST_ASSERT_EQUAL_MESSAGE(userdata, sizeof(internal_context), "custom allocator wasn't run properly");
|
||||
|
||||
TEST_ASSERT_EQUAL_MEMORY(&global_context, context, sizeof(internal_context));
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void duplicate_context_should_not_take_incomplete_allocators(void)
|
||||
{
|
||||
cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
|
||||
cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
|
||||
size_t userdata = 0;
|
||||
|
||||
TEST_ASSERT_NULL(cJSON_DuplicateContext(&global_context, &allocators1, &userdata));
|
||||
TEST_ASSERT_NULL(cJSON_DuplicateContext(&global_context, &allocators2, &userdata));
|
||||
}
|
||||
|
||||
static void set_allocators_should_set_allocators(void)
|
||||
{
|
||||
internal_context *context = NULL;
|
||||
cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
|
||||
size_t userdata = 0;
|
||||
|
||||
context = (internal_context*)cJSON_CreateContext(&allocators, &userdata);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
context = (internal_context*)cJSON_SetAllocators(context, allocators);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
TEST_ASSERT_TRUE_MESSAGE(custom_allocator == context->allocators.allocate, "Wrong allocator.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(custom_deallocator == context->allocators.deallocate, "Wrong deallocator.");
|
||||
TEST_ASSERT_NULL_MESSAGE(context->allocators.reallocate, "Reallocator is not null");
|
||||
|
||||
custom_deallocator(context, &userdata);
|
||||
}
|
||||
|
||||
static void set_allocators_should_not_set_incomplete_allocators(void)
|
||||
{
|
||||
internal_context *context = NULL;
|
||||
cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
|
||||
cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
|
||||
|
||||
context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
TEST_ASSERT_NULL(cJSON_SetAllocators(context, allocators1));
|
||||
TEST_ASSERT_NULL(cJSON_SetAllocators(context, allocators2));
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void set_userdata_should_set_userdata(void)
|
||||
{
|
||||
internal_context *context = NULL;
|
||||
size_t userdata = 0;
|
||||
context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
context = (internal_context*)cJSON_SetUserdata(context, &userdata);
|
||||
TEST_ASSERT_TRUE_MESSAGE(context->userdata == &userdata, "Userdata is incorrect.");
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void get_parse_end_should_get_the_parse_end(void)
|
||||
{
|
||||
internal_context context = global_default_context;
|
||||
context.end_position = 42;
|
||||
|
||||
TEST_ASSERT_EQUAL_MESSAGE(cJSON_GetParseEnd(&context), 42, "Failed to get parse end.");
|
||||
}
|
||||
|
||||
static void set_prebuffer_size_should_set_buffer_size(void)
|
||||
{
|
||||
internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
context = (internal_context*)cJSON_SetPrebufferSize(context, 1024);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
TEST_ASSERT_EQUAL_MESSAGE(context->buffer_size, 1024, "Didn't set the buffer size correctly.");
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void set_prebuffer_size_should_not_allow_empty_sizes(void)
|
||||
{
|
||||
internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
TEST_ASSERT_NULL(cJSON_SetPrebufferSize(context, 0));
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void set_format_should_set_format(void)
|
||||
{
|
||||
internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
context = (internal_context*)cJSON_SetFormat(context, CJSON_FORMAT_MINIFIED);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
TEST_ASSERT_FALSE_MESSAGE(context->format, "Failed to set CJSON_FORMAT_MINIFIED.");
|
||||
|
||||
context = (internal_context*)cJSON_SetFormat(context, CJSON_FORMAT_DEFAULT);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
TEST_ASSERT_TRUE_MESSAGE(context->format, "Failed to set CJSON_FORMAT_DEFAULT.");
|
||||
|
||||
TEST_ASSERT_NULL_MESSAGE(cJSON_SetFormat(context, (cJSON_Format)3), "Failed to detect invalid format.");
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void make_case_sensitive_should_change_case_sensitivity(void)
|
||||
{
|
||||
internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
context = (internal_context*)cJSON_MakeCaseSensitive(context, false);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
TEST_ASSERT_FALSE_MESSAGE(context->case_sensitive, "Didn't set the case sensitivity correctly.");
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
static void allow_data_after_json_should_change_allow_data_after_json(void)
|
||||
{
|
||||
internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
context = (internal_context*)cJSON_AllowDataAfterJson(context, false);
|
||||
TEST_ASSERT_NOT_NULL(context);
|
||||
|
||||
TEST_ASSERT_FALSE_MESSAGE(context->allow_data_after_json, "Didn't set allow_data_after_json property correctly.");
|
||||
|
||||
free(context);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(create_context_should_create_a_context);
|
||||
RUN_TEST(create_context_should_take_custom_allocators);
|
||||
RUN_TEST(create_context_should_not_take_incomplete_allocators);
|
||||
RUN_TEST(duplicate_context_should_duplicate_a_context);
|
||||
RUN_TEST(duplicate_context_should_take_custom_allocators);
|
||||
RUN_TEST(duplicate_context_should_not_take_incomplete_allocators);
|
||||
RUN_TEST(set_allocators_should_set_allocators);
|
||||
RUN_TEST(set_allocators_should_not_set_incomplete_allocators);
|
||||
RUN_TEST(set_userdata_should_set_userdata);
|
||||
RUN_TEST(get_parse_end_should_get_the_parse_end);
|
||||
RUN_TEST(set_prebuffer_size_should_set_buffer_size);
|
||||
RUN_TEST(set_prebuffer_size_should_not_allow_empty_sizes);
|
||||
RUN_TEST(set_format_should_set_format);
|
||||
RUN_TEST(make_case_sensitive_should_change_case_sensitivity);
|
||||
RUN_TEST(allow_data_after_json_should_change_allow_data_after_json);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -80,12 +80,5 @@
|
||||
"doc": { "foo": ["bar"] },
|
||||
"patch": [ { "op": "add", "path": "/foo/-", "value": ["abc", "def"] }],
|
||||
"expected": {"foo": ["bar", ["abc", "def"]] }
|
||||
},
|
||||
|
||||
{
|
||||
"comment": "15",
|
||||
"doc": {"foo": {"bar": 1}},
|
||||
"patch": [{"op": "add", "path": "/foo/bar/baz", "value": "5"}],
|
||||
"error": "attempting to add to subfield of non-object"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -410,16 +410,18 @@ static void cjson_functions_shouldnt_crash_with_null_pointers(void)
|
||||
cJSON_Delete(item);
|
||||
}
|
||||
|
||||
static void *failing_realloc(void *pointer, size_t size)
|
||||
static void *failing_realloc(void *pointer, size_t size, void *userdata)
|
||||
{
|
||||
(void)size;
|
||||
(void)pointer;
|
||||
(void)userdata;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ensure_should_fail_on_failed_realloc(void)
|
||||
{
|
||||
printbuffer buffer = {NULL, 10, 0, 0, false, false, {&malloc, &free, &failing_realloc}};
|
||||
printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, true, true, {malloc_wrapper, free_wrapper, failing_realloc}, NULL, 0 } };
|
||||
buffer.context.userdata = &buffer;
|
||||
buffer.buffer = (unsigned char*)malloc(100);
|
||||
TEST_ASSERT_NOT_NULL(buffer.buffer);
|
||||
|
||||
@@ -429,10 +431,10 @@ static void ensure_should_fail_on_failed_realloc(void)
|
||||
static void skip_utf8_bom_should_skip_bom(void)
|
||||
{
|
||||
const unsigned char string[] = "\xEF\xBB\xBF{}";
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = string;
|
||||
buffer.length = sizeof(string);
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_TRUE(skip_utf8_bom(&buffer) == &buffer);
|
||||
TEST_ASSERT_EQUAL_UINT(3U, (unsigned int)buffer.offset);
|
||||
@@ -441,10 +443,10 @@ static void skip_utf8_bom_should_skip_bom(void)
|
||||
static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
|
||||
{
|
||||
const unsigned char string[] = " \xEF\xBB\xBF{}";
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = string;
|
||||
buffer.length = sizeof(string);
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
buffer.offset = 1;
|
||||
|
||||
TEST_ASSERT_NULL(skip_utf8_bom(&buffer));
|
||||
@@ -508,23 +510,22 @@ static void cjson_create_array_reference_should_create_an_array_reference(void)
|
||||
cJSON_Delete(number_reference);
|
||||
}
|
||||
|
||||
static void cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased(void)
|
||||
static void is_nan_should_detect_nan(void)
|
||||
{
|
||||
cJSON *object = cJSON_CreateObject();
|
||||
cJSON *number = cJSON_CreateNumber(42);
|
||||
char *name = (char*)cJSON_strdup((const unsigned char*)"number", &global_hooks);
|
||||
double nan = 0.0/0.0;
|
||||
|
||||
TEST_ASSERT_NOT_NULL(object);
|
||||
TEST_ASSERT_NOT_NULL(number);
|
||||
TEST_ASSERT_NOT_NULL(name);
|
||||
TEST_ASSERT_TRUE(is_nan(nan));
|
||||
TEST_ASSERT_FALSE(is_nan(1));
|
||||
}
|
||||
|
||||
number->string = name;
|
||||
static void is_infinity_should_detect_infinity(void)
|
||||
{
|
||||
double negative_infinity = -1.0/0.0;
|
||||
double positive_infinity = 1.0/0.0;
|
||||
|
||||
/* The following should not have a use after free
|
||||
* that would show up in valgrind or with AddressSanitizer */
|
||||
cJSON_AddItemToObject(object, number->string, number);
|
||||
|
||||
cJSON_Delete(object);
|
||||
TEST_ASSERT_TRUE(is_infinity(negative_infinity));
|
||||
TEST_ASSERT_TRUE(is_infinity(positive_infinity));
|
||||
TEST_ASSERT_FALSE(is_infinity(1));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
@@ -549,7 +550,8 @@ int main(void)
|
||||
RUN_TEST(cjson_create_string_reference_should_create_a_string_reference);
|
||||
RUN_TEST(cjson_create_object_reference_should_create_an_object_reference);
|
||||
RUN_TEST(cjson_create_array_reference_should_create_an_array_reference);
|
||||
RUN_TEST(cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased);
|
||||
RUN_TEST(is_nan_should_detect_nan);
|
||||
RUN_TEST(is_infinity_should_detect_infinity);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
@@ -44,10 +44,10 @@ static void assert_is_array(cJSON *array_item)
|
||||
|
||||
static void assert_not_array(const char *json)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = (const unsigned char*)json;
|
||||
buffer.length = strlen(json) + sizeof("");
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_FALSE(parse_array(item, &buffer));
|
||||
assert_is_invalid(item);
|
||||
@@ -55,10 +55,10 @@ static void assert_not_array(const char *json)
|
||||
|
||||
static void assert_parse_array(const char *json)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = (const unsigned char*)json;
|
||||
buffer.length = strlen(json) + sizeof("");
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_TRUE(parse_array(item, &buffer));
|
||||
assert_is_array(item);
|
||||
|
||||
@@ -45,7 +45,7 @@ static void assert_is_number(cJSON *number_item)
|
||||
|
||||
static void assert_parse_number(const char *string, int integer, double real)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = (const unsigned char*)string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
|
||||
|
||||
@@ -52,10 +52,10 @@ static void assert_is_child(cJSON *child_item, const char *name, int type)
|
||||
|
||||
static void assert_not_object(const char *json)
|
||||
{
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
|
||||
parsebuffer.content = (const unsigned char*)json;
|
||||
parsebuffer.length = strlen(json) + sizeof("");
|
||||
parsebuffer.hooks = global_hooks;
|
||||
parsebuffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_FALSE(parse_object(item, &parsebuffer));
|
||||
assert_is_invalid(item);
|
||||
@@ -64,10 +64,10 @@ static void assert_not_object(const char *json)
|
||||
|
||||
static void assert_parse_object(const char *json)
|
||||
{
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
|
||||
parsebuffer.content = (const unsigned char*)json;
|
||||
parsebuffer.length = strlen(json) + sizeof("");
|
||||
parsebuffer.hooks = global_hooks;
|
||||
parsebuffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_TRUE(parse_object(item, &parsebuffer));
|
||||
assert_is_object(item);
|
||||
|
||||
@@ -45,24 +45,24 @@ static void assert_is_string(cJSON *string_item)
|
||||
|
||||
static void assert_parse_string(const char *string, const char *expected)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = (const unsigned char*)string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer), "Couldn't parse string.");
|
||||
assert_is_string(item);
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
|
||||
global_hooks.deallocate(item->valuestring);
|
||||
global_context.allocators.deallocate(item->valuestring, global_context.userdata);
|
||||
item->valuestring = NULL;
|
||||
}
|
||||
|
||||
static void assert_not_parse_string(const char * const string)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = (const unsigned char*)string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer), "Malformed string should not be accepted.");
|
||||
assert_is_invalid(item);
|
||||
|
||||
@@ -43,10 +43,10 @@ static void assert_is_value(cJSON *value_item, int type)
|
||||
|
||||
static void assert_parse_value(const char *string, int type)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.content = (const unsigned char*) string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_TRUE(parse_value(item, &buffer));
|
||||
assert_is_value(item, type);
|
||||
|
||||
@@ -31,36 +31,36 @@ static void assert_print_array(const char * const expected, const char * const i
|
||||
|
||||
cJSON item[1];
|
||||
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_context };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_context };
|
||||
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
|
||||
parsebuffer.content = (const unsigned char*)input;
|
||||
parsebuffer.length = strlen(input) + sizeof("");
|
||||
parsebuffer.hooks = global_hooks;
|
||||
parsebuffer.context = global_context;
|
||||
|
||||
/* buffer for formatted printing */
|
||||
formatted_buffer.buffer = printed_formatted;
|
||||
formatted_buffer.length = sizeof(printed_formatted);
|
||||
formatted_buffer.offset = 0;
|
||||
formatted_buffer.noalloc = true;
|
||||
formatted_buffer.hooks = global_hooks;
|
||||
formatted_buffer.context = global_context;
|
||||
|
||||
/* buffer for unformatted printing */
|
||||
unformatted_buffer.buffer = printed_unformatted;
|
||||
unformatted_buffer.length = sizeof(printed_unformatted);
|
||||
unformatted_buffer.offset = 0;
|
||||
unformatted_buffer.noalloc = true;
|
||||
unformatted_buffer.hooks = global_hooks;
|
||||
unformatted_buffer.context = global_context;
|
||||
|
||||
memset(item, 0, sizeof(item));
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer), "Failed to parse array.");
|
||||
|
||||
unformatted_buffer.format = false;
|
||||
unformatted_buffer.context.format = false;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer), "Failed to print unformatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct.");
|
||||
|
||||
formatted_buffer.format = true;
|
||||
formatted_buffer.context.format = true;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer), "Failed to print formatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct.");
|
||||
|
||||
|
||||
@@ -28,12 +28,12 @@ static void assert_print_number(const char *expected, double input)
|
||||
{
|
||||
unsigned char printed[1024];
|
||||
cJSON item[1];
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, default_context };
|
||||
buffer.buffer = printed;
|
||||
buffer.length = sizeof(printed);
|
||||
buffer.offset = 0;
|
||||
buffer.noalloc = true;
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
memset(item, 0, sizeof(item));
|
||||
cJSON_SetNumberValue(item, input);
|
||||
|
||||
@@ -31,37 +31,37 @@ static void assert_print_object(const char * const expected, const char * const
|
||||
|
||||
cJSON item[1];
|
||||
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_context };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_context };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
|
||||
|
||||
/* buffer for parsing */
|
||||
parsebuffer.content = (const unsigned char*)input;
|
||||
parsebuffer.length = strlen(input) + sizeof("");
|
||||
parsebuffer.hooks = global_hooks;
|
||||
parsebuffer.context = global_context;
|
||||
|
||||
/* buffer for formatted printing */
|
||||
formatted_buffer.buffer = printed_formatted;
|
||||
formatted_buffer.length = sizeof(printed_formatted);
|
||||
formatted_buffer.offset = 0;
|
||||
formatted_buffer.noalloc = true;
|
||||
formatted_buffer.hooks = global_hooks;
|
||||
formatted_buffer.context = global_context;
|
||||
|
||||
/* buffer for unformatted printing */
|
||||
unformatted_buffer.buffer = printed_unformatted;
|
||||
unformatted_buffer.length = sizeof(printed_unformatted);
|
||||
unformatted_buffer.offset = 0;
|
||||
unformatted_buffer.noalloc = true;
|
||||
unformatted_buffer.hooks = global_hooks;
|
||||
unformatted_buffer.context = global_context;
|
||||
|
||||
memset(item, 0, sizeof(item));
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object.");
|
||||
|
||||
unformatted_buffer.format = false;
|
||||
unformatted_buffer.context.format = false;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer), "Failed to print unformatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct.");
|
||||
|
||||
formatted_buffer.format = true;
|
||||
formatted_buffer.context.format = true;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer), "Failed to print formatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct.");
|
||||
|
||||
|
||||
@@ -27,12 +27,12 @@
|
||||
static void assert_print_string(const char *expected, const char *input)
|
||||
{
|
||||
unsigned char printed[1024];
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, default_context };
|
||||
buffer.buffer = printed;
|
||||
buffer.length = sizeof(printed);
|
||||
buffer.offset = 0;
|
||||
buffer.noalloc = true;
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer), "Failed to print string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected.");
|
||||
|
||||
@@ -32,17 +32,18 @@ static void assert_print_value(const char *input)
|
||||
{
|
||||
unsigned char printed[1024];
|
||||
cJSON item[1];
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, default_context };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
|
||||
buffer.buffer = printed;
|
||||
buffer.length = sizeof(printed);
|
||||
buffer.offset = 0;
|
||||
buffer.noalloc = true;
|
||||
buffer.hooks = global_hooks;
|
||||
buffer.context = global_context;
|
||||
buffer.context.format = false;
|
||||
|
||||
parsebuffer.content = (const unsigned char*)input;
|
||||
parsebuffer.length = strlen(input) + sizeof("");
|
||||
parsebuffer.hooks = global_hooks;
|
||||
parsebuffer.context = global_context;
|
||||
|
||||
memset(item, 0, sizeof(item));
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
* you're going to want to define this. That way, Unity will know to skip the
|
||||
* inclusion of this file and you won't be left with a compiler error.
|
||||
*/
|
||||
#define UNITY_EXCLUDE_STDINT_H
|
||||
/* #define UNITY_EXCLUDE_STDINT_H */
|
||||
|
||||
/* ********************** MANUAL INTEGER TYPE DEFINITION ***********************
|
||||
* If you've disabled all of the automatic options above, you're going to have
|
||||
|
||||
Reference in New Issue
Block a user