summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2019-11-04 22:41:18 +0100
committerPetr Štetiar <ynezz@true.cz>2019-11-14 17:11:34 +0100
commit19ceff323f1e2e7df26031a9fae29fff2edc65bd (patch)
tree49fff1f82a6f0a1d770c14a216668f67da9b2cd4
parent18049a84fe402068ba10dfcffe6dbb088aefc53a (diff)
downloaduci-19ceff323f1e2e7df26031a9fae29fff2edc65bd.tar.gz
lua: fix memory leak in changes method
Configs returned by uci_list_configs call are not freed when not needed, leading to the memory leak. While at it make the code cleaner. Signed-off-by: Petr Štetiar <ynezz@true.cz>
-rw-r--r--lua/uci.c15
-rw-r--r--tests/cram/lua/test_cases/changes_doesnt_leak.lua11
-rw-r--r--tests/cram/test_ucilua_testcases.t10
3 files changed, 29 insertions, 7 deletions
diff --git a/lua/uci.c b/lua/uci.c
index 323f81a..42fe4b7 100644
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -880,16 +880,17 @@ uci_lua_changes(lua_State *L)
lua_newtable(L);
if (package) {
uci_lua_changes_pkg(L, ctx, package);
- } else {
- if (uci_list_configs(ctx, &config) != 0)
- goto done;
+ return 1;
+ }
- for(i = 0; config[i] != NULL; i++) {
- uci_lua_changes_pkg(L, ctx, config[i]);
- }
+ if ((uci_list_configs(ctx, &config) != UCI_OK) || !config)
+ return 1;
+
+ for (i = 0; config[i] != NULL; i++) {
+ uci_lua_changes_pkg(L, ctx, config[i]);
}
-done:
+ free(config);
return 1;
}
diff --git a/tests/cram/lua/test_cases/changes_doesnt_leak.lua b/tests/cram/lua/test_cases/changes_doesnt_leak.lua
new file mode 100644
index 0000000..68f0ed0
--- /dev/null
+++ b/tests/cram/lua/test_cases/changes_doesnt_leak.lua
@@ -0,0 +1,11 @@
+local A = assert
+local c = uci.cursor(os.getenv("CONFIG_DIR"))
+
+A(c:set("network", "lan", "dns", {
+ "ns1.king.banik.cz",
+ "ns2.openwrt.org",
+}))
+
+local changes = c:changes()
+A(changes.network.lan.dns[1] == "ns1.king.banik.cz")
+A(changes.network.lan.dns[2] == "ns2.openwrt.org")
diff --git a/tests/cram/test_ucilua_testcases.t b/tests/cram/test_ucilua_testcases.t
new file mode 100644
index 0000000..279dfce
--- /dev/null
+++ b/tests/cram/test_ucilua_testcases.t
@@ -0,0 +1,10 @@
+set LUA_CPATH and ucilua for convenience:
+
+ $ [ -n "$UCI_LUA" ] && export LUA_CPATH="$(dirname "$UCI_LUA")/?.so"
+ $ alias ucilua="valgrind --quiet --leak-check=full lua -luci"
+
+check that changes method doesnt leak memory:
+
+ $ cp -R "$TESTDIR/config" .
+ $ export CONFIG_DIR=$(pwd)/config
+ $ ucilua $TESTDIR/lua/test_cases/changes_doesnt_leak.lua