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.
This commit is contained in:
Jude Melton-Houghton
2022-09-11 13:28:37 -04:00
committed by GitHub
parent 7486f184c3
commit fe13f9dfd1
5 changed files with 47 additions and 26 deletions

View File

@@ -21,11 +21,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_base.h"
#include "inventory.h" // ItemStack
#include "util/pointer.h"
class LuaItemStack : public ModApiBase {
class LuaItemStack : public ModApiBase, public IntrusiveReferenceCounted {
private:
ItemStack m_stack;
LuaItemStack(const ItemStack &item);
~LuaItemStack() = default;
static const char className[];
static const luaL_Reg methods[];
@@ -138,11 +142,10 @@ class LuaItemStack : public ModApiBase {
static int l_peek_item(lua_State *L);
public:
LuaItemStack(const ItemStack &item);
~LuaItemStack() = default;
DISABLE_CLASS_COPY(LuaItemStack)
const ItemStack& getItem() const;
ItemStack& getItem();
inline const ItemStack& getItem() const { return m_stack; }
inline ItemStack& getItem() { return m_stack; }
// LuaItemStack(itemstack or itemstring or table or nil)
// Creates an LuaItemStack and leaves it on top of stack