diff mbox

[libdrm] xf86drm: remove memory leaks in drmGetBusid/drmGetReservedContextList

Message ID 1490580569-5167-1-git-send-email-sw0312.kim@samsung.com (mailing list archive)
State New, archived
Headers show

Commit Message

Seung-Woo Kim March 27, 2017, 2:09 a.m. UTC
In error path of drmGetBusid() and drmGetReservedContextList(),
there are memory leaks for error path. So this removes them.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
---
 xf86drm.c |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)

Comments

Nicolai Hähnle March 28, 2017, 3:35 p.m. UTC | #1
On 27.03.2017 04:09, Seung-Woo Kim wrote:
> In error path of drmGetBusid() and drmGetReservedContextList(),
> there are memory leaks for error path. So this removes them.
>
> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>

Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>


> ---
>  xf86drm.c |   18 ++++++++++++------
>  1 files changed, 12 insertions(+), 6 deletions(-)
>
> diff --git a/xf86drm.c b/xf86drm.c
> index 88f86ed..685cf69 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -994,8 +994,10 @@ char *drmGetBusid(int fd)
>      if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
>          return NULL;
>      u.unique = drmMalloc(u.unique_len + 1);
> -    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
> +    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
> +        drmFree(u.unique);
>          return NULL;
> +    }
>      u.unique[u.unique_len] = '\0';
>
>      return u.unique;
> @@ -1523,14 +1525,12 @@ drm_context_t *drmGetReservedContextList(int fd, int *count)
>
>      if (!(list   = drmMalloc(res.count * sizeof(*list))))
>          return NULL;
> -    if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
> -        drmFree(list);
> -        return NULL;
> -    }
> +    if (!(retval = drmMalloc(res.count * sizeof(*retval))))
> +        goto err_free_list;
>
>      res.contexts = list;
>      if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
> -        return NULL;
> +        goto err_free_context;
>
>      for (i = 0; i < res.count; i++)
>          retval[i] = list[i].handle;
> @@ -1538,6 +1538,12 @@ drm_context_t *drmGetReservedContextList(int fd, int *count)
>
>      *count = res.count;
>      return retval;
> +
> +err_free_list:
> +    drmFree(list);
> +err_free_context:
> +    drmFree(retval);
> +    return NULL;
>  }
>
>  void drmFreeReservedContextList(drm_context_t *pt)
>
Emil Velikov April 3, 2017, 4:31 p.m. UTC | #2
On 28 March 2017 at 16:35, Nicolai Hähnle <nhaehnle@gmail.com> wrote:
> On 27.03.2017 04:09, Seung-Woo Kim wrote:
>>
>> In error path of drmGetBusid() and drmGetReservedContextList(),
>> there are memory leaks for error path. So this removes them.
>>
>> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
>
>
> Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
>
Thanks gents, I'll push this in a second.

-Emil
diff mbox

Patch

diff --git a/xf86drm.c b/xf86drm.c
index 88f86ed..685cf69 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -994,8 +994,10 @@  char *drmGetBusid(int fd)
     if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
         return NULL;
     u.unique = drmMalloc(u.unique_len + 1);
-    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
+    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
+        drmFree(u.unique);
         return NULL;
+    }
     u.unique[u.unique_len] = '\0';
 
     return u.unique;
@@ -1523,14 +1525,12 @@  drm_context_t *drmGetReservedContextList(int fd, int *count)
 
     if (!(list   = drmMalloc(res.count * sizeof(*list))))
         return NULL;
-    if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
-        drmFree(list);
-        return NULL;
-    }
+    if (!(retval = drmMalloc(res.count * sizeof(*retval))))
+        goto err_free_list;
 
     res.contexts = list;
     if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
-        return NULL;
+        goto err_free_context;
 
     for (i = 0; i < res.count; i++)
         retval[i] = list[i].handle;
@@ -1538,6 +1538,12 @@  drm_context_t *drmGetReservedContextList(int fd, int *count)
 
     *count = res.count;
     return retval;
+
+err_free_list:
+    drmFree(list);
+err_free_context:
+    drmFree(retval);
+    return NULL;
 }
 
 void drmFreeReservedContextList(drm_context_t *pt)