Skip to content

Commit 6ee34b3

Browse files
authored
cJSON update to 1.7.17 (#1081)
1 parent 8eaa098 commit 6ee34b3

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

source/external/cJSON.c

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020
THE SOFTWARE.
2121
*/
2222

23+
/*
24+
* This file has been modified from its original version by Amazon:
25+
* (1) Remove cJSON_GetErrorPtr and global_error as they are not thread-safe.
26+
* (2) Add NOLINTBEGIN/NOLINTEND so clang-tidy ignores file.
27+
* (3) Replace sprintf() with snprintf() to make compilers happier.
28+
*/
29+
/* NOLINTBEGIN */
30+
2331
/* cJSON */
2432
/* JSON parser in C. */
25-
/* NOLINTBEGIN */
2633

2734
/* disable warnings about old C89 functions in MSVC */
2835
#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
@@ -57,7 +64,7 @@
5764
#pragma GCC visibility pop
5865
#endif
5966

60-
#include <aws/common/external/cJSON.h>
67+
#include "cJSON.h"
6168

6269
/* define our own boolean type */
6370
#ifdef true
@@ -90,6 +97,14 @@ typedef struct {
9097
const unsigned char *json;
9198
size_t position;
9299
} error;
100+
#if 0 /* Amazon edit */
101+
static error global_error = { NULL, 0 };
102+
103+
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
104+
{
105+
return (const char*) (global_error.json + global_error.position);
106+
}
107+
#endif /* Amazon edit */
93108

94109
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item)
95110
{
@@ -112,14 +127,14 @@ CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item)
112127
}
113128

114129
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
115-
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 16)
130+
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 17)
116131
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
117132
#endif
118133

119134
CJSON_PUBLIC(const char*) cJSON_Version(void)
120135
{
121136
static char version[15];
122-
snprintf(version, sizeof(version) / sizeof(char), "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);
137+
snprintf(version, sizeof(version), "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); /* Amazon edit */
123138

124139
return version;
125140
}
@@ -396,7 +411,12 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
396411
{
397412
char *copy = NULL;
398413
/* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
399-
if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference))
414+
if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference))
415+
{
416+
return NULL;
417+
}
418+
/* return NULL if the object is corrupted */
419+
if (object->valuestring == NULL)
400420
{
401421
return NULL;
402422
}
@@ -555,22 +575,22 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
555575
/* This checks for NaN and Infinity */
556576
if (isnan(d) || isinf(d))
557577
{
558-
length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "null");
559-
}
560-
else if (d == (double)item->valueint)
561-
{
562-
length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "%d", item->valueint);
578+
length = snprintf((char*)number_buffer, sizeof(number_buffer), "null"); /* Amazon edit */
563579
}
580+
else if(d == (double)item->valueint)
581+
{
582+
length = snprintf((char*)number_buffer, sizeof(number_buffer), "%d", item->valueint); /* Amazon edit */
583+
}
564584
else
565585
{
566586
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
567-
length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "%1.15g", d);
587+
length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.15g", d); /* Amazon edit */
568588

569589
/* Check whether the original double can be recovered */
570590
if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
571591
{
572592
/* If not, print with 17 decimal places of precision */
573-
length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "%1.17g", d);
593+
length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.17g", d); /* Amazon edit */
574594
}
575595
}
576596

@@ -1003,7 +1023,7 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe
10031023
break;
10041024
default:
10051025
/* escape and print as unicode codepoint */
1006-
snprintf((char*)output_pointer, 6 * sizeof(char), "u%04x", *input_pointer);
1026+
snprintf((char*)output_pointer, 6, "u%04x", *input_pointer); /* Amazon edit */
10071027
output_pointer += 4;
10081028
break;
10091029
}
@@ -1092,6 +1112,12 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer
10921112
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
10931113
cJSON *item = NULL;
10941114

