nwfilter: Don't have virNWFilterIPAddrMapAddIPAddr consume input

On pure success paths, virNWFilterIPAddrMapAddIPAddr was validly
consuming the input @addr; however, on failure paths it was possible
that virNWFilterVarValueCreateSimple succeed, but virNWFilterHashTablePut
failed resulting in virNWFilterVarValueFree being called to clean
up @val which also cleaned up the input @addr. Thus the caller had
no way to determine on failure whether it too should clean up the
passed parameter.

Instead, let's create a copy of the input @addr, then handle that
properly in the API allowing/forcing the caller to free it's own
copy of the input parameter.
This commit is contained in:
John Ferlan 2017-09-29 15:55:29 -04:00
parent 039702174c
commit a55eaced60
2 changed files with 11 additions and 5 deletions

View File

@ -26,7 +26,9 @@
#include "internal.h" #include "internal.h"
#include "viralloc.h"
#include "virerror.h" #include "virerror.h"
#include "virstring.h"
#include "datatypes.h" #include "datatypes.h"
#include "nwfilter_params.h" #include "nwfilter_params.h"
#include "nwfilter_ipaddrmap.h" #include "nwfilter_ipaddrmap.h"
@ -51,28 +53,35 @@ int
virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr) virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr)
{ {
int ret = -1; int ret = -1;
char *addrCopy;
virNWFilterVarValuePtr val; virNWFilterVarValuePtr val;
if (VIR_STRDUP(addrCopy, addr) < 0)
return -1;
virMutexLock(&ipAddressMapLock); virMutexLock(&ipAddressMapLock);
val = virHashLookup(ipAddressMap->hashTable, ifname); val = virHashLookup(ipAddressMap->hashTable, ifname);
if (!val) { if (!val) {
val = virNWFilterVarValueCreateSimple(addr); val = virNWFilterVarValueCreateSimple(addrCopy);
if (!val) if (!val)
goto cleanup; goto cleanup;
addrCopy = NULL;
ret = virNWFilterHashTablePut(ipAddressMap, ifname, val); ret = virNWFilterHashTablePut(ipAddressMap, ifname, val);
if (ret < 0) if (ret < 0)
virNWFilterVarValueFree(val); virNWFilterVarValueFree(val);
goto cleanup; goto cleanup;
} else { } else {
if (virNWFilterVarValueAddValue(val, addr) < 0) if (virNWFilterVarValueAddValue(val, addrCopy) < 0)
goto cleanup; goto cleanup;
addrCopy = NULL;
} }
ret = 0; ret = 0;
cleanup: cleanup:
virMutexUnlock(&ipAddressMapLock); virMutexUnlock(&ipAddressMapLock);
VIR_FREE(addrCopy);
return ret; return ret;
} }

View File

@ -476,9 +476,6 @@ virNWFilterSnoopIPLeaseInstallRule(virNWFilterSnoopIPLeasePtr ipl,
if (virNWFilterIPAddrMapAddIPAddr(req->ifname, ipaddr) < 0) if (virNWFilterIPAddrMapAddIPAddr(req->ifname, ipaddr) < 0)
goto exit_snooprequnlock; goto exit_snooprequnlock;
/* ipaddr now belongs to the map */
ipaddr = NULL;
if (!instantiate) { if (!instantiate) {
rc = 0; rc = 0;
goto exit_snooprequnlock; goto exit_snooprequnlock;