iio.c: Fix leaking filedescriptor on read error
[contrib/mraa.git] / src / iio / iio.c
old mode 100755 (executable)
new mode 100644 (file)
index a9c1ef9..1aee9fe
@@ -111,6 +111,7 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
             fd = open(buf, O_RDONLY);
             if (fd != -1) {
                 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
+                    close(fd);
                     break;
                 }
                 chan_num = ((int) strtol(readbuf, NULL, 10));
@@ -365,6 +366,7 @@ mraa_iio_get_event_data(mraa_iio_context dev)
     int curr_bytes = 0;
     char shortbuf, signchar;
     memset(buf, 0, MAX_SIZE);
+    memset(readbuf, 0, 32);
     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
     dir = opendir(buf);
     if (dir != NULL) {
@@ -544,17 +546,14 @@ mraa_iio_create_trigger(mraa_iio_context dev, const char* trigger)
     struct stat configfs_status;
     struct stat trigger_status;
     char buf[MAX_SIZE];
+    int ret;
 
     if (stat(IIO_CONFIGFS_TRIGGER, &configfs_status) == 0) {
         memset(buf, 0, MAX_SIZE);
         snprintf(buf, MAX_SIZE, IIO_CONFIGFS_TRIGGER "%s", trigger);
-        if (stat(buf, &trigger_status) != 0) {
-            if (mkdir(buf, configfs_status.st_mode) == 0)
-                return MRAA_SUCCESS;
-        } else {
-            // trigger folder already created
-            return MRAA_SUCCESS;
-        }
+    // we actually don't care if this doesn't succeed, as it just means
+    // it's already been initialised
+    mkdir(buf, configfs_status.st_mode);
     }
 
     return MRAA_ERROR_UNSPECIFIED;