From fe13f9dfd12c0a7f08355b83e34e7dec1bfdd86d Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sun, 11 Sep 2022 13:28:37 -0400 Subject: Fix potential use-after-free with item metadata (#12729) This fixes a use-after-free bug in the case where itemstack metadata is accessed after the itemstack has been garbage-collected. --- src/script/lua_api/l_item.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'src/script/lua_api/l_item.cpp') diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index cf0fcce71..bf73e78c1 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., int LuaItemStack::gc_object(lua_State *L) { LuaItemStack *o = *(LuaItemStack **)(lua_touserdata(L, 1)); - delete o; + o->drop(); return 0; } @@ -152,7 +152,7 @@ int LuaItemStack::l_get_meta(lua_State *L) { NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); - ItemStackMetaRef::create(L, &o->m_stack); + ItemStackMetaRef::create(L, o); return 1; } @@ -438,15 +438,6 @@ LuaItemStack::LuaItemStack(const ItemStack &item): { } -const ItemStack& LuaItemStack::getItem() const -{ - return m_stack; -} -ItemStack& LuaItemStack::getItem() -{ - return m_stack; -} - // LuaItemStack(itemstack or itemstring or table or nil) // Creates an LuaItemStack and leaves it on top of stack int LuaItemStack::create_object(lua_State *L) -- cgit v1.2.3