Compare commits

...

19 Commits

Author SHA1 Message Date
Max Bruckner
3c8935676a Merge pull request #355 from DaveGamble/release-1.7.12
Release 1.7.12
2019-05-17 01:05:59 +02:00
Max Bruckner
b93fd34044 Update changelog and contributors 2019-05-17 00:37:26 +02:00
Max Bruckner
687b1a2fe1 Update version to 1.7.12 2019-05-16 20:03:12 +02:00
Max Bruckner
08d2bc766a Fix infinite loop in cJSON_Minify 2019-05-16 20:01:02 +02:00
Max Bruckner
465352fb99 Merge pull request #352 from tan-wei/master
Add link dependency to fix tests link error when ENABLE_CJSON_UTILS is ON
2019-05-11 12:06:26 +02:00
Winterreise
19ff92da79 Add link dependency to fix tests link error when ENABLE_CJSON_UTILS is ON 2019-05-11 07:37:33 +08:00
Max Bruckner
62bc2e3432 Merge pull request #347 from raiden00pl/master
Undef true and false first also for cJSON_Utils.c
2019-04-28 13:31:46 +02:00
raiden00pl
359567fdde Undef true and false first also for cJSON_Utils.c 2019-04-28 10:33:44 +02:00
Max Bruckner
0b5a7abf48 Merge pull request #340 from ldm0/master
Typo Fix
2019-04-15 23:06:11 +02:00
Max Bruckner
93688cbe72 Merge pull request #344 from DaveGamble/release-1.7.11
Release 1.7.11
2019-04-15 00:17:19 +02:00
Max Bruckner
6b249213dd Fix clang -Wcomma warning 2019-04-15 00:06:35 +02:00
Max Bruckner
09ebae8149 Release cJSON 1.7.11 2019-04-14 23:58:02 +02:00
Max Bruckner
a43fa56a63 Rewrite cJSON_Minify, fixing buffer overflows, fixes #338
Also first tests for cJSON_Minify.
Thanks @bigric3 for reporting
2019-04-14 23:18:48 +02:00
Max Bruckner
5a52eaddfd Undef true and false first, fixes #339
Thanks  @raiden00pl for reporting
2019-04-13 01:44:23 +02:00
Donough Liu
add86a6be8 Update cJSON.c 2019-03-18 22:56:34 +08:00
Max Bruckner
c69134d017 Release Version 1.7.10 2018-12-20 18:15:05 +01:00
Max Bruckner
563d861f92 cJSON_Utils: sort_lists: Properly split the lists
Since `prev` is not used anymore after that by the algorithm it should
have been fine anyways, still splitting it correctly in the first place
is probably a good idea.
Thanks @andysCaplin for the fix!
2018-12-20 18:12:23 +01:00
Max Bruckner
6820448db5 libcjson.pc.in: Use Libs.private instead of Private
Thanks @shiluotang
2018-12-20 18:04:52 +01:00
Max Bruckner
d44b594ab3 Add missing changelog entry 2018-12-16 11:21:35 +01:00
10 changed files with 307 additions and 57 deletions

View File

