Re: [PATCH 07/10] qemu: avoid memory leak on vcpupin

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jun 02, 2011 at 05:07:59PM -0600, Eric Blake wrote:
> Detected by Coverity.  This leaked a cpumap on every iteration
> of the loop.  Leak introduced in commit 1cc4d02 (v0.9.0).
> 
> * src/qemu/qemu_process.c (qemuProcessSetVcpuAffinites): Plug
> leak, and hoist allocation outside loop.
> ---
>  src/qemu/qemu_process.c |   21 +++++++++++++--------
>  1 files changed, 13 insertions(+), 8 deletions(-)
> 
> diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
> index 116253e..f175d50 100644
> --- a/src/qemu/qemu_process.c
> +++ b/src/qemu/qemu_process.c
> @@ -1195,6 +1195,8 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
>      pid_t vcpupid;
>      unsigned char *cpumask;
>      int vcpu, cpumaplen, hostcpus, maxcpu;
> +    unsigned char *cpumap = NULL;
> +    int ret = -1;
> 
>      if (virNodeGetInfo(conn, &nodeinfo) != 0) {
>          return  -1;
> @@ -1216,18 +1218,18 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
>      if (maxcpu > hostcpus)
>          maxcpu = hostcpus;
> 
> +    if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
> +        virReportOOMError();
> +        return -1;
> +    }
> +
>      for (vcpu = 0; vcpu < def->cputune.nvcpupin; vcpu++) {
>          if (vcpu != def->cputune.vcpupin[vcpu]->vcpuid)
>              continue;
> 
>          int i;
> -        unsigned char *cpumap = NULL;
> -
> -        if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
> -            virReportOOMError();
> -            return -1;
> -        }
> 
> +        memset(cpumap, 0, cpumaplen);
>          cpumask = (unsigned char *)def->cputune.vcpupin[vcpu]->cpumask;
>          vcpupid = priv->vcpupids[vcpu];
> 
> @@ -1249,11 +1251,14 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
>                                        cpumap,
>                                        cpumaplen,
>                                        maxcpu) < 0) {
> -            return -1;
> +            goto cleanup;
>          }
>      }
> 
> -    return 0;
> +    ret = 0;
> +cleanup:
> +    VIR_FREE(cpumap);
> +    return ret;
>  }

  Whoops !!! ACK, better to allocate out of the loop, fix looks fine,

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel@xxxxxxxxxxxx  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]