From: Luka Perkov Date: Sun, 26 Jan 2014 17:33:36 +0000 (+0000) Subject: lua: allow methods with no arguments X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=4e82a1fabb87b5e3c948a792e16b0fac3702721b;p=project%2Fubus.git lua: allow methods with no arguments In C it is possible to have methods without arguments (UBUS_METHOD_NOARG). Enable support for the same calls in Lua too. This commit also fixes segfault which can be caused by passing non-table type where table is expected. The lua_gettablelen() function is called after we have made sure we are dealing with a table in the first place. Signed-off-by: Luka Perkov --- diff --git a/lua/ubus.c b/lua/ubus.c index 45ea3bb..6da935d 100644 --- a/lua/ubus.c +++ b/lua/ubus.c @@ -349,12 +349,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 +362,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 +396,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;