@@ -1,9 +1,33 @@
1.7.12
======
Fixes:
------
* Fix infinite loop in `cJSON_Minify` (potential Denial of Service), thanks @Alanscut for reporting. See #354
* Fix link error for Visual Studio. Thanks @tan-wei, see #352
* Undefine `true` and `false` for `cJSON_Utils` before redefining them. Thanks @raiden00pl, see #347
1.7.11
======
Fixes:
------
* Fix a bug where cJSON_Minify could overflow it's buffer, both reading and writing. This is a security issue. (see #338). Big thanks @bigric3 for reporting.
* Unset `true` and `false` macros before setting them if they exist. See #339, thanks @raiden00pl for reporting
1.7.10
======
Fixes:
------
* Fix package config file for `libcjson`. Thanks @shiluotang for reporting (#321)
* Correctly split lists in `cJSON_Utils`'s merge sort. Thanks @andysCaplin for the fix (#322)
1.7.9 1.7.9
===== =====
Fixes: Fixes:
------ ------
* Fix a bug where `cJSON_GetObjectItemCaseSensitive` would pass a nullpointer to `strcmp` when called on an array (#315). Thanks @yuweol for reporting. * Fix a bug where `cJSON_GetObjectItemCaseSensitive` would pass a nullpointer to `strcmp` when called on an array (#315). Thanks @yuweol for reporting.
* Fix error in `cJSON_Utils` where the case sensitivity was not respected (#317). Thanks @yuta-oxo for fixing. * Fix error in `cJSON_Utils` where the case sensitivity was not respected (#317). Thanks @yuta-oxo for fixing.
* Fix some warnings detected by the Visual Studio Static Analyzer (#307). Thanks @bnason-nf
1.7.8 1.7.8
===== =====

View File

@@ -7,7 +7,7 @@ include(GNUInstallDirs)
set(PROJECT_VERSION_MAJOR 1) set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 7) set(PROJECT_VERSION_MINOR 7)
set(PROJECT_VERSION_PATCH 9) set(PROJECT_VERSION_PATCH 12)
set(CJSON_VERSION_SO 1) set(CJSON_VERSION_SO 1)
set(CJSON_UTILS_VERSION_SO 1) set(CJSON_UTILS_VERSION_SO 1)
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")

View File

@@ -14,6 +14,7 @@ Current Maintainer: [Max Bruckner](https://github.com/FSMaxB)
* [Debora Grosse](https://github.com/DeboraG) * [Debora Grosse](https://github.com/DeboraG)
* [dieyushi](https://github.com/dieyushi) * [dieyushi](https://github.com/dieyushi)
* [Dōngwén Huáng (黄东文)](https://github.com/DongwenHuang) * [Dōngwén Huáng (黄东文)](https://github.com/DongwenHuang)
* [Donough Liu](https://github.com/ldm0)
* Eswar Yaganti * Eswar Yaganti
* [Evan Todd](https://github.com/etodd) * [Evan Todd](https://github.com/etodd)
* [Fabrice Fontaine](https://github.com/ffontaine) * [Fabrice Fontaine](https://github.com/ffontaine)
@@ -34,12 +35,14 @@ Current Maintainer: [Max Bruckner](https://github.com/FSMaxB)
* [Pawel Winogrodzki](https://github.com/PawelWMS) * [Pawel Winogrodzki](https://github.com/PawelWMS)
* [prefetchnta](https://github.com/prefetchnta) * [prefetchnta](https://github.com/prefetchnta)
* [Rafael Leal Dias](https://github.com/rafaeldias) * [Rafael Leal Dias](https://github.com/rafaeldias)
* [raiden00pl](https://github.com/raiden00pl)
* [Robin Mallinson](https://github.com/rmallins) * [Robin Mallinson](https://github.com/rmallins)
* [Rod Vagg](https://github.com/rvagg) * [Rod Vagg](https://github.com/rvagg)
* [Roland Meertens](https://github.com/rmeertens) * [Roland Meertens](https://github.com/rmeertens)
* [Romain Porte](https://github.com/MicroJoe) * [Romain Porte](https://github.com/MicroJoe)
* [Simon Ricaldone](https://github.com/simon-p-r) * [Simon Ricaldone](https://github.com/simon-p-r)
* [Stephan Gatzka](https://github.com/gatzka) * [Stephan Gatzka](https://github.com/gatzka)
* [tan-wei](https://github.com/tan-wei)
* [Weston Schmidt](https://github.com/schmidtw) * [Weston Schmidt](https://github.com/schmidtw)
* [yangfl](https://github.com/yangfl) * [yangfl](https://github.com/yangfl)
* [yuta-oxo](https://github.com/yuta-oxo) * [yuta-oxo](https://github.com/yuta-oxo)

View File

@@ -8,7 +8,7 @@ CJSON_TEST_SRC = cJSON.c test.c
LDLIBS = -lm LDLIBS = -lm
LIBVERSION = 1.7.9 LIBVERSION = 1.7.12
CJSON_SOVERSION = 1 CJSON_SOVERSION = 1
UTILS_SOVERSION = 1 UTILS_SOVERSION = 1

143
cJSON.c
View File

@@ -58,7 +58,14 @@
#include "cJSON.h" #include "cJSON.h"
/* define our own boolean type */ /* define our own boolean type */
#ifdef true
#undef true
#endif
#define true ((cJSON_bool)1) #define true ((cJSON_bool)1)
#ifdef false
#undef false
#endif
#define false ((cJSON_bool)0) #define false ((cJSON_bool)0)
typedef struct { typedef struct {
@@ -81,7 +88,7 @@ CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item) {
} }
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 9) #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 12)
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
#endif #endif
@@ -144,6 +151,9 @@ static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
#define internal_realloc realloc #define internal_realloc realloc
#endif #endif
/* strlen of character literals resolved at compile time */
#define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc };
static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
@@ -504,7 +514,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
} }
} }
/* sprintf failed or buffer overrun occured */ /* sprintf failed or buffer overrun occurred */
if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
{ {
return false; return false;
@@ -1555,7 +1565,7 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu
buffer_skip_whitespace(input_buffer); buffer_skip_whitespace(input_buffer);
if (!parse_string(current_item, input_buffer)) if (!parse_string(current_item, input_buffer))
{ {
goto fail; /* faile to parse name */ goto fail; /* failed to parse name */
} }
buffer_skip_whitespace(input_buffer); buffer_skip_whitespace(input_buffer);
@@ -2630,69 +2640,96 @@ fail:
return NULL; return NULL;
} }
static void skip_oneline_comment(char **input)
{
*input += static_strlen("//");
for (; (*input)[0] != '\0'; ++(*input))
{
if ((*input)[0] == '\n') {
*input += static_strlen("\n");
return;
}
}
}
static void skip_multiline_comment(char **input)
{
*input += static_strlen("/*");
for (; (*input)[0] != '\0'; ++(*input))
{
if (((*input)[0] == '*') && ((*input)[1] == '/'))
{
*input += static_strlen("*/");
return;
}
}
}
static void minify_string(char **input, char **output) {
(*output)[0] = (*input)[0];
*input += static_strlen("\"");
*output += static_strlen("\"");
for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) {
(*output)[0] = (*input)[0];
if ((*input)[0] == '\"') {
(*output)[0] = '\"';
*input += static_strlen("\"");
*output += static_strlen("\"");
return;
} else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) {
(*output)[1] = (*input)[1];
*input += static_strlen("\"");
*output += static_strlen("\"");
}
}
}
CJSON_PUBLIC(void) cJSON_Minify(char *json) CJSON_PUBLIC(void) cJSON_Minify(char *json)
{ {
unsigned char *into = (unsigned char*)json; char *into = json;
if (json == NULL) if (json == NULL)
{ {
return; return;
} }
while (*json) while (json[0] != '\0')
{ {
if (*json == ' ') switch (json[0])
{ {
json++; case ' ':
} case '\t':
else if (*json == '\t') case '\r':
{ case '\n':
/* Whitespace characters. */
json++;
}
else if (*json == '\r')
{
json++;
}
else if (*json=='\n')
{
json++;
}
else if ((*json == '/') && (json[1] == '/'))
{
/* double-slash comments, to end of line. */
while (*json && (*json != '\n'))
{
json++; json++;
} break;
}
else if ((*json == '/') && (json[1] == '*')) case '/':
{ if (json[1] == '/')
/* multiline comments. */
while (*json && !((*json == '*') && (json[1] == '/')))
{
json++;
}
json += 2;
}
else if (*json == '\"')
{
/* string literals, which are \" sensitive. */
*into++ = (unsigned char)*json++;
while (*json && (*json != '\"'))
{
if (*json == '\\')
{ {
*into++ = (unsigned char)*json++; skip_oneline_comment(&json);
} }
*into++ = (unsigned char)*json++; else if (json[1] == '*')
} {
*into++ = (unsigned char)*json++; skip_multiline_comment(&json);
} } else {
else json++;
{ }
/* All other characters. */ break;
*into++ = (unsigned char)*json++;
case '\"':
minify_string(&json, (char**)&into);
break;
default:
into[0] = json[0];
json++;
into++;
} }
} }

View File

@@ -81,7 +81,7 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ
/* project version */ /* project version */
#define CJSON_VERSION_MAJOR 1 #define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 7 #define CJSON_VERSION_MINOR 7
#define CJSON_VERSION_PATCH 9 #define CJSON_VERSION_PATCH 12
#include <stddef.h> #include <stddef.h>

View File

@@ -50,7 +50,14 @@
#include "cJSON_Utils.h" #include "cJSON_Utils.h"
/* define our own boolean type */ /* define our own boolean type */
#ifdef true
#undef true
#endif
#define true ((cJSON_bool)1) #define true ((cJSON_bool)1)
#ifdef false
#undef false
#endif
#define false ((cJSON_bool)0) #define false ((cJSON_bool)0)
static unsigned char* cJSONUtils_strdup(const unsigned char* const string) static unsigned char* cJSONUtils_strdup(const unsigned char* const string)
@@ -499,6 +506,7 @@ static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive)
{ {
/* Split the lists */ /* Split the lists */
second->prev->next = NULL; second->prev->next = NULL;
second->prev = NULL;
} }
/* Recursively sort the sub-lists. */ /* Recursively sort the sub-lists. */

View File

@@ -6,5 +6,5 @@ Version: @PROJECT_VERSION@
Description: Ultralightweight JSON parser in ANSI C Description: Ultralightweight JSON parser in ANSI C
URL: https://github.com/DaveGamble/cJSON URL: https://github.com/DaveGamble/cJSON
Libs: -L${libdir} -lcjson Libs: -L${libdir} -lcjson
Libs.Private: -lm Libs.private: -lm
Cflags: -I${includedir} Cflags: -I${includedir}

View File

@@ -57,6 +57,7 @@ if(ENABLE_CJSON_TEST)
compare_tests compare_tests
cjson_add cjson_add
readme_examples readme_examples
minify_tests
) )
option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.") option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.")
@@ -101,6 +102,9 @@ if(ENABLE_CJSON_TEST)
foreach (cjson_utils_test ${cjson_utils_tests}) foreach (cjson_utils_test ${cjson_utils_tests})
add_executable("${cjson_utils_test}" "${cjson_utils_test}.c") add_executable("${cjson_utils_test}" "${cjson_utils_test}.c")
target_link_libraries("${cjson_utils_test}" "${CJSON_LIB}" "${CJSON_UTILS_LIB}" unity) target_link_libraries("${cjson_utils_test}" "${CJSON_LIB}" "${CJSON_UTILS_LIB}" unity)
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
target_sources(${cjson_utils_test} PRIVATE unity_setup.c)
endif()
if(MEMORYCHECK_COMMAND) if(MEMORYCHECK_COMMAND)
add_test(NAME "${cjson_utils_test}" add_test(NAME "${cjson_utils_test}"
COMMAND "${MEMORYCHECK_COMMAND}" ${MEMORYCHECK_COMMAND_OPTIONS} "${CMAKE_CURRENT_BINARY_DIR}/${cjson_utils_test}") COMMAND "${MEMORYCHECK_COMMAND}" ${MEMORYCHECK_COMMAND_OPTIONS} "${CMAKE_CURRENT_BINARY_DIR}/${cjson_utils_test}")

174
tests/minify_tests.c Normal file
View File

@@ -0,0 +1,174 @@
/*
Copyright (c) 2009-2019 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 cjson_minify_should_not_overflow_buffer(void)
{
char unclosed_multiline_comment[] = "/* bla";
char pending_escape[] = "\"\\";
cJSON_Minify(unclosed_multiline_comment);
TEST_ASSERT_EQUAL_STRING("", unclosed_multiline_comment);
cJSON_Minify(pending_escape);
TEST_ASSERT_EQUAL_STRING("\"\\", pending_escape);
}
static void cjson_minify_should_remove_single_line_comments(void)
{
const char to_minify[] = "{// this is {} \"some kind\" of [] comment /*, don't you see\n}";
char* minified = (char*) malloc(sizeof(to_minify));
TEST_ASSERT_NOT_NULL(minified);
strcpy(minified, to_minify);
cJSON_Minify(minified);
TEST_ASSERT_EQUAL_STRING("{}", minified);
free(minified);
}
static void cjson_minify_should_remove_spaces(void)
{
const char to_minify[] = "{ \"key\":\ttrue\r\n }";
char* minified = (char*) malloc(sizeof(to_minify));
TEST_ASSERT_NOT_NULL(minified);
strcpy(minified, to_minify);
cJSON_Minify(minified);
TEST_ASSERT_EQUAL_STRING("{\"key\":true}", minified);
free(minified);
}
static void cjson_minify_should_remove_multiline_comments(void)
{
const char to_minify[] = "{/* this is\n a /* multi\n //line \n {comment \"\\\" */}";
char* minified = (char*) malloc(sizeof(to_minify));
TEST_ASSERT_NOT_NULL(minified);
strcpy(minified, to_minify);
cJSON_Minify(minified);
TEST_ASSERT_EQUAL_STRING("{}", minified);
free(minified);
}
static void cjson_minify_should_not_modify_strings(void)
{
const char to_minify[] = "\"this is a string \\\" \\t bla\"";
char* minified = (char*) malloc(sizeof(to_minify));
TEST_ASSERT_NOT_NULL(minified);
strcpy(minified, to_minify);
cJSON_Minify(minified);
TEST_ASSERT_EQUAL_STRING(to_minify, minified);
free(minified);
}
static void cjson_minify_should_minify_json(void) {
const char to_minify[] =
"{\n"
" \"glossary\": { // comment\n"
" \"title\": \"example glossary\",\n"
" /* multi\n"
" line */\n"
" \"GlossDiv\": {\n"
" \"title\": \"S\",\n"
" \"GlossList\": {\n"
" \"GlossEntry\": {\n"
" \"ID\": \"SGML\",\n"
" \"SortAs\": \"SGML\",\n"
" \"Acronym\": \"SGML\",\n"
" \"Abbrev\": \"ISO 8879:1986\",\n"
" \"GlossDef\": {\n"
" \"GlossSeeAlso\": [\"GML\", \"XML\"]\n"
" },\n"
" \"GlossSee\": \"markup\"\n"
" }\n"
" }\n"
" }\n"
" }\n"
"}";
const char* minified =
"{"
"\"glossary\":{"
"\"title\":\"example glossary\","
"\"GlossDiv\":{"
"\"title\":\"S\","
"\"GlossList\":{"
"\"GlossEntry\":{"
"\"ID\":\"SGML\","
"\"SortAs\":\"SGML\","
"\"Acronym\":\"SGML\","
"\"Abbrev\":\"ISO 8879:1986\","
"\"GlossDef\":{"
"\"GlossSeeAlso\":[\"GML\",\"XML\"]"
"},"
"\"GlossSee\":\"markup\""
"}"
"}"
"}"
"}"
"}";
char *buffer = (char*) malloc(sizeof(to_minify));
strcpy(buffer, to_minify);
cJSON_Minify(buffer);
TEST_ASSERT_EQUAL_STRING(minified, buffer);
free(buffer);
}
static void cjson_minify_should_not_loop_infinitely(void) {
char string[] = { '8', ' ', '/', ' ', '5', '\n', '\0' };
/* this should not be an infinite loop */
cJSON_Minify(string);
}
int CJSON_CDECL main(void)
{
UNITY_BEGIN();
RUN_TEST(cjson_minify_should_not_overflow_buffer);
RUN_TEST(cjson_minify_should_minify_json);
RUN_TEST(cjson_minify_should_remove_single_line_comments);
RUN_TEST(cjson_minify_should_remove_multiline_comments);
RUN_TEST(cjson_minify_should_remove_spaces);
RUN_TEST(cjson_minify_should_not_modify_strings);
RUN_TEST(cjson_minify_should_not_loop_infinitely);
return UNITY_END();
}