Skip to content

Commit

Permalink
Tell us which module corrupted the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
CurlyMoo committed Jun 16, 2019
1 parent a624dcc commit 3d3dfe5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 20 additions & 1 deletion libs/pilight/lua_c/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ struct lua_state_t *plua_get_current_state(lua_State *L) {
return NULL;
}

void plua_clear_state(struct lua_state_t *state) {
void _plua_clear_state(struct lua_state_t *state, char *file, int line) {
int i = 0;
uv_mutex_lock(&state->gc.lock);
for(i=0;i<state->gc.nr;i++) {
Expand All @@ -1027,6 +1027,12 @@ void plua_clear_state(struct lua_state_t *state) {
state->gc.nr = 0;
state->gc.size = 0;

state->oldmod = state->module;
if(state->oldmod != NULL) {
state->oldmod->btfile = file;
state->oldmod->btline = line;
}

uv_mutex_unlock(&state->gc.lock);

if(state->L != NULL) {
Expand Down Expand Up @@ -1873,10 +1879,17 @@ static unsigned int number2bitwise(unsigned int num) {
};

int plua_check_stack(lua_State *L, int numargs, ...) {
struct lua_state_t *state = plua_get_current_state(L);
unsigned int val = 0, i = 0, a = 0, b = 0;
va_list ap;

if(lua_gettop(L) != numargs || lua_gettop(L) < 0) {
plua_stack_dump(L);
if(state->oldmod != NULL) {
logprintf(LOG_ERR, "%s:%s %s #%d\n",
state->oldmod->name, state->oldmod->file, state->oldmod->btfile, state->oldmod->btline
);
}
return -1;
}

Expand All @@ -1886,6 +1899,12 @@ int plua_check_stack(lua_State *L, int numargs, ...) {
a = number2bitwise(lua_type(L, i+1));
b = val;
if((b & a) != a) {
plua_stack_dump(L);
if(state->oldmod != NULL) {
logprintf(LOG_ERR, "%:%s %s #%d\n",
state->oldmod->name, state->oldmod->file, state->oldmod->btfile, state->oldmod->btline
);
}
return -1;
}
}
Expand Down
7 changes: 6 additions & 1 deletion libs/pilight/lua_c/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ typedef struct plua_module_t {
char *bytecode;
int size;
int type;

int btline;
const char *btfile;
// struct plua_metatable_t *table;

struct plua_module_t *next;
Expand Down Expand Up @@ -114,7 +117,7 @@ void plua_module_load(char *, int);
int plua_module_exists(char *, int);
void plua_metatable_clone(struct plua_metatable_t **, struct plua_metatable_t **);
struct lua_state_t *plua_get_free_state(void);
void plua_clear_state(struct lua_state_t *state);
void _plua_clear_state(struct lua_state_t *state, char *file, int line);
struct lua_state_t *plua_get_current_state(lua_State *L);
struct plua_module_t *plua_get_modules(void);
void plua_init(void);
Expand All @@ -136,4 +139,6 @@ int plua_gc(void);
luaL_error(a, b, ##__VA_ARGS__); \
} while(0)

#define plua_clear_state(a) _plua_clear_state(a, __FILE__, __LINE__);

#endif

0 comments on commit 3d3dfe5

Please sign in to comment.