[client/common] fixes for issues detected through static analysis.

This commit is contained in:
Geoffrey McRae
2022-03-07 10:13:54 +11:00
parent a3820536ab
commit 3a8cb6a613
18 changed files with 228 additions and 24 deletions

View File

@@ -26,6 +26,12 @@
struct ll * ll_new(void)
{
struct ll * list = malloc(sizeof(*list));
if (!list)
{
DEBUG_ERROR("out of memory");
return NULL;
}
list->head = NULL;
list->tail = NULL;
list->count = 0;
@@ -45,6 +51,12 @@ void ll_free(struct ll * list)
void ll_push(struct ll * list, void * data)
{
struct ll_item * item = malloc(sizeof(*item));
if (!item)
{
DEBUG_ERROR("out of memory");
return;
}
item->data = data;
item->next = NULL;

View File

@@ -88,6 +88,12 @@ static char * int_toString(struct Option * opt)
{
int len = snprintf(NULL, 0, "%d", opt->value.x_int);
char * ret = malloc(len + 1);
if (!ret)
{
DEBUG_ERROR("out of memory");
return NULL;
}
sprintf(ret, "%d", opt->value.x_int);
return ret;
}
@@ -101,6 +107,12 @@ static char * float_toString(struct Option * opt)
{
int len = snprintf(NULL, 0, "%f", opt->value.x_float);
char * ret = malloc(len + 1);
if (!ret)
{
DEBUG_ERROR("out of memory");
return NULL;
}
sprintf(ret, "%f", opt->value.x_float);
return ret;
}
@@ -124,10 +136,22 @@ bool option_register(struct Option options[])
sizeof(*state.options) * (state.oCount + new)
);
if (!state.options)
{
DEBUG_ERROR("out of memory");
return false;
}
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
{
state.options[state.oCount + i] = malloc(sizeof(**state.options));
struct Option * o = state.options[state.oCount + i];
struct Option * o =
state.options[state.oCount + i] = malloc(sizeof(**state.options));
if (!o)
{
DEBUG_ERROR("out of memory");
return false;
}
memcpy(o, &options[i], sizeof(*o));
if (!o->parser)
@@ -186,7 +210,14 @@ bool option_register(struct Option options[])
if (o->type == OPTION_TYPE_STRING)
{
if (o->value.x_string)
{
o->value.x_string = strdup(o->value.x_string);
if (!o->value.x_string)
{
DEBUG_ERROR("out of memory");
return false;
}
}
}
// add the option to the correct group for help printout
@@ -202,6 +233,11 @@ bool option_register(struct Option options[])
group->options,
sizeof(*group->options) * (group->count + 1)
);
if (!group->options)
{
DEBUG_ERROR("out of memory");
return false;
}
group->options[group->count] = o;
int len = strlen(o->name);
@@ -218,6 +254,11 @@ bool option_register(struct Option options[])
state.groups,
sizeof(*state.groups) * (state.gCount + 1)
);
if (!state.groups)
{
DEBUG_ERROR("out of memory");
return false;
}
struct OptionGroup * group = &state.groups[state.gCount];
++state.gCount;
@@ -239,7 +280,7 @@ void option_free(void)
for(int i = 0; i < state.oCount; ++i)
{
struct Option * o = state.options[i];
if (o->type == OPTION_TYPE_STRING)
if (o->type == OPTION_TYPE_STRING && o->value.x_string)
free(o->value.x_string);
free(o);
}

View File

@@ -120,7 +120,7 @@ bool ivshmemOpenDev(struct IVSHMEM * dev, const char * shmDevice)
DEBUG_ASSERT(dev);
unsigned int devSize;
int devFd = -1;
int devFd;
bool hasDMA;
dev->opaque = NULL;

View File

@@ -44,6 +44,12 @@ static void * threadWrapper(void * opaque)
bool lgCreateThread(const char * name, LGThreadFunction function, void * opaque, LGThread ** handle)
{
*handle = malloc(sizeof(**handle));
if (!*handle)
{
DEBUG_ERROR("out of memory");
return false;
}
(*handle)->name = name;
(*handle)->function = function;
(*handle)->opaque = opaque;

View File

@@ -42,6 +42,12 @@ RingBuffer ringbuffer_newInternal(int length, size_t valueSize,
DEBUG_ASSERT(valueSize > 0 && valueSize < UINT32_MAX);
struct RingBuffer * rb = calloc(1, sizeof(*rb) + valueSize * length);
if (!rb)
{
DEBUG_ERROR("out of memory");
return NULL;
}
rb->length = length;
rb->valueSize = valueSize;
atomic_store(&rb->readPos , 0);

View File

@@ -19,6 +19,7 @@
*/
#include "common/runningavg.h"
#include "common/debug.h"
#include <stdlib.h>
@@ -33,6 +34,12 @@ struct RunningAvg
RunningAvg runningavg_new(int length)
{
struct RunningAvg * ra = calloc(1, sizeof(*ra) + sizeof(*ra->values) * length);
if (!ra)
{
DEBUG_ERROR("out of memory");
return NULL;
}
ra->length = length;
return ra;
}

View File

@@ -20,6 +20,7 @@
#include "common/stringlist.h"
#include "common/vector.h"
#include "common/debug.h"
#include <stdlib.h>
@@ -32,6 +33,12 @@ struct StringList
StringList stringlist_new(bool owns_strings)
{
StringList sl = malloc(sizeof(*sl));
if (!sl)
{
DEBUG_ERROR("out of memory");
return NULL;
}
sl->owns_strings = owns_strings;
if (!vector_create(&sl->vector, sizeof(char *), 32))