20
20
THE SOFTWARE.
21
21
*/
22
22
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
+
23
31
/* cJSON */
24
32
/* JSON parser in C. */
25
- /* NOLINTBEGIN */
26
33
27
34
/* disable warnings about old C89 functions in MSVC */
28
35
#if !defined(_CRT_SECURE_NO_DEPRECATE ) && defined(_MSC_VER )
57
64
#pragma GCC visibility pop
58
65
#endif
59
66
60
- #include <aws/common/external/ cJSON.h>
67
+ #include " cJSON.h"
61
68
62
69
/* define our own boolean type */
63
70
#ifdef true
@@ -90,6 +97,14 @@ typedef struct {
90
97
const unsigned char * json ;
91
98
size_t position ;
92
99
} 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 */
93
108
94
109
CJSON_PUBLIC (char * ) cJSON_GetStringValue (const cJSON * const item )
95
110
{
@@ -112,14 +127,14 @@ CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item)
112
127
}
113
128
114
129
/* 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 )
116
131
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
117
132
#endif
118
133
119
134
CJSON_PUBLIC (const char * ) cJSON_Version (void )
120
135
{
121
136
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 */
123
138
124
139
return version ;
125
140
}
@@ -396,7 +411,12 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
396
411
{
397
412
char * copy = NULL ;
398
413
/* 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 )
400
420
{
401
421
return NULL ;
402
422
}
@@ -555,22 +575,22 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
555
575
/* This checks for NaN and Infinity */
556
576
if (isnan (d ) || isinf (d ))
557
577
{
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 */
563
579
}
580
+ else if (d == (double )item -> valueint )
581
+ {
582
+ length = snprintf ((char * )number_buffer , sizeof (number_buffer ), "%d" , item -> valueint ); /* Amazon edit */
583
+ }
564
584
else
565
585
{
566
586
/* 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 */
568
588
569
589
/* Check whether the original double can be recovered */
570
590
if ((sscanf ((char * )number_buffer , "%lg" , & test ) != 1 ) || !compare_double ((double )test , d ))
571
591
{
572
592
/* 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 */
574
594
}
575
595
}
576
596
@@ -1003,7 +1023,7 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe
1003
1023
break ;
1004
1024
default :
1005
1025
/* 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 */
1007
1027
output_pointer += 4 ;
1008
1028
break ;
1009
1029
}
@@ -1092,6 +1112,12 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer
1092
1112
parse_buffer buffer = { 0 , 0 , 0 , 0 , { 0 , 0 , 0 } };
1093
1113
cJSON * item = NULL ;
1094
1114
1115
+ #if 0 /* Amazon edit */
1116
+ /* reset error position */
1117
+ global_error .json = NULL ;
1118
+ global_error .position = 0 ;
1119
+ #endif /* Amazon edit */
1120
+
1095
1121
if (value == NULL || 0 == buffer_length )
1096
1122
{
1097
1123
goto fail ;
@@ -1155,6 +1181,10 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer
1155
1181
{
1156
1182
* return_parse_end = (const char * )local_error .json + local_error .position ;
1157
1183
}
1184
+
1185
+ #if 0 /* Amazon edit */
1186
+ global_error = local_error ;
1187
+ #endif /* Amazon edit */
1158
1188
}
1159
1189
1160
1190
return NULL ;
@@ -1982,14 +2012,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
1982
2012
}
1983
2013
1984
2014
#if defined(__clang__ ) || (defined(__GNUC__ ) && ((__GNUC__ > 4 ) || ((__GNUC__ == 4 ) && (__GNUC_MINOR__ > 5 ))))
1985
- #pragma GCC diagnostic push
2015
+ #pragma GCC diagnostic push
1986
2016
#endif
1987
2017
#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"
1991
2019
#endif
1992
-
1993
2020
/* helper function to cast away const */
1994
2021
static void * cast_away_const (const void * string )
1995
2022
{
@@ -2256,7 +2283,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON
2256
2283
{
2257
2284
cJSON * after_inserted = NULL ;
2258
2285
2259
- if (which < 0 )
2286
+ if (which < 0 || newitem == NULL )
2260
2287
{
2261
2288
return false;
2262
2289
}
@@ -2267,6 +2294,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON
2267
2294
return add_item_to_array (array , newitem );
2268
2295
}
2269
2296
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
+
2270
2302
newitem -> next = after_inserted ;
2271
2303
newitem -> prev = after_inserted -> prev ;
2272
2304
after_inserted -> prev = newitem ;
@@ -3109,4 +3141,5 @@ CJSON_PUBLIC(void) cJSON_free(void *object)
3109
3141
{
3110
3142
global_hooks .deallocate (object );
3111
3143
}
3144
+ /* Amazon edit */
3112
3145
/* NOLINTEND */
0 commit comments