[PATCH xf86-input-joystick 1/3] Use jstkCloseDevice_* on error in jstkOpenDevice_* backend functions

Hans de Goede hdegoede at redhat.com
Thu Mar 13 06:49:51 PDT 2014


This is a preparation patch for adding support for server managed fds, this
also fixes a missing free() in an error handling path in the evdev back-end.

Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 src/backend_bsd.c      | 18 +++++++-----------
 src/backend_evdev.c    | 27 +++++++++++----------------
 src/backend_joystick.c | 12 ++++--------
 3 files changed, 22 insertions(+), 35 deletions(-)

diff --git a/src/backend_bsd.c b/src/backend_bsd.c
index c6ce7b4..9fa3035 100644
--- a/src/backend_bsd.c
+++ b/src/backend_bsd.c
@@ -100,29 +100,27 @@ jstkOpenDevice_bsd(JoystickDevPtr joystick, Bool probe)
     if ((rd = hid_get_report_desc(joystick->fd)) == 0) {
         xf86Msg(X_ERROR, "Joystick: hid_get_report_desc failed: %s\n",
                 strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_bsd(joystick);
         return -1;
     }
 
     if (ioctl(joystick->fd, USB_GET_REPORT_ID, &report_id) < 0) {
         xf86Msg(X_ERROR, "Joystick: ioctl USB_GET_REPORT_ID failed: %s\n",
                 strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_bsd(joystick);
         return -1;
     }
 
     bsddata = (struct jstk_bsd_hid_data*)
               malloc(sizeof(struct jstk_bsd_hid_data));
+    joystick->devicedata = (void*) bsddata;
+
     bsddata->dlen = hid_report_size(rd, hid_input, report_id);
 
     if ((bsddata->data_buf = malloc(bsddata->dlen)) == NULL) {
         fprintf(stderr, "error: couldn't malloc %d bytes\n", bsddata->dlen);
         hid_dispose_report_desc(rd);
-        free(bsddata);
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_bsd(joystick);
         return -1;
     }
 
@@ -181,14 +179,11 @@ jstkOpenDevice_bsd(JoystickDevPtr joystick, Bool probe)
     if (!got_something) {
         free(bsddata->data_buf);
         xf86Msg(X_ERROR, "Joystick: Didn't find any usable axes.\n");
-        free(bsddata);
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_bsd(joystick);
         return -1;
     }
 
     bsddata->hotdata = 0;
-    joystick->devicedata = (void*) bsddata;
     if (probe == TRUE) {
         xf86Msg(X_INFO, "Joystick: %d buttons, %d axes\n", 
                 joystick->num_buttons, joystick->num_axes);
@@ -222,6 +217,7 @@ jstkCloseDevice_bsd(JoystickDevPtr joystick)
         if (((struct jstk_bsd_hid_data*)joystick->devicedata)->data_buf)
             free(((struct jstk_bsd_hid_data*)joystick->devicedata)->data_buf);
         free(joystick->devicedata);
+        joystick->devicedata = NULL;
     }
 }
 
diff --git a/src/backend_evdev.c b/src/backend_evdev.c
index ca0b2e0..6d6f644 100644
--- a/src/backend_evdev.c
+++ b/src/backend_evdev.c
@@ -99,16 +99,14 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe)
     if (ioctl(joystick->fd, EVIOCGVERSION, &driver_version) == -1) {
         xf86Msg(X_ERROR, "Joystick: ioctl EVIOCGVERSION on '%s' failed: %s\n", 
                 joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_evdev(joystick);
         return -1;
     }
 
     if (ioctl(joystick->fd, EVIOCGID, &id) == -1) {
         xf86Msg(X_ERROR, "Joystick: ioctl EVIOCGID on '%s' failed: %s\n",
                 joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_evdev(joystick);
         return -1;
     }
 
@@ -118,13 +116,14 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe)
     {
         xf86Msg(X_ERROR, "Joystick: ioctl EVIOCGBIT on '%s' failed: %s\n",
                 joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_evdev(joystick);
         return -1;
     }
 
     evdevdata = (struct jstk_evdev_data*)
                 malloc(sizeof(struct jstk_evdev_data));
+    joystick->devicedata = (void*) evdevdata;
+
     for (axes=0; axes<ABS_MAX; axes++)
     {
         evdevdata->axis[axes].number = -1;
@@ -144,9 +143,7 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe)
             if (ioctl(joystick->fd, EVIOCGABS(j), &absinfo) == -1) {
                 xf86Msg(X_ERROR, "Joystick: ioctl EVIOCGABS on '%s' failed: %s\n",
                         joystick->device, strerror(errno));
-                close(joystick->fd);
-                joystick->fd = -1;
-                free(evdevdata);
+                jstkCloseDevice_evdev(joystick);
                 return -1;
             }
             evdevdata->axis[j].number = axes; /* physical -> logical mapping */
@@ -163,8 +160,7 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe)
     {
         xf86Msg(X_ERROR, "Joystick: ioctl EVIOCGBIT on '%s' failed: %s\n",
                 joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_evdev(joystick);
         return -1;
     }
     buttons = 0; /* Our logical index */
@@ -191,7 +187,6 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe)
     joystick->open_proc = jstkOpenDevice_evdev;
     joystick->read_proc = jstkReadData_evdev;
     joystick->close_proc = jstkCloseDevice_evdev;
-    joystick->devicedata = (void*) evdevdata;
 
     if (buttons > MAXBUTTONS)
         buttons = MAXBUTTONS;
@@ -217,12 +212,12 @@ jstkCloseDevice_evdev(JoystickDevPtr joystick)
 {
     if ((joystick->fd >= 0)) {
         xf86CloseSerial(joystick->fd);
-        if (joystick->devicedata) {
-            free(joystick->devicedata);
-            joystick->devicedata = NULL;
-        }
         joystick->fd = -1;
     }
+    if (joystick->devicedata) {
+        free(joystick->devicedata);
+        joystick->devicedata = NULL;
+    }
 }
 
 
diff --git a/src/backend_joystick.c b/src/backend_joystick.c
index e61d908..c84654e 100644
--- a/src/backend_joystick.c
+++ b/src/backend_joystick.c
@@ -82,8 +82,7 @@ jstkOpenDevice_joystick(JoystickDevPtr joystick, Bool probe)
     if (ioctl(joystick->fd, JSIOCGVERSION, &driver_version) == -1) {
         xf86Msg(X_ERROR, "Joystick: ioctl JSIOCGVERSION on '%s' failed: %s\n", 
                 joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_joystick(joystick);
         return -1;
     }
     if ((driver_version >> 16) < 1) {
@@ -96,24 +95,21 @@ jstkOpenDevice_joystick(JoystickDevPtr joystick, Bool probe)
     if (ioctl(joystick->fd, JSIOCGAXES, &axes) == -1) {
         xf86Msg(X_ERROR, "Joystick: ioctl JSIOCGAXES on '%s' failed: %s\n", 
                 joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_joystick(joystick);
         return -1;
     }
 
     if (ioctl(joystick->fd, JSIOCGBUTTONS, &buttons) == -1) {
         xf86Msg(X_ERROR, "Joystick: ioctl JSIOCGBUTTONS on '%s' failed: %s\n", 
                 joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_joystick(joystick);
         return -1;
     }
 
     if (ioctl(joystick->fd, JSIOCGNAME(128), joy_name) == -1) {
         xf86Msg(X_ERROR, "Joystick: ioctl JSIOCGNAME on '%s' failed: %s\n", 
                   joystick->device, strerror(errno));
-        close(joystick->fd);
-        joystick->fd = -1;
+        jstkCloseDevice_joystick(joystick);
         return -1;
     }
 
-- 
1.9.0



More information about the xorg-devel mailing list