[core] Coverity fixes

* [core] Coverity 1518099, 1518097, 1518098 (Unchecked return value from library)

* [core] Coverity 1468551 Unchecked return value

* [core] Coverity 1468293 Unchecked return value

* [core] Coverity 1468274 Explicit null dereferenced

* [core] Coverity 1395588 Unchecked return value

* [core] Coverity 1395515 Logically dead code

* [core] Coverity 1364984 Result is not floating-point

* [core] Coverity 1395554, 1468440 Dereference before null check

* [core] Coverity 1024487 Dereference after null check

* [core] Coverity 1024872 Unchecked return value

* [core] Coverity 1025822 Unchecked return value

* [core] Coverity 1025823 Unchecked return value

* [core] Coverity 1087637, 1346467, 1087638 Unchecked return value

* [core] Coverity 1107607 Unchecked return value

* [core] Coverity 1210777 Unchecked return value

* [core] Coverity 1227670 Dereference before null check

* [core] Coverity 1024551 Logically dead code

* [core] Coverity 1024560 Logically dead code

* [core] Coverity 1024664 Operands don't affect result

* [core] Coverity 1364957 Dereference after null check

* [core] Coverity 1395572 Logically dead code

* [core] Coverity 1412459 Unchecked return value

* [core] Coverity 1412490 Unchecked return value

* [core] Coverity 1395515/2 Logically dead code

* [core] Coverity cleanup
This commit is contained in:
Jakub Karolczyk
2023-09-08 18:17:57 +01:00
committed by GitHub
parent b6ccc27e6f
commit bb9afcb388
14 changed files with 142 additions and 80 deletions
+22 -22
View File
@@ -1104,34 +1104,32 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
buffer->length = default_buffer_size;
buffer->format = format;
buffer->hooks = *hooks;
if (buffer->buffer == NULL)
{
if (buffer->buffer == NULL) {
goto fail;
}
/* print the value */
if (!print_value(item, buffer))
{
if (!print_value(item, buffer)) {
goto fail;
}
update_offset(buffer);
/* check if reallocate is available */
if (hooks->reallocate != NULL)
{
if (hooks->reallocate != NULL) {
printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
if (printed == NULL) {
goto fail;
}
buffer->buffer = NULL;
}
else /* otherwise copy the JSON over to a new buffer */
{
} else { /* otherwise copy the JSON over to a new buffer */
printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
if (printed == NULL)
{
if (printed == NULL) {
goto fail;
}
memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
printed[buffer->offset] = '\0'; /* just to be sure */
@@ -1142,16 +1140,10 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
return printed;
fail:
if (buffer->buffer != NULL)
{
if (buffer->buffer != NULL) {
hooks->deallocate(buffer->buffer);
}
if (printed != NULL)
{
hooks->deallocate(printed);
}
return NULL;
}
@@ -1942,33 +1934,41 @@ static cJSON_bool add_item_to_object(cJSON * const object, const char * const st
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
{
add_item_to_object(object, string, item, &global_hooks, false);
cJSON_bool res = add_item_to_object(object, string, item, &global_hooks, false);
(void)res;
}
/* Add an item to an object with constant string as key */
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
{
add_item_to_object(object, string, item, &global_hooks, true);
cJSON_bool res = add_item_to_object(object, string, item, &global_hooks, true);
(void)res;
}
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
{
cJSON_bool res;
if (array == NULL)
{
return;
}
add_item_to_array(array, create_reference(item, &global_hooks));
res = add_item_to_array(array, create_reference(item, &global_hooks));
(void)res;
}
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
{
cJSON_bool res;
if ((object == NULL) || (string == NULL))
{
return;
}
add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
res = add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
(void)res;
}
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name)