mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
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:
parent
7486f184c3
commit
fe13f9dfd1
5 changed files with 47 additions and 26 deletions
|
@ -23,13 +23,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
|
||||
#include "lua_api/l_base.h"
|
||||
#include "lua_api/l_metadata.h"
|
||||
#include "lua_api/l_item.h"
|
||||
#include "irrlichttypes_bloated.h"
|
||||
#include "inventory.h"
|
||||
|
||||
class ItemStackMetaRef : public MetaDataRef
|
||||
{
|
||||
private:
|
||||
ItemStack *istack = nullptr;
|
||||
LuaItemStack *istack;
|
||||
|
||||
static const char className[];
|
||||
static const luaL_Reg methods[];
|
||||
|
@ -44,12 +44,12 @@ private:
|
|||
|
||||
void setToolCapabilities(const ToolCapabilities &caps)
|
||||
{
|
||||
istack->metadata.setToolCapabilities(caps);
|
||||
istack->getItem().metadata.setToolCapabilities(caps);
|
||||
}
|
||||
|
||||
void clearToolCapabilities()
|
||||
{
|
||||
istack->metadata.clearToolCapabilities();
|
||||
istack->getItem().metadata.clearToolCapabilities();
|
||||
}
|
||||
|
||||
// Exported functions
|
||||
|
@ -58,12 +58,15 @@ private:
|
|||
// garbage collector
|
||||
static int gc_object(lua_State *L);
|
||||
public:
|
||||
ItemStackMetaRef(ItemStack *istack): istack(istack) {}
|
||||
~ItemStackMetaRef() = default;
|
||||
// takes a reference
|
||||
ItemStackMetaRef(LuaItemStack *istack);
|
||||
~ItemStackMetaRef();
|
||||
|
||||
DISABLE_CLASS_COPY(ItemStackMetaRef)
|
||||
|
||||
// Creates an ItemStackMetaRef and leaves it on top of stack
|
||||
// Not callable from Lua; all references are created on the C side.
|
||||
static void create(lua_State *L, ItemStack *istack);
|
||||
static void create(lua_State *L, LuaItemStack *istack);
|
||||
|
||||
static void Register(lua_State *L);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue