ubus: Fix imbalance in lua stack push/pop of values.
[project/ubus.git] / lua / ubus.c
index 45ea3bb..0f2338c 100644 (file)
@@ -289,6 +289,8 @@ ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
        lua_getglobal(state, "__ubus_cb");
        lua_rawgeti(state, -1, o->r);
        lua_getfield(state, -1, method);
+       lua_remove(state, -2);
+       lua_remove(state, -2);
 
        if (lua_isfunction(state, -1)) {
                lua_pushlightuserdata(state, req);
@@ -297,7 +299,9 @@ ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj,
                else
                        ubus_lua_parse_blob_array(state, blob_data(msg), blob_len(msg), true);
                lua_call(state, 2, 0);
-       }
+       } else
+               lua_pop(state, 1);
+
        return 0;
 }
 
@@ -349,12 +353,11 @@ static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
        /* get the policy table */
        lua_pushinteger(L, 2);
        lua_gettable(L, -3);
-       plen = lua_gettablelen(L, -1);
 
        /* check if the method table is valid */
        if ((lua_type(L, -2) != LUA_TFUNCTION) ||
                        (lua_type(L, -1) != LUA_TTABLE) ||
-                       lua_objlen(L, -1) || !plen) {
+                       lua_objlen(L, -1)) {
                lua_pop(L, 2);
                return 1;
        }
@@ -363,6 +366,17 @@ static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
        lua_pushvalue(L, -2);
        lua_setfield(L, -6, lua_tostring(L, -5));
 
+       m->name = lua_tostring(L, -4);
+       m->handler = ubus_method_handler;
+
+       plen = lua_gettablelen(L, -1);
+
+       /* exit if policy table is empty */
+       if (!plen) {
+               lua_pop(L, 2);
+               return 0;
+       }
+
        /* setup the policy pointers */
        p = malloc(sizeof(struct blobmsg_policy) * plen);
        memset(p, 0, sizeof(struct blobmsg_policy) * plen);
@@ -386,8 +400,6 @@ static int ubus_lua_load_methods(lua_State *L, struct ubus_method *m)
        }
 
        m->n_policy = pidx;
-       m->name = lua_tostring(L, -4);
-       m->handler = ubus_method_handler;
        lua_pop(L, 2);
 
        return 0;