Compare commits

...

33 Commits

Author SHA1 Message Date
Max Bruckner
5310da351d contributors: Add Alper Akcan 2016-12-06 09:01:12 +07:00
Max Bruckner
68196f5b14 Release version 1.1.0 2016-12-06 08:56:23 +07:00
Max Bruckner
d992cd46e6 test: Handle cJSON_PrintPreallocated return values correctly 2016-12-06 08:47:44 +07:00
Max Bruckner
84b48121bb header: explain PrintPreallocated return value 2016-12-06 08:46:48 +07:00
Max Bruckner
f10e2f884a test: exit on malloc failure 2016-12-06 08:41:51 +07:00
Max Bruckner
eeb6d237b6 Merge pull request #81 from alperakcan/master
fix memory leak in cJSON_Duplicate
2016-12-06 08:36:47 +07:00
Alper Akcan
5b4420298f fix memory leak in cJSON_Duplicate 2016-12-05 14:23:04 +03:00
Max Bruckner
e1eb06fae0 cJSON.h warn about write access to constants (#80)
Add a comment that warns about potential write access to constant memory
locations after using cJSON_AddItemToObjectCS.
2016-11-30 22:14:16 +07:00
Max Bruckner
6aefa45056 Merge pull request #79 from gatzka/fix/test
Make print_preallocated static.
2016-11-29 08:19:33 +07:00
Stephan Gatzka
345d57e9e5 Make print_preallocated static.
Otherwise compilation failes due to missing prototypes.
2016-11-28 20:21:38 +01:00
Max Bruckner
3da18a2df9 contributors: Add Kyle Chisholm 2016-11-29 00:01:21 +07:00
Max Bruckner
bf17703012 Merge branch 'print_preallocated' 2016-11-28 23:08:25 +07:00
Max Bruckner
6622c54f18 Handle out of memory when printing string 2016-11-28 23:06:38 +07:00
Kyle Chisholm
8df4cd46eb more concise return 2016-11-28 22:59:55 +07:00
Kyle Chisholm
3917fb5255 use print_preallocated function to test printout of JSON 2016-11-28 22:59:55 +07:00
Kyle Chisholm
99896d879c function to print and compare to preallocated buffer 2016-11-28 22:59:55 +07:00
Kyle Chisholm
4d1dcaa160 check print_value return 2016-11-28 22:59:54 +07:00
Kyle Chisholm
de93d76d0b changed to cJSON_PrintPreallocated, added flag in printbuffer 2016-11-28 22:59:53 +07:00
Kyle Chisholm
602c241a0b fixed "make test" on Mac (clang) 2016-11-28 22:53:36 +07:00
Kyle Chisholm
23dafa4739 added Print function call for pre-allocated buffer 2016-11-28 22:53:33 +07:00
Max Bruckner
59cf4112d2 Merge pull request #78 from gatzka/feature/gcc_check_strict_overflow_v2
gcc check strict overflow v2
2016-11-28 22:29:03 +07:00
Stephan
5cfda2292f Warn if compiler optimizes based on asumption signed overflow does not occur.
This switch is only active when making a release build.
2016-11-28 09:12:17 +01:00
Stephan
038b04d80a Take out len from condition check.
Otherwise, the check is just undefined behaviour. gcc even takes out
this check because len can never be zero if len does not wrap around.

Found with -Wstrict-overflow=2
2016-11-28 09:11:14 +01:00
Max Bruckner
624bc85143 contributors: add Stephan Gatzka 2016-11-28 01:03:04 +07:00
Max Bruckner
1f0ad823c9 Merge pull request #75 from gatzka/feature/enhance_gcc_checks_v2
Enhance gcc checks v2
2016-11-28 00:58:45 +07:00
Stephan Gatzka
29b6643bab Warn about missing prototypes.
This switch warns about missing prototypes. It does not warn if
functions are declared static. So this switch encourages the usage of
static whenever possible. This enables the compiler to perform inlining.
2016-11-27 18:22:12 +01:00
Stephan Gatzka
a8e1368697 Make function static when possible.
This is a prerequisite to later enable -Wmissing-prototypes.
2016-11-27 18:20:58 +01:00
Stephan Gatzka
bea0be48b6 Perform additional format checks. 2016-11-27 18:20:41 +01:00
Stephan Gatzka
91d11cd050 Warn if casting a pointer increases alignment of target. 2016-11-27 18:19:52 +01:00
Stephan Gatzka
9591ecddbe Warn about variables initialized by themselves. 2016-11-27 18:19:07 +01:00
Stephan Gatzka
1568015de6 Warn about shadowing variables and functions. 2016-11-27 18:18:00 +01:00
Stephan Gatzka
7ed067e680 Rename some variables to avoid shadowing. 2016-11-27 18:16:28 +01:00
Max Bruckner
4703f01cf4 Make/CMake: Fix use of compiler flags
CMake: Option was not enabled by default as intended.
Makefile: Flags were not passed when building the tests
2016-11-26 22:09:20 +07:00
7 changed files with 167 additions and 55 deletions

View File

@@ -6,16 +6,16 @@ include(GNUInstallDirs)
project(cJSON C)
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 2)
set(PROJECT_VERSION_MINOR 1)
set(PROJECT_VERSION_PATCH 0)
set(CJSON_VERSION_SO 1)
set(CJSON_UTILS_VERSION_SO 1)
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
option(ENABLE_CUSTOM_COMPILER_FLAGS ON)
option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
if (ENABLE_CUSTOM_COMPILER_FLAGS)
if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang"))
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2")
endif()
endif()

