summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Opasiak <k.opasiak@samsung.com>2014-06-10 17:20:38 +0200
committerKrzysztof Opasiak <k.opasiak@samsung.com>2014-07-05 10:27:50 +0200
commitc7e3da994f919ca44282d06cd644374d06f19ff0 (patch)
tree93a6d6bc4a6d497395c55aeaeabc583f67a78c63
parent471edae7c196d905a88f81830821510fd5b84ffa (diff)
downloadlibusbg-c7e3da994f919ca44282d06cd644374d06f19ff0.tar.gz
libusbg-c7e3da994f919ca44282d06cd644374d06f19ff0.tar.bz2
libusbg-c7e3da994f919ca44282d06cd644374d06f19ff0.zip
libusbg: Fix potential memory leak in usbg_init()
Memory allocated with asprintf() for variable path could be not free() in some cases. Fix this issue by doing some small refactoring. Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r--src/usbg.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/src/usbg.c b/src/usbg.c
index fe525e3..fe7e239 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -1218,6 +1218,7 @@ int usbg_init(char *configfs_path, usbg_state **state)
int ret = USBG_SUCCESS;
DIR *dir;
char *path;
+ usbg_state *s;
ret = asprintf(&path, "%s/usb_gadget", configfs_path);
if (ret < 0)
@@ -1227,21 +1228,33 @@ int usbg_init(char *configfs_path, usbg_state **state)
/* Check if directory exist */
dir = opendir(path);
- if (dir) {
- closedir(dir);
- *state = malloc(sizeof(usbg_state));
- ret = *state ? usbg_init_state(path, *state)
- : USBG_ERROR_NO_MEM;
- if (*state && ret != USBG_SUCCESS) {
- ERRORNO("couldn't init gadget state\n");
- usbg_free_state(*state);
- }
- } else {
+ if (!dir) {
ERRORNO("couldn't init gadget state\n");
ret = usbg_translate_error(errno);
- free(path);
+ goto err;
+ }
+
+ closedir(dir);
+ s = malloc(sizeof(usbg_state));
+ if (!s) {
+ ret = USBG_ERROR_NO_MEM;
+ goto err;
}
+ ret = usbg_init_state(path, s);
+ if (ret != USBG_SUCCESS) {
+ ERRORNO("couldn't init gadget state\n");
+ usbg_free_state(s);
+ goto out;
+ }
+
+ *state = s;
+
+ return ret;
+
+err:
+ free(path);
+out:
return ret;
}