Skip to content

Commit

Permalink
luavgl: avoid wild pointer in lua context
Browse files Browse the repository at this point in the history
Monitor if the root obj is deleted, and change the ctx->root pointer correspondingly to avoid accessing wild pointer

Signed-off-by: Neo Xu <[email protected]>
  • Loading branch information
XuNeo committed Oct 21, 2024
1 parent 31b52d2 commit d097625
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/luavgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,17 @@ static int root_clean(lua_State *L)
{
LV_LOG_INFO("enter");
luavgl_ctx_t *ctx = luavgl_context(L);
lv_obj_clean(ctx->root);
if (ctx->root)
lv_obj_clean(ctx->root);
return 0;
}

static void root_delete_cb(lv_event_t *e)
{
luavgl_ctx_t *ctx = e->user_data;
ctx->root = NULL;
}

LUALIB_API int luaopen_lvgl(lua_State *L)
{
luavgl_ctx_t *ctx = luavgl_context(L);
Expand Down Expand Up @@ -148,7 +155,7 @@ LUALIB_API int luaopen_lvgl(lua_State *L)

#ifdef LUAVGL_EXPOSE_WIDGET_API
const luaL_Reg *reg;
for (int i = 0; ;i++) {
for (int i = 0;; i++) {
reg = &widget_create_methods[i];
if (reg->name == NULL)
break;
Expand All @@ -158,6 +165,8 @@ LUALIB_API int luaopen_lvgl(lua_State *L)
}
#endif

lv_obj_add_event_cb(root, root_delete_cb, LV_EVENT_DELETE, ctx);

(void)dumpstack;
(void)dumptable;
(void)dumpvalue;
Expand Down

0 comments on commit d097625

Please sign in to comment.