[all] general: fix possible memory leaks with realloc usage

This commit is contained in:
Geoffrey McRae 2025-03-09 02:56:20 +11:00
parent 5382a94945
commit a421329d9a
3 changed files with 20 additions and 10 deletions

View File

@ -131,16 +131,16 @@ bool option_register(struct Option options[])
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
++new;
state.options = realloc(
void * tmp = realloc(
state.options,
sizeof(*state.options) * (state.oCount + new)
);
if (!state.options)
if (!tmp)
{
DEBUG_ERROR("out of memory");
return false;
}
state.options = tmp;
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
{
@ -229,15 +229,16 @@ bool option_register(struct Option options[])
continue;
found = true;
group->options = realloc(
void * tmp = realloc(
group->options,
sizeof(*group->options) * (group->count + 1)
);
if (!group->options)
if (!tmp)
{
DEBUG_ERROR("out of memory");
return false;
}
group->options = tmp;
group->options[group->count] = o;
int len = strlen(o->name);
@ -250,15 +251,16 @@ bool option_register(struct Option options[])
if (!found)
{
state.groups = realloc(
void * new = realloc(
state.groups,
sizeof(*state.groups) * (state.gCount + 1)
);
if (!state.groups)
if (!new)
{
DEBUG_ERROR("out of memory");
return false;
}
state.groups = new;
struct OptionGroup * group = &state.groups[state.gCount];
++state.gCount;

View File

@ -164,7 +164,14 @@ static int dl_iterate_phdr_callback(struct dl_phdr_info * info, size_t size, voi
ttl += hdr.p_memsz;
}
crash.ranges = realloc(crash.ranges, sizeof(*crash.ranges) * (crash.rangeCount + 1));
void * tmp = realloc(crash.ranges,
sizeof(*crash.ranges) * (crash.rangeCount + 1));
if (!tmp)
{
DEBUG_ERROR("out of memory");
return 1;
}
crash.ranges = tmp;
crash.ranges[crash.rangeCount].start = info->dlpi_addr;
crash.ranges[crash.rangeCount].end = info->dlpi_addr + ttl;
++crash.rangeCount;

View File

@ -626,12 +626,13 @@ static bool appendData(KVMFRUserData * dst, const void * src, const size_t size)
if (size > dst->size - dst->used)
{
size_t newSize = dst->size + max(1024, size);
dst->data = realloc(dst->data, newSize);
if (!dst->data)
void * tmp = realloc(dst->data, newSize);
if (!tmp)
{
DEBUG_ERROR("Out of memory");
return false;
}
dst->data = tmp;
memset(dst->data + dst->size, 0, newSize - dst->size);
dst->size = newSize;