gpio.c: check fd before attempting to use it
authorJon Trulson <jtrulson@ics.com>
Fri, 20 Feb 2015 13:35:26 +0000 (13:35 +0000)
committerBrendan Le Foll <brendan.le.foll@intel.com>
Fri, 20 Feb 2015 13:35:34 +0000 (13:35 +0000)
mraa_gpio_wait_interrupt needs to check fd before using it

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
src/gpio/gpio.c

index bbcccba..78327d8 100644 (file)
@@ -151,6 +151,10 @@ mraa_gpio_wait_interrupt(int fd)
     unsigned char c;
     struct pollfd pfd;
 
+    if (fd <= 0) {
+        return MRAA_ERROR_INVALID_RESOURCE;
+    }
+
     // setup poll on POLLPRI
     pfd.fd = fd;
     pfd.events = POLLPRI;
@@ -159,10 +163,6 @@ mraa_gpio_wait_interrupt(int fd)
     lseek (fd, 0, SEEK_SET);
     read (fd, &c, 1);
 
-    if (fd <= 0) {
-        return MRAA_ERROR_INVALID_RESOURCE;
-    }
-
     // Wait for it forever or until pthread_cancel
     // poll is a cancelable point like sleep()
     int x = poll (&pfd, 1, -1);