From 6fb99d6b1ebc5bf3bbd8edef199fd0bc8b34100f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 29 Dec 2023 09:45:38 +1100 Subject: [PATCH] AP_Scripting: fixed use after free bug found with valgrind --- libraries/AP_Scripting/lua_scripts.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/libraries/AP_Scripting/lua_scripts.cpp b/libraries/AP_Scripting/lua_scripts.cpp index d1c8badff71d95..b1b60a29c8a711 100644 --- a/libraries/AP_Scripting/lua_scripts.cpp +++ b/libraries/AP_Scripting/lua_scripts.cpp @@ -402,19 +402,18 @@ void lua_scripts::remove_script(lua_State *L, script_info *script) { } } + { + // Remove from running checksum + WITH_SEMAPHORE(crc_sem); + running_checksum ^= script->crc; + } + if (L != nullptr) { // state could be null if we are force killing all scripts luaL_unref(L, LUA_REGISTRYINDEX, script->lua_ref); } _heap.deallocate(script->name); _heap.deallocate(script); - - { - // Remove from running checksum - WITH_SEMAPHORE(crc_sem); - running_checksum ^= script->crc; - } - } void lua_scripts::reschedule_script(script_info *script) {