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

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



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 veillard com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/


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