From 39093f3b040df895da812e2932b55feeeb1201df Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0tetiar?= Date: Mon, 4 Nov 2019 23:08:26 +0100 Subject: [PATCH] lua: fix memory leak in set method MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit scan-build from clang version 9 has reported following issue: uci.c:624:12: warning: Potential leak of memory pointed to by 's' return luaL_error(L, "Cannot set an uci option to an empty table value"); ^~~~~~~~~~ valgrind confirmed it on the supplied test case: ==31013== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==31013== by 0x56C49B9: strdup (strdup.c:42) Signed-off-by: Petr Å tetiar --- lua/uci.c | 4 +++- .../lua/test_cases/set_with_empty_table_doesnt_leak.lua | 2 ++ tests/cram/test_ucilua_testcases.t | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/cram/lua/test_cases/set_with_empty_table_doesnt_leak.lua diff --git a/lua/uci.c b/lua/uci.c index 42fe4b7..ad08299 100644 --- a/lua/uci.c +++ b/lua/uci.c @@ -620,8 +620,10 @@ uci_lua_set(lua_State *L) case 4: /* Format: uci.set("p", "s", "o", "v") */ if (lua_istable(L, nargs)) { - if (lua_rawlen(L, nargs) < 1) + if (lua_rawlen(L, nargs) < 1) { + free(s); return luaL_error(L, "Cannot set an uci option to an empty table value"); + } lua_rawgeti(L, nargs, 1); ptr.value = luaL_checkstring(L, -1); lua_pop(L, 1); diff --git a/tests/cram/lua/test_cases/set_with_empty_table_doesnt_leak.lua b/tests/cram/lua/test_cases/set_with_empty_table_doesnt_leak.lua new file mode 100644 index 0000000..83e91d3 --- /dev/null +++ b/tests/cram/lua/test_cases/set_with_empty_table_doesnt_leak.lua @@ -0,0 +1,2 @@ +local c = uci.cursor(os.getenv("CONFIG_DIR")) +print(pcall(c.set, c, "network", "lan", "dns", {})) diff --git a/tests/cram/test_ucilua_testcases.t b/tests/cram/test_ucilua_testcases.t index 279dfce..6a0216b 100644 --- a/tests/cram/test_ucilua_testcases.t +++ b/tests/cram/test_ucilua_testcases.t @@ -8,3 +8,10 @@ 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 + +check that set method with empty table value doesn't leak memory: + + $ cp -R "$TESTDIR/config" . + $ export CONFIG_DIR=$(pwd)/config + $ ucilua $TESTDIR/lua/test_cases/set_with_empty_table_doesnt_leak.lua + false\tCannot set an uci option to an empty table value (esc) -- 2.25.1