@@ -203,6 +203,12 @@ CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const obje
203203 {
204204 /* reserve enough memory for a 64 bit integer + '/' and '\0' */
205205 unsigned char * full_pointer = (unsigned char * )cJSON_malloc (strlen ((char * )target_pointer ) + 20 + sizeof ("/" ));
206+ if (full_pointer == NULL )
207+ {
208+ /* Return early on allocation failure to prevent crash */
209+ cJSON_free (target_pointer );
210+ return NULL ;
211+ }
206212 /* check if conversion to unsigned long is valid
207213 * This should be eliminated at compile time by dead code elimination
208214 * if size_t is an alias of unsigned long, or if it is bigger */
@@ -220,6 +226,12 @@ CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const obje
220226 if (cJSON_IsObject (object ))
221227 {
222228 unsigned char * full_pointer = (unsigned char * )cJSON_malloc (strlen ((char * )target_pointer ) + pointer_encoded_length ((unsigned char * )current_child -> string ) + 2 );
229+ if (full_pointer == NULL )
230+ {
231+ /* Return early on allocation failure to prevent crash */
232+ cJSON_free (target_pointer );
233+ return NULL ;
234+ }
223235 full_pointer [0 ] = '/' ;
224236 encode_string_as_pointer (full_pointer + 1 , (unsigned char * )current_child -> string );
225237 strcat ((char * )full_pointer , (char * )target_pointer );
@@ -1091,6 +1103,11 @@ static void compose_patch(cJSON * const patches, const unsigned char * const ope
10911103 size_t suffix_length = pointer_encoded_length (suffix );
10921104 size_t path_length = strlen ((const char * )path );
10931105 unsigned char * full_path = (unsigned char * )cJSON_malloc (path_length + suffix_length + sizeof ("/" ));
1106+ if (full_path == NULL )
1107+ {
1108+ /* Return early on allocation failure to prevent crash */
1109+ return ;
1110+ }
10941111
10951112 sprintf ((char * )full_path , "%s/" , (const char * )path );
10961113 encode_string_as_pointer (full_path + path_length + 1 , suffix );
@@ -1146,6 +1163,11 @@ static void create_patches(cJSON * const patches, const unsigned char * const pa
11461163 cJSON * from_child = from -> child ;
11471164 cJSON * to_child = to -> child ;
11481165 unsigned char * new_path = (unsigned char * )cJSON_malloc (strlen ((const char * )path ) + 20 + sizeof ("/" )); /* Allow space for 64bit int. log10(2^64) = 20 */
1166+ if (new_path == NULL )
1167+ {
1168+ /* Return early on allocation failure to prevent crash */
1169+ return ;
1170+ }
11491171
11501172 /* generate patches for all array elements that exist in both "from" and "to" */
11511173 for (index = 0 ; (from_child != NULL ) && (to_child != NULL ); (void )(from_child = from_child -> next ), (void )(to_child = to_child -> next ), index ++ )
@@ -1217,7 +1239,11 @@ static void create_patches(cJSON * const patches, const unsigned char * const pa
12171239 size_t path_length = strlen ((const char * )path );
12181240 size_t from_child_name_length = pointer_encoded_length ((unsigned char * )from_child -> string );
12191241 unsigned char * new_path = (unsigned char * )cJSON_malloc (path_length + from_child_name_length + sizeof ("/" ));
1220-
1242+ if (new_path == NULL )
1243+ {
1244+ /* Return early on allocation failure to prevent crash */
1245+ return ;
1246+ }
12211247 sprintf ((char * )new_path , "%s/" , path );
12221248 encode_string_as_pointer (new_path + path_length + 1 , (unsigned char * )from_child -> string );
12231249
0 commit comments