1115+
#if 0 /* Amazon edit */
1116+
/* reset error position */
1117+
global_error.json = NULL;
1118+
global_error.position = 0;
1119+
#endif /* Amazon edit */
1120+
10951121
if (value == NULL || 0 == buffer_length)
10961122
{
10971123
goto fail;
@@ -1155,6 +1181,10 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer
11551181
{
11561182
*return_parse_end = (const char*)local_error.json + local_error.position;
11571183
}
1184+
1185+
#if 0 /* Amazon edit */
1186+
global_error = local_error;
1187+
#endif /* Amazon edit */
11581188
}
11591189

11601190
return NULL;
@@ -1982,14 +2012,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
19822012
}
19832013

19842014
#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
1985-
#pragma GCC diagnostic push
2015+
#pragma GCC diagnostic push
19862016
#endif
19872017
#ifdef __GNUC__
1988-
#if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 1)))
1989-
#pragma GCC diagnostic ignored "-Wcast-qual"
1990-
#endif
2018+
#pragma GCC diagnostic ignored "-Wcast-qual"
19912019
#endif
1992-
19932020
/* helper function to cast away const */
19942021
static void* cast_away_const(const void* string)
19952022
{
@@ -2256,7 +2283,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON
22562283
{
22572284
cJSON *after_inserted = NULL;
22582285

2259-
if (which < 0)
2286+
if (which < 0 || newitem == NULL)
22602287
{
22612288
return false;
22622289
}
@@ -2267,6 +2294,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON
22672294
return add_item_to_array(array, newitem);
22682295
}
22692296

2297+
if (after_inserted != array->child && after_inserted->prev == NULL) {
2298+
/* return false if after_inserted is a corrupted array item */
2299+
return false;
2300+
}
2301+
22702302
newitem->next = after_inserted;
22712303
newitem->prev = after_inserted->prev;
22722304
after_inserted->prev = newitem;
@@ -3109,4 +3141,5 @@ CJSON_PUBLIC(void) cJSON_free(void *object)
31093141
{
31103142
global_hooks.deallocate(object);
31113143
}
3144+
/* Amazon edit */
31123145
/* NOLINTEND */

include/aws/common/external/cJSON.h renamed to source/external/cJSON.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@
2222

2323
/*
2424
* This file has been modified from its original version by Amazon:
25-
* (1) Address clang-tidy errors by renaming function parameters in a number of places
26-
* to match their .c counterparts.
27-
* (2) Misc tweaks to unchecked writes to make security static analysis happier
28-
* (3) Remove cJSON_GetErrorPtr and global_error as they are not thread-safe.
25+
* (1) Remove cJSON_GetErrorPtr and global_error as they are not thread-safe.
26+
* (2) Add NOLINTBEGIN/NOLINTEND so clang-tidy ignores file.
2927
*/
30-
3128
/* NOLINTBEGIN */
3229

33-
#ifndef AWS_COMMON_EXTERNAL_CJSON_H
34-
#define AWS_COMMON_EXTERNAL_CJSON_H
30+
#ifndef cJSON__h
31+
#define cJSON__h
3532

3633
#ifdef __cplusplus
3734
extern "C"
@@ -91,7 +88,7 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ
9188
/* project version */
9289
#define CJSON_VERSION_MAJOR 1
9390
#define CJSON_VERSION_MINOR 7
94-
#define CJSON_VERSION_PATCH 16
91+
#define CJSON_VERSION_PATCH 17
9592

9693
#include <stddef.h>
9794

@@ -182,6 +179,10 @@ CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
182179
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
183180
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
184181
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
182+
#if 0 /* Amazon edit */
183+
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
184+
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
185+
#endif /* Amazon edit */
185186

186187
/* Check item type and return its value */
187188
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
@@ -304,5 +305,6 @@ CJSON_PUBLIC(void) cJSON_free(void *object);
304305
#ifdef __cplusplus
305306
}
306307
#endif
308+
/* Amazon edit */
307309
/* NOLINTEND */
308310
#endif

source/json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <aws/common/json.h>
1010
#include <aws/common/private/json_impl.h>
1111

12-
#include <aws/common/external/cJSON.h>
12+
#include "external/cJSON.h"
1313

1414
static struct aws_allocator *s_aws_json_module_allocator = NULL;
1515
static bool s_aws_json_module_initialized = false;

0 commit comments

Comments
 (0)