lua: fix memory leak in set method
[project/uci.git] / lua / uci.c
index 323f81a68c17882e33a33455603153d989f18093..ad082996a3c37f60b038f54cd5fab45d5ac22bd6 100644 (file)
--- 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);
@@ -880,16 +882,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;
 }