View File

@@ -2,6 +2,7 @@ Contributors
============
* [Ajay Bhargav](https://github.com/ajaybhargav)
* [Alper Akcan](https://github.com/alperakcan)
* [Anton Sergeev](https://github.com/anton-sergeev)
* [Christian Schulze](https://github.com/ChristianSch)
* [Dave Gamble](https://github.com/DaveGamble)
@@ -15,6 +16,7 @@ Contributors
* [IvanVoid](https://github.com/npi3pak)
* [Jonathan Fether](https://github.com/jfether)
* [Kevin Branigan](https://github.com/kbranigan)
* [Kyle Chisholm](https://github.com/ChisholmKyle)
* [Linus Wallgren](https://github.com/ecksun)
* [Max Bruckner](https://github.com/FSMaxB)
* Mike Pontillo
@@ -22,6 +24,7 @@ Contributors
* [Rafael Leal Dias](https://github.com/rafaeldias)
* [Rod Vagg](https://github.com/rvagg)
* [Roland Meertens](https://github.com/rmeertens)
* [Stephan Gatzka](https://github.com/gatzka)
* [Weston Schmidt](https://github.com/schmidtw)
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)

View File

@@ -5,9 +5,12 @@ UTILS_LIBNAME = libcjson_utils
CJSON_TEST = cJSON_test
UTILS_TEST = cJSON_test_utils
CJSON_TEST_SRC = cJSON.c test.c
UTILS_TEST_SRC = cJSON.c cJSON_Utils.c test_utils.c
LDLIBS = -lm
LIBVERSION = 1.0.2
LIBVERSION = 1.1.0
CJSON_SOVERSION = 1
UTILS_SOVERSION = 1
@@ -20,7 +23,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH)
INSTALL ?= cp -a
R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings $(CFLAGS)
R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 $(CFLAGS)
uname := $(shell sh -c 'uname -s 2>/dev/null || echo false')
@@ -62,15 +65,15 @@ test: tests
./$(UTILS_TEST)
.c.o:
$(CC) -ansi -pedantic -c $(R_CFLAGS) $<
$(CC) -c $(R_CFLAGS) $<
#tests
#cJSON
$(CJSON_TEST): cJSON.c cJSON.h test.c
$(CC) $^ -o $@ $(LDLIBS) -I.
$(CJSON_TEST): $(CJSON_TEST_SRC) cJSON.h
$(CC) $(R_CFLAGS) $(CJSON_TEST_SRC) -o $@ $(LDLIBS) -I.
#cJSON_Utils
$(UTILS_TEST): cJSON.c cJSON.h test.c
$(CC) $^ -o $@ $(LDLIBS) -I.
$(UTILS_TEST): $(UTILS_TEST_SRC) cJSON.h cJSON_Utils.h
$(CC) $(R_CFLAGS) $(UTILS_TEST_SRC) -o $@ $(LDLIBS) -I.
#static libraries
#cJSON

35
cJSON.c
View File

@@ -244,6 +244,7 @@ typedef struct
char *buffer;
int length;
int offset;
cjbool noalloc;
} printbuffer;
/* realloc printbuffer if necessary to have at least "needed" bytes more */
@@ -261,6 +262,10 @@ static char* ensure(printbuffer *p, int needed)
return p->buffer + p->offset;
}
if (p->noalloc) {
return NULL;
}
newsize = pow2gt(needed);
newbuffer = (char*)cJSON_malloc(newsize);
if (!newbuffer)
@@ -712,8 +717,9 @@ static char *print_string_ptr(const char *str, printbuffer *p)
ptr = str;
/* calculate additional space that is needed for escaping */
while ((token = *ptr) && ++len)
while ((token = *ptr))
{
++len;
if (strchr("\"\\\b\f\n\r\t", token))
{
len++; /* +1 for the backslash */
@@ -882,10 +888,20 @@ char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt)
}
p.length = prebuffer;
p.offset = 0;
p.noalloc = false;
return print_value(item, 0, fmt, &p);
}
int cJSON_PrintPreallocated(cJSON *item,char *buf, const int len, const cjbool fmt)
{
printbuffer p;
p.buffer = buf;
p.length = len;
p.offset = 0;
p.noalloc = true;
return print_value(item,0,fmt,&p) != NULL;
}
/* Parser core - when encountering text, process appropriately. */
static const char *parse_value(cJSON *item, const char *value, const char **ep)
@@ -1137,7 +1153,10 @@ static char *print_array(const cJSON *item, int depth, cjbool fmt, printbuffer *
child = item->child;
while (child && !fail)
{
print_value(child, depth + 1, fmt, p);
if (!print_value(child, depth + 1, fmt, p))
{
return NULL;
}
p->offset = update(p);
if (child->next)
{
@@ -1434,7 +1453,10 @@ static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer
}
/* print key */
print_string_ptr(child->string, p);
if (!print_string_ptr(child->string, p))
{
return NULL;
}
p->offset = update(p);
len = fmt ? 2 : 1;
@@ -1451,7 +1473,10 @@ static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer
p->offset+=len;
/* print value */
print_value(child, depth, fmt, p);
if (!print_value(child, depth, fmt, p))
{
return NULL;
};
p->offset = update(p);
/* print comma if not last */
@@ -2128,7 +2153,7 @@ cJSON *cJSON_Duplicate(const cJSON *item, cjbool recurse)
}
if (item->string)
{
newitem->string = cJSON_strdup(item->string);
newitem->string = (item->type&cJSON_StringIsConst) ? item->string : cJSON_strdup(item->string);
if (!newitem->string)
{
cJSON_Delete(newitem);

View File

@@ -83,6 +83,8 @@ extern char *cJSON_Print(const cJSON *item);
extern 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 */
extern char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, int fmt);
/* Render a cJSON entity to text using a buffer already allocated in memory with length buf_len. Returns 1 on success and 0 on failure. */
extern int cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const int fmt);
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);
@@ -115,7 +117,10 @@ extern cJSON *cJSON_CreateStringArray(const char **strings, int count);
/* Append item to the specified array/object. */
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
extern void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
* writing to `item->string` */
extern void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);

130
test.c
View File

@@ -22,10 +22,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
/* Parse text to JSON, then render back to text, and print! */
void doit(char *text)
static void doit(char *text)
{
char *out = NULL;
cJSON *json = NULL;
@@ -44,8 +45,9 @@ void doit(char *text)
}
}
#if 0
/* Read a file, parse, render back, etc. */
void dofile(char *filename)
static void dofile(char *filename)
{
FILE *f = NULL;
long len = 0;
@@ -59,6 +61,11 @@ void dofile(char *filename)
fseek(f, 0, SEEK_SET);
data = (char*)malloc(len + 1);
if (data == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
fread(data, 1, len, f);
data[len] = '\0';
@@ -67,6 +74,7 @@ void dofile(char *filename)
doit(data);
free(data);
}
#endif
/* Used by some code below as an example datatype. */
struct record
@@ -81,8 +89,75 @@ struct record
const char *country;
};
/* Create a bunch of objects as demonstration. */
void create_objects(void)
static int print_preallocated(cJSON *root)
{
/* declarations */
char *out = NULL;
char *buf = NULL;
char *buf_fail = NULL;
int len = 0;
int len_fail = 0;
/* formatted print */
out = cJSON_Print(root);
/* create buffer to succeed */
/* the extra 64 bytes are in case a floating point value is printed */
len = strlen(out) + 64;
buf = malloc(len);
if (buf == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
/* create buffer to fail */
len_fail = strlen(out);
buf_fail = malloc(len_fail);
if (buf_fail == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
/* Print to buffer */
if (!cJSON_PrintPreallocated(root, buf, len, 1)) {
printf("cJSON_PrintPreallocated failed!\n");
if (strcmp(out, buf) != 0) {
printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
printf("cJSON_Print result:\n%s\n", out);
printf("cJSON_PrintPreallocated result:\n%s\n", buf);
}
free(out);
free(buf_fail);
free(buf);
return -1;
}
/* success */
printf("%s\n", buf);
/* force it to fail */
if (cJSON_PrintPreallocated(root, buf_fail, len_fail, 1)) {
printf("cJSON_PrintPreallocated failed to show error with insufficient memory!\n");
printf("cJSON_Print result:\n%s\n", out);
printf("cJSON_PrintPreallocated result:\n%s\n", buf_fail);
free(out);
free(buf_fail);
free(buf);
return -1;
}
free(out);
free(buf_fail);
free(buf);
return 0;
}
/* Create a bunch of objects as demonstration. */
static void create_objects(void)
{
/* declare a few. */
cJSON *root = NULL;
@@ -90,7 +165,6 @@ void create_objects(void)
cJSON *img = NULL;
cJSON *thm = NULL;
cJSON *fld = NULL;
char *out = NULL;
int i = 0;
/* Our "days of the week" array: */
@@ -152,21 +226,20 @@ void create_objects(void)
cJSON_AddNumberToObject(fmt, "frame rate", 24);
/* Print to text */
out = cJSON_Print(root);
/* Delete the cJSON */
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
/* print it */
printf("%s\n",out);
/* release the string */
free(out);
/* Our "days of the week" array: */
root = cJSON_CreateStringArray(strings, 7);
out = cJSON_Print(root);
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
printf("%s\n", out);
free(out);
/* Our matrix: */
root = cJSON_CreateArray();
@@ -177,11 +250,11 @@ void create_objects(void)
/* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */
out = cJSON_Print(root);
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
printf("%s\n", out);
free(out);
/* Our "gallery" item: */
root = cJSON_CreateObject();
@@ -195,13 +268,13 @@ void create_objects(void)
cJSON_AddStringToObject(thm, "Width", "100");
cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, 4));
out = cJSON_Print(root);
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
printf("%s\n", out);
free(out);
/* Our array of "records": */
root = cJSON_CreateArray();
for (i = 0; i < 2; i++)
{
@@ -218,17 +291,20 @@ void create_objects(void)
/* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */
out = cJSON_Print(root);
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
printf("%s\n", out);
free(out);
root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "number", 1.0 / zero);
out = cJSON_Print(root);
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
printf("%s\n", out);
free(out);
}
int main(void)

View File

@@ -96,14 +96,14 @@ int main(void)
printf("JSON Apply Patch Tests\n");
for (i = 0; i < 15; i++)
{
cJSON *object = cJSON_Parse(patches[i][0]);
cJSON *object_to_be_patched = cJSON_Parse(patches[i][0]);
cJSON *patch = cJSON_Parse(patches[i][1]);
int err = cJSONUtils_ApplyPatches(object, patch);
char *output = cJSON_Print(object);
int err = cJSONUtils_ApplyPatches(object_to_be_patched, patch);
char *output = cJSON_Print(object_to_be_patched);
printf("Test %d (err %d):\n%s\n\n", i + 1, err, output);
free(output);
cJSON_Delete(object);
cJSON_Delete(object_to_be_patched);
cJSON_Delete(patch);
}
@@ -168,19 +168,19 @@ int main(void)
printf("JSON Merge Patch tests\n");
for (i = 0; i < 15; i++)
{
cJSON *object = cJSON_Parse(merges[i][0]);
cJSON *object_to_be_merged = cJSON_Parse(merges[i][0]);
cJSON *patch = cJSON_Parse(merges[i][1]);
char *before = cJSON_PrintUnformatted(object);
char *before_merge = cJSON_PrintUnformatted(object_to_be_merged);
patchtext = cJSON_PrintUnformatted(patch);
printf("Before: [%s] -> [%s] = ", before, patchtext);
object = cJSONUtils_MergePatch(object, patch);
after = cJSON_PrintUnformatted(object);
printf("Before: [%s] -> [%s] = ", before_merge, patchtext);
object_to_be_merged = cJSONUtils_MergePatch(object_to_be_merged, patch);
after = cJSON_PrintUnformatted(object_to_be_merged);
printf("[%s] vs [%s] (%s)\n", after, merges[i][2], strcmp(after, merges[i][2]) ? "FAIL" : "OK");
free(before);
free(before_merge);
free(patchtext);
free(after);
cJSON_Delete(object);
cJSON_Delete(object_to_be_merged);
cJSON_Delete(patch);
}