mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-20 07:59:00 +00:00
virNetDev{Replace,Restore}MacAddress: Fix memory leak
Functions virNetDevRestoreMacAddress() and virNetDevRestoreMacAddress() allocate memory for variable @path using virAsprintf(), but they haven't freed that memory before returning out. Signed-off-by: Zhang bo <oscar.zhangbo@huawei.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
23419a62c2
commit
79bef03a2c
@ -310,11 +310,11 @@ virNetDevReplaceMacAddress(const char *linkdev,
|
|||||||
virMacAddr oldmac;
|
virMacAddr oldmac;
|
||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
char macstr[VIR_MAC_STRING_BUFLEN];
|
char macstr[VIR_MAC_STRING_BUFLEN];
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
if (virNetDevGetMAC(linkdev, &oldmac) < 0)
|
if (virNetDevGetMAC(linkdev, &oldmac) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
|
||||||
if (virAsprintf(&path, "%s/%s",
|
if (virAsprintf(&path, "%s/%s",
|
||||||
stateDir,
|
stateDir,
|
||||||
linkdev) < 0)
|
linkdev) < 0)
|
||||||
@ -323,13 +323,17 @@ virNetDevReplaceMacAddress(const char *linkdev,
|
|||||||
if (virFileWriteStr(path, macstr, O_CREAT|O_TRUNC|O_WRONLY) < 0) {
|
if (virFileWriteStr(path, macstr, O_CREAT|O_TRUNC|O_WRONLY) < 0) {
|
||||||
virReportSystemError(errno, _("Unable to preserve mac for %s"),
|
virReportSystemError(errno, _("Unable to preserve mac for %s"),
|
||||||
linkdev);
|
linkdev);
|
||||||
return -1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virNetDevSetMAC(linkdev, macaddress) < 0)
|
if (virNetDevSetMAC(linkdev, macaddress) < 0)
|
||||||
return -1;
|
goto cleanup;
|
||||||
|
|
||||||
return 0;
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(path);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -344,7 +348,7 @@ int
|
|||||||
virNetDevRestoreMacAddress(const char *linkdev,
|
virNetDevRestoreMacAddress(const char *linkdev,
|
||||||
const char *stateDir)
|
const char *stateDir)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc = -1;
|
||||||
char *oldmacname = NULL;
|
char *oldmacname = NULL;
|
||||||
char *macstr = NULL;
|
char *macstr = NULL;
|
||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
@ -356,21 +360,22 @@ virNetDevRestoreMacAddress(const char *linkdev,
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (virFileReadAll(path, VIR_MAC_STRING_BUFLEN, &macstr) < 0)
|
if (virFileReadAll(path, VIR_MAC_STRING_BUFLEN, &macstr) < 0)
|
||||||
return -1;
|
goto cleanup;
|
||||||
|
|
||||||
if (virMacAddrParse(macstr, &oldmac) != 0) {
|
if (virMacAddrParse(macstr, &oldmac) != 0) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("Cannot parse MAC address from '%s'"),
|
_("Cannot parse MAC address from '%s'"),
|
||||||
oldmacname);
|
oldmacname);
|
||||||
VIR_FREE(macstr);
|
goto cleanup;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*reset mac and remove file-ignore results*/
|
/*reset mac and remove file-ignore results*/
|
||||||
rc = virNetDevSetMAC(linkdev, &oldmac);
|
rc = virNetDevSetMAC(linkdev, &oldmac);
|
||||||
ignore_value(unlink(path));
|
ignore_value(unlink(path));
|
||||||
VIR_FREE(macstr);
|
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(macstr);
|
||||||
|
VIR_FREE(path);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user