Skip to content

Commit 2708a5b

Browse files
committed
Avoid realloc leak
1 parent 8d5b6f3 commit 2708a5b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

libdnet-stripped/src/intf-win32.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,23 @@ _ifcombo_type(const char *device)
9292
static void
9393
_ifcombo_add(struct ifcombo *ifc, DWORD ipv4_idx, DWORD ipv6_idx)
9494
{
95+
void* pmem = NULL;
9596
if (ifc->cnt == ifc->max) {
9697
if (ifc->idx) {
9798
ifc->max *= 2;
98-
ifc->idx = realloc(ifc->idx,
99+
pmem = realloc(ifc->idx,
99100
sizeof(ifc->idx[0]) * ifc->max);
100101
} else {
101102
ifc->max = 8;
102-
ifc->idx = malloc(sizeof(ifc->idx[0]) * ifc->max);
103+
pmem = malloc(sizeof(ifc->idx[0]) * ifc->max);
103104
}
105+
if (!pmem) {
106+
/* malloc or realloc failed. Restore state.
107+
* TODO: notify caller. */
108+
ifc->max = ifc->cnt;
109+
return;
110+
}
111+
ifc->idx = pmem;
104112
}
105113
ifc->idx[ifc->cnt].ipv4 = ipv4_idx;
106114
ifc->idx[ifc->cnt].ipv6 = ipv6_idx;

0 commit comments

Comments
 (0)