iio staging: fix potential memory/resource leaks in find_type_by_name()
authorPeter Meerwald <pmeerw@pmeerw.net>
Mon, 25 Jun 2012 21:13:25 +0000 (23:13 +0200)
committerJonathan Cameron <jic23@kernel.org>
Sat, 30 Jun 2012 09:12:24 +0000 (10:12 +0100)
Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
drivers/staging/iio/Documentation/iio_utils.h

index ab78859..81a6289 100644 (file)
@@ -472,23 +472,30 @@ inline int find_type_by_name(const char *name, const char *type)
                                                + strlen(type)
                                                + numstrlen
                                                + 6);
-                               if (filename == NULL)
+                               if (filename == NULL) {
+                                       closedir(dp);
                                        return -ENOMEM;
+                               }
                                sprintf(filename, "%s%s%d/name",
                                        iio_dir,
                                        type,
                                        number);
                                nameFile = fopen(filename, "r");
-                               if (!nameFile)
+                               if (!nameFile) {
+                                       free(filename);
                                        continue;
+                               }
                                free(filename);
                                fscanf(nameFile, "%s", thisname);
-                               if (strcmp(name, thisname) == 0)
-                                       return number;
                                fclose(nameFile);
+                               if (strcmp(name, thisname) == 0) {
+                                       closedir(dp);
+                                       return number;
+                               }
                        }
                }
        }
+       closedir(dp);
        return -ENODEV;
 }