diff mbox

[libdrm] tests/exynos: fix memory issues of error path in g2d test

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

Commit Message

Seung-Woo Kim March 16, 2017, 2 a.m. UTC
This patch fixes memory issues including NULL deference and leak
in g2d test in error path.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
---
 tests/exynos/exynos_fimg2d_test.c |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)

Comments

Emil Velikov March 20, 2017, 12:08 a.m. UTC | #1
Hi Seung-Woo Kim,

On 16 March 2017 at 02:00, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
> This patch fixes memory issues including NULL deference and leak
> in g2d test in error path.
>
> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
> ---
>  tests/exynos/exynos_fimg2d_test.c |   13 +++++++------
>  1 files changed, 7 insertions(+), 6 deletions(-)
>
> diff --git a/tests/exynos/exynos_fimg2d_test.c b/tests/exynos/exynos_fimg2d_test.c
> index 797fb6e..2177e08 100644
> --- a/tests/exynos/exynos_fimg2d_test.c
> +++ b/tests/exynos/exynos_fimg2d_test.c
> @@ -59,7 +59,6 @@ static void connector_find_mode(int fd, struct connector *c,
>                 if (!connector) {
>                         fprintf(stderr, "could not get connector %i: %s\n",
>                                 resources->connectors[i], strerror(errno));
> -                       drmModeFreeConnector(connector);
>                         continue;
>                 }
>
> @@ -98,7 +97,6 @@ static void connector_find_mode(int fd, struct connector *c,
>                 if (!c->encoder) {
>                         fprintf(stderr, "could not get encoder %i: %s\n",
>                                 resources->encoders[i], strerror(errno));
> -                       drmModeFreeEncoder(c->encoder);
>                         continue;
>                 }
>
> @@ -264,7 +262,8 @@ static int g2d_copy_test(struct exynos_device *dev, struct exynos_bo *src,
>                 userptr = (unsigned long)malloc(size);
>                 if (!userptr) {
>                         fprintf(stderr, "failed to allocate userptr.\n");
> -                       return -EFAULT;
> +                       ret = -EFAULT;
> +                       goto fail;
Even if you have the best of intentions, but there's yet another bug
in the existing code :-\

Namely: we always return 0 even on error - i.e. the "return 0", after
the g2d_fini() must be "return ret"
This applies to all the tests, afaics.


> @@ -755,7 +756,7 @@ int main(int argc, char **argv)
>
>         dev = exynos_device_create(fd);
>         if (!dev) {
> -               drmClose(dev->fd);
> +               drmClose(fd);
Seems correct, but an alternative (better IMHO) solution is to:
 - flip/fix the call drmClose() <> exynos_device_destroy() order in
err_drm_close.
 - use "fd" in exynos_device_destroy's drmClose.
 - add separate label and use it in the above case.

Can you give this a try, please ?

Thanks
Emil
Seung-Woo Kim March 20, 2017, 12:14 a.m. UTC | #2
Hello Emil,

Thanks for comment.

On 2017년 03월 20일 09:08, Emil Velikov wrote:
> Hi Seung-Woo Kim,
> 
> On 16 March 2017 at 02:00, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
>> This patch fixes memory issues including NULL deference and leak
>> in g2d test in error path.
>>
>> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
>> ---
>>  tests/exynos/exynos_fimg2d_test.c |   13 +++++++------
>>  1 files changed, 7 insertions(+), 6 deletions(-)
>>
>> diff --git a/tests/exynos/exynos_fimg2d_test.c b/tests/exynos/exynos_fimg2d_test.c
>> index 797fb6e..2177e08 100644
>> --- a/tests/exynos/exynos_fimg2d_test.c
>> +++ b/tests/exynos/exynos_fimg2d_test.c
>> @@ -59,7 +59,6 @@ static void connector_find_mode(int fd, struct connector *c,
>>                 if (!connector) {
>>                         fprintf(stderr, "could not get connector %i: %s\n",
>>                                 resources->connectors[i], strerror(errno));
>> -                       drmModeFreeConnector(connector);
>>                         continue;
>>                 }
>>
>> @@ -98,7 +97,6 @@ static void connector_find_mode(int fd, struct connector *c,
>>                 if (!c->encoder) {
>>                         fprintf(stderr, "could not get encoder %i: %s\n",
>>                                 resources->encoders[i], strerror(errno));
>> -                       drmModeFreeEncoder(c->encoder);
>>                         continue;
>>                 }
>>
>> @@ -264,7 +262,8 @@ static int g2d_copy_test(struct exynos_device *dev, struct exynos_bo *src,
>>                 userptr = (unsigned long)malloc(size);
>>                 if (!userptr) {
>>                         fprintf(stderr, "failed to allocate userptr.\n");
>> -                       return -EFAULT;
>> +                       ret = -EFAULT;
>> +                       goto fail;
> Even if you have the best of intentions, but there's yet another bug
> in the existing code :-\
> 
> Namely: we always return 0 even on error - i.e. the "return 0", after
> the g2d_fini() must be "return ret"
> This applies to all the tests, afaics.

You are right. I will fix it.

> 
> 
>> @@ -755,7 +756,7 @@ int main(int argc, char **argv)
>>
>>         dev = exynos_device_create(fd);
>>         if (!dev) {
>> -               drmClose(dev->fd);
>> +               drmClose(fd);
> Seems correct, but an alternative (better IMHO) solution is to:
>  - flip/fix the call drmClose() <> exynos_device_destroy() order in
> err_drm_close.
>  - use "fd" in exynos_device_destroy's drmClose.
>  - add separate label and use it in the above case.

Ok, I will add error label.

> 
> Can you give this a try, please ?

After fixing as your comment, I will send v2.

Regards,
- Seung-Woo Kim

> 
> Thanks
> Emil
> 
> 
>
diff mbox

Patch

diff --git a/tests/exynos/exynos_fimg2d_test.c b/tests/exynos/exynos_fimg2d_test.c
index 797fb6e..2177e08 100644
--- a/tests/exynos/exynos_fimg2d_test.c
+++ b/tests/exynos/exynos_fimg2d_test.c
@@ -59,7 +59,6 @@  static void connector_find_mode(int fd, struct connector *c,
 		if (!connector) {
 			fprintf(stderr, "could not get connector %i: %s\n",
 				resources->connectors[i], strerror(errno));
-			drmModeFreeConnector(connector);
 			continue;
 		}
 
@@ -98,7 +97,6 @@  static void connector_find_mode(int fd, struct connector *c,
 		if (!c->encoder) {
 			fprintf(stderr, "could not get encoder %i: %s\n",
 				resources->encoders[i], strerror(errno));
-			drmModeFreeEncoder(c->encoder);
 			continue;
 		}
 
@@ -264,7 +262,8 @@  static int g2d_copy_test(struct exynos_device *dev, struct exynos_bo *src,
 		userptr = (unsigned long)malloc(size);
 		if (!userptr) {
 			fprintf(stderr, "failed to allocate userptr.\n");
-			return -EFAULT;
+			ret = -EFAULT;
+			goto fail;
 		}
 
 		src_img.user_ptr[0].userptr = userptr;
@@ -469,7 +468,8 @@  static int g2d_copy_with_scale_test(struct exynos_device *dev,
 		userptr = (unsigned long)malloc(size);
 		if (!userptr) {
 			fprintf(stderr, "failed to allocate userptr.\n");
-			return -EFAULT;
+			ret = -EFAULT;
+			goto fail;
 		}
 
 		src_img.user_ptr[0].userptr = userptr;
@@ -557,7 +557,8 @@  static int g2d_blend_test(struct exynos_device *dev,
 		userptr = (unsigned long)malloc(size);
 		if (!userptr) {
 			fprintf(stderr, "failed to allocate userptr.\n");
-			return -EFAULT;
+			ret = -EFAULT;
+			goto fail;
 		}
 
 		src_img.user_ptr[0].userptr = userptr;
@@ -755,7 +756,7 @@  int main(int argc, char **argv)
 
 	dev = exynos_device_create(fd);
 	if (!dev) {
-		drmClose(dev->fd);
+		drmClose(fd);
 		return -EFAULT;
 	}