diff options
author | Jude Melton-Houghton <jwmhjwmh@gmail.com> | 2022-09-11 13:28:37 -0400 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2022-09-14 13:48:06 +0200 |
commit | f8bb0cd3d1da9d2d9d8dffe78cd0fb651e16a8af (patch) | |
tree | e301b693df3b4b79302bbca1221887046da84d28 /src/script/lua_api/l_itemstackmeta.cpp | |
parent | 129aef758ece753e684aa494e5471045a996ac8f (diff) | |
download | minetest-f8bb0cd3d1da9d2d9d8dffe78cd0fb651e16a8af.tar.gz minetest-f8bb0cd3d1da9d2d9d8dffe78cd0fb651e16a8af.tar.bz2 minetest-f8bb0cd3d1da9d2d9d8dffe78cd0fb651e16a8af.zip |
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.
Diffstat (limited to 'src/script/lua_api/l_itemstackmeta.cpp')
-rw-r--r-- | src/script/lua_api/l_itemstackmeta.cpp | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/script/lua_api/l_itemstackmeta.cpp b/src/script/lua_api/l_itemstackmeta.cpp index 739fb9221..c17bb8995 100644 --- a/src/script/lua_api/l_itemstackmeta.cpp +++ b/src/script/lua_api/l_itemstackmeta.cpp @@ -38,12 +38,12 @@ ItemStackMetaRef* ItemStackMetaRef::checkobject(lua_State *L, int narg) Metadata* ItemStackMetaRef::getmeta(bool auto_create) { - return &istack->metadata; + return &istack->getItem().metadata; } void ItemStackMetaRef::clearMeta() { - istack->metadata.clear(); + istack->getItem().metadata.clear(); } void ItemStackMetaRef::reportMetadataChange(const std::string *name) @@ -67,6 +67,16 @@ int ItemStackMetaRef::l_set_tool_capabilities(lua_State *L) return 0; } +ItemStackMetaRef::ItemStackMetaRef(LuaItemStack *istack): istack(istack) +{ + istack->grab(); +} + +ItemStackMetaRef::~ItemStackMetaRef() +{ + istack->drop(); +} + // garbage collector int ItemStackMetaRef::gc_object(lua_State *L) { ItemStackMetaRef *o = *(ItemStackMetaRef **)(lua_touserdata(L, 1)); @@ -76,7 +86,7 @@ int ItemStackMetaRef::gc_object(lua_State *L) { // Creates an NodeMetaRef and leaves it on top of stack // Not callable from Lua; all references are created on the C side. -void ItemStackMetaRef::create(lua_State *L, ItemStack *istack) +void ItemStackMetaRef::create(lua_State *L, LuaItemStack *istack) { ItemStackMetaRef *o = new ItemStackMetaRef(istack); //infostream<<"NodeMetaRef::create: o="<<o<<std::endl; |