Avoid realloc leak

This commit is contained in:
dmiller 2022-08-29 17:11:27 +00:00
parent 8d5b6f3388
commit 2708a5b399

View file

@ -92,15 +92,23 @@ _ifcombo_type(const char *device)
static void
_ifcombo_add(struct ifcombo *ifc, DWORD ipv4_idx, DWORD ipv6_idx)
{
void* pmem = NULL;
if (ifc->cnt == ifc->max) {
if (ifc->idx) {
ifc->max *= 2;
ifc->idx = realloc(ifc->idx,
pmem = realloc(ifc->idx,
sizeof(ifc->idx[0]) * ifc->max);
} else {
ifc->max = 8;
ifc->idx = malloc(sizeof(ifc->idx[0]) * ifc->max);
pmem = malloc(sizeof(ifc->idx[0]) * ifc->max);
}
if (!pmem) {
/* malloc or realloc failed. Restore state.
* TODO: notify caller. */
ifc->max = ifc->cnt;
return;
}
ifc->idx = pmem;
}
ifc->idx[ifc->cnt].ipv4 = ipv4_idx;
ifc->idx[ifc->cnt].ipv6 = ipv6_idx;