diff options
author | Rob Bradford <rob@linux.intel.com> | 2012-12-05 18:47:09 +0000 |
---|---|---|
committer | Kristian Høgsberg <krh@bitplanet.net> | 2012-12-11 23:47:26 -0500 |
commit | 7d6af71c08e61756f15c2383e44e45734250fb08 (patch) | |
tree | a1fad8ea9d8960d19719b13f88d52785d20b6603 | |
parent | b28d8edb657b37e8a33ef40f261035e27838c9f1 (diff) | |
download | weston-7d6af71c08e61756f15c2383e44e45734250fb08.tar.gz |
backlight: Revamp error handling code to not leak the directory
To neatly free the directory pointer allocated by opendir(), adjust the error
handling paths to go through to the err label.
-rw-r--r-- | src/libbacklight.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/libbacklight.c b/src/libbacklight.c index 2ac135a5..add89b27 100644 --- a/src/libbacklight.c +++ b/src/libbacklight.c @@ -149,11 +149,11 @@ struct backlight *backlight_init(struct udev_device *drm_device, char *pci_name = NULL; char *chosen_path = NULL; char *path = NULL; - DIR *backlights; + DIR *backlights = NULL; struct dirent *entry; enum backlight_type type = 0; char buffer[100]; - struct backlight *backlight; + struct backlight *backlight = NULL; int ret; if (!drm_device) @@ -208,10 +208,10 @@ struct backlight *backlight_init(struct udev_device *drm_device, if (asprintf(&backlight_path, "%s/%s", "/sys/class/backlight", entry->d_name) < 0) - return NULL; + goto err; if (asprintf(&path, "%s/%s", backlight_path, "type") < 0) - return NULL; + goto err; fd = open(path, O_RDONLY); @@ -246,7 +246,7 @@ struct backlight *backlight_init(struct udev_device *drm_device, free (path); if (asprintf(&path, "%s/%s", backlight_path, "device") < 0) - return NULL; + goto err; ret = readlink(path, buffer, sizeof(buffer) - 1); @@ -280,7 +280,7 @@ struct backlight *backlight_init(struct udev_device *drm_device, } if (!chosen_path) - return NULL; + goto err; backlight = malloc(sizeof(struct backlight)); @@ -298,10 +298,11 @@ struct backlight *backlight_init(struct udev_device *drm_device, if (backlight->brightness < 0) goto err; + closedir(backlights); return backlight; err: - if (chosen_path) - free(chosen_path); + closedir(backlights); + free (chosen_path); free (backlight); return NULL; } |