Fix stack overflow bug of uloop lua binding.
[project/libubox.git] / lua / uloop.c
index 0f0c878..5922e04 100644 (file)
@@ -22,8 +22,8 @@
 #include <lualib.h>
 #include <lauxlib.h>
 
-#include <libubox/uloop.h>
-#include <libubox/list.h>
+#include "../uloop.h"
+#include "../list.h"
 
 struct lua_uloop_timeout {
        struct uloop_timeout t;
@@ -43,6 +43,7 @@ static void ul_timer_cb(struct uloop_timeout *t)
 
        lua_getglobal(state, "__uloop_cb");
        lua_rawgeti(state, -1, tout->r);
+       lua_remove(state, -2);
        lua_call(state, 0, 0);
 }
 
@@ -126,13 +127,14 @@ static int ul_timer(lua_State *L)
        return 1;
 }
 
-static void proc_cb(struct uloop_process *p, int ret)
+static void ul_process_cb(struct uloop_process *p, int ret)
 {
        struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
 
        lua_getglobal(state, "__uloop_cb");
        lua_rawgeti(state, -1, proc->r);
        luaL_unref(state, -2, proc->r);
+       lua_remove(state, -2);
        lua_pushinteger(state, ret >> 8);
        lua_call(state, 1, 0);
 }
@@ -196,7 +198,7 @@ static int ul_process(lua_State *L)
 
        proc->r = ref;
        proc->p.pid = pid;
-       proc->p.cb = proc_cb;
+       proc->p.cb = ul_process_cb;
        uloop_process_add(&proc->p);
 
        return 1;
@@ -226,6 +228,10 @@ static luaL_reg uloop_func[] = {
        {NULL, NULL},
 };
 
+/* avoid warnings about missing declarations */
+int luaopen_uloop(lua_State *L);
+int luaclose_uloop(lua_State *L);
+
 int luaopen_uloop(lua_State *L)
 {
        state = L;