Fix memleaks while parsing binaries 00/170500/3 accepted/tizen/unified/20180221.061146 submit/tizen/20180220.123455
authorAlexander Aksenov <a.aksenov@samsung.com>
Tue, 20 Feb 2018 09:14:26 +0000 (12:14 +0300)
committerAlexander Aksenov <a.aksenov@samsung.com>
Tue, 20 Feb 2018 12:14:58 +0000 (15:14 +0300)
Change-Id: I085dfdd460f532636dc19fe0c8947466e67b755e
Signed-off-by: Alexander Aksenov <a.aksenov@samsung.com>
helper/dahelper.c

index e70bf80f2597eef20f69dd7e291779fc2515e01c..e224b9383558f4d42e0d094d0d217cf7e3deda9f 100755 (executable)
@@ -274,6 +274,28 @@ remove_bin_unlock:
        return ret;
 }
 
+static struct ext_bininfo_t *_create_bininfo(const char *path, size_t len)
+{
+       struct ext_bininfo_t *ebi;
+
+       ebi = malloc(sizeof(*ebi));
+       if (!ebi) {
+               PRINTERR("No memory to alloc for ext_bininfo");
+               return NULL;
+       }
+
+       ebi->next = NULL;
+       ebi->path = malloc(len);
+       if (!ebi->path) {
+               PRINTERR("No memory to alloc for ext_bininfo path");
+               free(ebi);
+               return NULL;
+       }
+       memcpy(ebi->path, path, len);
+
+       return ebi;
+}
+
 static void _parse_bundle(char *bundle, size_t size,
                          struct ext_bininfo_t **resp, parse_cb_t cb)
 {
@@ -283,18 +305,16 @@ static void _parse_bundle(char *bundle, size_t size,
        char *path;
        unsigned int i;
        int ret;
-       struct ext_bininfo_t *res, *last, *tmp;
+       struct ext_bininfo_t *last = NULL, *tmp;
+
+       if (resp)
+               *resp = NULL;
 
        if (!cb) {
                PRINTERR("Error! No callback for parsing bundle!");
                return;
        }
 
-       if (resp) {
-               res = *resp;
-               last = *resp;
-       }
-
        if (_decrease_size(&size, sizeof(cnt)))
                return;
 
@@ -321,23 +341,23 @@ static void _parse_bundle(char *bundle, size_t size,
                ptr += len;
 
                ret = cb(path);
-               if (!ret && resp) {
-                       tmp = malloc(sizeof(*tmp));
+               if (ret) {
+                       PRINTERR("Adding failed, error code %d", ret);
+               } else if (resp) {
+                       tmp = _create_bininfo(path, len);
                        if (!tmp) {
-                               PRINTERR("No memory to alloc for ext_bininfo");
+                               free(path);
                                return;
                        }
-                       tmp->next = NULL;
-                       tmp->path = path;
-                       if (res) {
+
+                       if (*resp) {
                                last->next = tmp;
                                last = tmp;
                        } else {
-                               res = last = tmp;
+                               *resp = last = tmp;
                        }
-               } else {
-                       PRINTERR("Adding failed, error code %d", ret);
                }
+               free(path);
        }
 }