XuNeo / luavgl

lua + lvgl = luavgl An optimized lvgl Lua binding
MIT License
57 stars 13 forks source link

Remove event in event callback #19

Closed XuNeo closed 4 months ago

XuNeo commented 4 months ago

Introduce the problem

Continue of #10.

To free resources added to obj from LUA code, we monitor if obj is deleted by event LV_EVENT_DELETE and clean up in the event callback.

https://github.com/XuNeo/luavgl/blob/964eeba9e9b8388cad3879965b31c2e1c755e806/src/obj.c#L846-L847

However, the event callback registered to lvgl is called from below code.

https://github.com/lvgl/lvgl/blob/745ef50e65c2682f87adfa53b42daab9f2dfa685/src/misc/lv_event.c#L63-L86

Note the for loop is actually accessing the event array. So it's crucial not to mess up the array in the event callback we registered.

So below code will report heap-use-after-free, because luavgl_obj_delete will remove events registered from lua, thus modifying event array while iterating it.

https://github.com/XuNeo/luavgl/blob/964eeba9e9b8388cad3879965b31c2e1c755e806/src/obj.c#L50-L69

Proposal

No response