Skip to content

Commit

Permalink
Set free'd pointers to NULL whenever they are not reassigned immediat…
Browse files Browse the repository at this point in the history
…ely after
  • Loading branch information
maebex authored and Alanscut committed May 9, 2024
1 parent 5437b79 commit 1ac905d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,12 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
{
global_hooks.deallocate(item->valuestring);
item->valuestring = NULL;
}
if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
{
global_hooks.deallocate(item->string);
item->string = NULL;
}
global_hooks.deallocate(item);
item = next;
Expand Down Expand Up @@ -820,6 +822,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
if (output != NULL)
{
input_buffer->hooks.deallocate(output);
output = NULL;
}

if (input_pointer != NULL)
Expand Down Expand Up @@ -1165,6 +1168,7 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i

/* free the buffer */
hooks->deallocate(buffer->buffer);
buffer->buffer = NULL;
}

return printed;
Expand All @@ -1173,11 +1177,13 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
if (buffer->buffer != NULL)
{
hooks->deallocate(buffer->buffer);
buffer->buffer = NULL;
}

if (printed != NULL)
{
hooks->deallocate(printed);
printed = NULL;
}

return NULL;
Expand Down Expand Up @@ -1218,6 +1224,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
if (!print_value(item, &p))
{
global_hooks.deallocate(p.buffer);
p.buffer = NULL;
return NULL;
}

Expand Down Expand Up @@ -2987,4 +2994,5 @@ CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
CJSON_PUBLIC(void) cJSON_free(void *object)
{
global_hooks.deallocate(object);
object = NULL;
}

0 comments on commit 1ac905d

Please sign in to comment.