Skip to contentSkip to conversation

xf86drm: Fix error handling for drmGetDevices()

PATCH In review Matt Roper 2 participants 4 messages Sep 30, 2015 – Oct 2, 2015

lore
Share & tools
lore

Matt Roper

author
#0 lore raw

PATCH 1/1 xf86drm: Fix error handling for drmGetDevices()

If the opendir() call in drmGetDevices() returns failure, we jump to an error label that calls closedir() and then returns. However this means that we're calling closedir(NULL) which may not be safe on all implementations. We are also leaking the local_devices array that was allocated before the opendir() call.

Fix both of these issues by jumping to an earlier error label (to free local_devices) and guarding the closedir() call with a NULL test.

---
 xf86drm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
xf86drm.c +4 -3
index c1cab1b..763d710 100644
@@ -3209,7 +3209,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
     sysdir = opendir(DRM_DIR_NAME);
     if (!sysdir) {
         ret = -errno;
-        goto close_sysdir;
+        goto free_locals;
     }
 
     i = 0;
@@ -3280,9 +3280,10 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
 
 free_devices:
     drmFreeDevices(local_devices, i);
+free_locals:
     free(local_devices);
 
-close_sysdir:
-    closedir(sysdir);
+    if (sysdir)
+	    closedir(sysdir);
     return ret;
 }
Signature · 6 lines
2.1.4

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

Hi Matt,

Quoted 29 lines · Matt Roper

If the opendir() call in drmGetDevices() returns failure, we jump to an error label that calls closedir() and then returns. However this means that we're calling closedir(NULL) which may not be safe on all implementations. We are also leaking the local_devices array that was allocated before the opendir() call.

Fix both of these issues by jumping to an earlier error label (to free local_devices) and guarding the closedir() call with a NULL test.

---
 xf86drm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
xf86drm.c +1 -1
index c1cab1b..763d710 100644
@@ -3209,7 +3209,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
     sysdir = opendir(DRM_DIR_NAME);
     if (!sysdir) {
         ret = -errno;
-        goto close_sysdir;
+        goto free_locals;
     }
 
     i = 0;

xf86drm.c int drmGetDevices(drmDevicePtr devices[], int max_devices) · line 3287

Quoted 10 lines
xf86drm.c +3 -2
@@ -3280,9 +3280,10 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
 
 free_devices:
     drmFreeDevices(local_devices, i);
+free_locals:
     free(local_devices);
 
-close_sysdir:
-    closedir(sysdir);
+    if (sysdir)
+           closedir(sysdir);

Any objections if we move the new label & free() here and drop the if check above? I can do that before pushing if that's ok with you.

Thanks for catching this.
Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

author
#2 lore raw
Quoted 45 lines · Emil Velikov

Hi Matt,

Quoted 29 lines · Matt Roper

If the opendir() call in drmGetDevices() returns failure, we jump to an error label that calls closedir() and then returns. However this means that we're calling closedir(NULL) which may not be safe on all implementations. We are also leaking the local_devices array that was allocated before the opendir() call.

Fix both of these issues by jumping to an earlier error label (to free local_devices) and guarding the closedir() call with a NULL test.

---
 xf86drm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
xf86drm.c +1 -1
index c1cab1b..763d710 100644
@@ -3209,7 +3209,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
     sysdir = opendir(DRM_DIR_NAME);
     if (!sysdir) {
         ret = -errno;
-        goto close_sysdir;
+        goto free_locals;
     }
 
     i = 0;

xf86drm.c int drmGetDevices(drmDevicePtr devices[], int max_devices) · line 3287

Quoted 10 lines
xf86drm.c +3 -2
@@ -3280,9 +3280,10 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
 
 free_devices:
     drmFreeDevices(local_devices, i);
+free_locals:
     free(local_devices);
 
-close_sysdir:
-    closedir(sysdir);
+    if (sysdir)
+           closedir(sysdir);

Any objections if we move the new label & free() here and drop the if check above? I can do that before pushing if that's ok with you.

Quoted 2 lines

Thanks for catching this.
Emil

Sure, that sounds fine too. Thanks!

Matt

Signature · 9 lines
Matt Roper
Graphics Software Engineer
IoTG Platform Enabling & Development
Intel Corporation
(916) 356-2795
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw
Quoted 46 lines · Matt Roper
Quoted 45 lines · Emil Velikov

Hi Matt,

Quoted 29 lines · Matt Roper

If the opendir() call in drmGetDevices() returns failure, we jump to an error label that calls closedir() and then returns. However this means that we're calling closedir(NULL) which may not be safe on all implementations. We are also leaking the local_devices array that was allocated before the opendir() call.

Fix both of these issues by jumping to an earlier error label (to free local_devices) and guarding the closedir() call with a NULL test.

---
 xf86drm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
xf86drm.c +1 -1
index c1cab1b..763d710 100644
@@ -3209,7 +3209,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
     sysdir = opendir(DRM_DIR_NAME);
     if (!sysdir) {
         ret = -errno;
-        goto close_sysdir;
+        goto free_locals;
     }
 
     i = 0;

xf86drm.c int drmGetDevices(drmDevicePtr devices[], int max_devices) · line 3287

Quoted 10 lines
xf86drm.c +3 -2
@@ -3280,9 +3280,10 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
 
 free_devices:
     drmFreeDevices(local_devices, i);
+free_locals:
     free(local_devices);
 
-close_sysdir:
-    closedir(sysdir);
+    if (sysdir)
+           closedir(sysdir);

Any objections if we move the new label & free() here and drop the if check above? I can do that before pushing if that's ok with you.

Quoted 5 lines
Quoted 2 lines

Thanks for catching this.
Emil

Sure, that sounds fine too. Thanks!

Ack. Amended and pushed to master.

-Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel