uci: fix memory leak in rpc_uci_replace_savedir()
authorJo-Philipp Wich <jo@mein.io>
Sun, 13 May 2018 19:13:05 +0000 (21:13 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 13 May 2018 19:13:05 +0000 (21:13 +0200)
The rpc_uci_replace_savedir() function did not take into account that libuci
uci_set_savedir() does an additional implicit uci_strdup() of the directory
path string when appending a new delta directory item.

Due to this oversight, only the struct uci_element items got freed, but not
the duplicated path string, leading to leaking memory when invoking the uci
api with session id argument.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
uci.c

diff --git a/uci.c b/uci.c
index 17b4d89eefd84cffc6d057d6ce46b20fed089e4c..a1b83117abaed2823193ab110bfd4d211cf02d38 100644 (file)
--- a/uci.c
+++ b/uci.c
@@ -212,8 +212,12 @@ rpc_uci_replace_savedir(const char *path)
 {
        struct uci_element *e, *tmp;
 
-       uci_foreach_element_safe(&cursor->delta_path, tmp, e)
+       uci_foreach_element_safe(&cursor->delta_path, tmp, e) {
+               if (e->name)
+                       free(e->name);
+
                free(e);
+       }
 
        cursor->delta_path.prev = &cursor->delta_path;
        cursor->delta_path.next = &cursor->delta_path;