From 2708a5b39972c605fd84849fa8e4d46b52f7c561 Mon Sep 17 00:00:00 2001 From: dmiller Date: Mon, 29 Aug 2022 17:11:27 +0000 Subject: [PATCH] Avoid realloc leak --- libdnet-stripped/src/intf-win32.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libdnet-stripped/src/intf-win32.c b/libdnet-stripped/src/intf-win32.c index 599d7cf83..157595aa6 100644 --- a/libdnet-stripped/src/intf-win32.c +++ b/libdnet-stripped/src/intf-win32.c @@ -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;