[libvirt] [PATCH v2 0/2] Fix some Coverity found issues.

v1: https://www.redhat.com/archives/libvir-list/2018-September/msg01115.html Already pushed patch 1 and 3 from v1, just reworked patch 2 (the lxc change) based on review and repost. John Ferlan (2): lxc: Remove unnecessary error label lxc: Resolve memory leak src/lxc/lxc_native.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) -- 2.17.1

Since lxcConvertSize already creates an error message, there is no need to use an error: label in lxcSetMemTune to just overwrite or essentially rewrite the same error. So remove the label. Signed-off-by: John Ferlan <jferlan@redhat.com> --- src/lxc/lxc_native.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c index e1992fd1f9..cb20f1f5cf 100644 --- a/src/lxc/lxc_native.c +++ b/src/lxc/lxc_native.c @@ -763,7 +763,7 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) "lxc.cgroup.memory.limit_in_bytes", &value) > 0) { if (lxcConvertSize(value, &size) < 0) - goto error; + return -1; size = size / 1024; virDomainDefSetMemoryTotal(def, size); def->mem.hard_limit = virMemoryLimitTruncate(size); @@ -773,7 +773,7 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) "lxc.cgroup.memory.soft_limit_in_bytes", &value) > 0) { if (lxcConvertSize(value, &size) < 0) - goto error; + return -1; def->mem.soft_limit = virMemoryLimitTruncate(size / 1024); } @@ -781,16 +781,10 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) "lxc.cgroup.memory.memsw.limit_in_bytes", &value) > 0) { if (lxcConvertSize(value, &size) < 0) - goto error; + return -1; def->mem.swap_hard_limit = virMemoryLimitTruncate(size / 1024); } return 0; - - error: - virReportError(VIR_ERR_INTERNAL_ERROR, - _("failed to parse integer: '%s'"), value); - return -1; - } static int -- 2.17.1

Commit 40b5c99a modified the virConfGetValue callers to use virConfGetValueString. However, using the virConfGetValueString resulted in leaking the returned @value string in each case. So, let's modify each instance to use the VIR_AUTOFREE(char *) syntax. In some instances changing the variable name since @value was used more than once. Found by Coverity Signed-off-by: John Ferlan <jferlan@redhat.com> --- src/lxc/lxc_native.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c index cb20f1f5cf..cbdec723dd 100644 --- a/src/lxc/lxc_native.c +++ b/src/lxc/lxc_native.c @@ -198,7 +198,7 @@ lxcSetRootfs(virDomainDefPtr def, virConfPtr properties) { int type = VIR_DOMAIN_FS_TYPE_MOUNT; - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; if (virConfGetValueString(properties, "lxc.rootfs", &value) <= 0) return -1; @@ -679,7 +679,7 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virConfPtr properties) static int lxcCreateConsoles(virDomainDefPtr def, virConfPtr properties) { - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; int nbttys = 0; virDomainChrDefPtr console; size_t i; @@ -756,7 +756,7 @@ lxcIdmapWalkCallback(const char *name, virConfValuePtr value, void *data) static int lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) { - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; unsigned long long size = 0; if (virConfGetValueString(properties, @@ -767,6 +767,7 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) size = size / 1024; virDomainDefSetMemoryTotal(def, size); def->mem.hard_limit = virMemoryLimitTruncate(size); + VIR_FREE(value); } if (virConfGetValueString(properties, @@ -775,6 +776,7 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) if (lxcConvertSize(value, &size) < 0) return -1; def->mem.soft_limit = virMemoryLimitTruncate(size / 1024); + VIR_FREE(value); } if (virConfGetValueString(properties, @@ -790,23 +792,25 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties) static int lxcSetCpuTune(virDomainDefPtr def, virConfPtr properties) { - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; if (virConfGetValueString(properties, "lxc.cgroup.cpu.shares", &value) > 0) { if (virStrToLong_ull(value, NULL, 10, &def->cputune.shares) < 0) goto error; def->cputune.sharesSpecified = true; + VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_quota_us", &value) > 0) { if (virStrToLong_ll(value, NULL, 10, &def->cputune.quota) < 0) goto error; + VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_period_us", - &value) > 0) { + &value) > 0) { if (virStrToLong_ull(value, NULL, 10, &def->cputune.period) < 0) goto error; } @@ -822,7 +826,7 @@ lxcSetCpuTune(virDomainDefPtr def, virConfPtr properties) static int lxcSetCpusetTune(virDomainDefPtr def, virConfPtr properties) { - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; virBitmapPtr nodeset = NULL; if (virConfGetValueString(properties, "lxc.cgroup.cpuset.cpus", @@ -830,10 +834,11 @@ lxcSetCpusetTune(virDomainDefPtr def, virConfPtr properties) if (virBitmapParse(value, &def->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0) return -1; def->placement_mode = VIR_DOMAIN_CPU_PLACEMENT_MODE_STATIC; + VIR_FREE(value); } if (virConfGetValueString(properties, "lxc.cgroup.cpuset.mems", - &value) > 0) { + &value) > 0) { if (virBitmapParse(value, &nodeset, VIR_DOMAIN_CPUMASK_LEN) < 0) return -1; if (virDomainNumatuneSet(def->numa, @@ -943,7 +948,7 @@ lxcBlkioDeviceWalkCallback(const char *name, virConfValuePtr value, void *data) static int lxcSetBlkioTune(virDomainDefPtr def, virConfPtr properties) { - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; if (virConfGetValueString(properties, "lxc.cgroup.blkio.weight", &value) > 0) { @@ -963,7 +968,7 @@ lxcSetBlkioTune(virDomainDefPtr def, virConfPtr properties) static void lxcSetCapDrop(virDomainDefPtr def, virConfPtr properties) { - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; char **toDrop = NULL; const char *capString; size_t i; @@ -990,7 +995,7 @@ lxcParseConfigString(const char *config, { virDomainDefPtr vmdef = NULL; virConfPtr properties = NULL; - char *value = NULL; + VIR_AUTOFREE(char *) value = NULL; if (!(properties = virConfReadString(config, VIR_CONF_FLAG_LXC_FORMAT))) return NULL; @@ -1030,6 +1035,7 @@ lxcParseConfigString(const char *config, else if (arch == VIR_ARCH_NONE && STREQ(value, "amd64")) arch = VIR_ARCH_X86_64; vmdef->os.arch = arch; + VIR_FREE(value); } if (VIR_STRDUP(vmdef->os.init, "/sbin/init") < 0) -- 2.17.1

On Mon, Sep 24, 2018 at 08:56:33AM -0400, John Ferlan wrote:
v1: https://www.redhat.com/archives/libvir-list/2018-September/msg01115.html
Already pushed patch 1 and 3 from v1, just reworked patch 2 (the lxc change) based on review and repost.
John Ferlan (2): lxc: Remove unnecessary error label lxc: Resolve memory leak
src/lxc/lxc_native.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
Reviewed-by: Erik Skultety <eskultet@redhat.com>
participants (2)
-
Erik Skultety
-
John Ferlan