mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-06 17:41:04 +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
|
@ -257,3 +257,17 @@ private:
|
|||
unsigned int *refcount;
|
||||
};
|
||||
|
||||
// This class is not thread-safe!
|
||||
class IntrusiveReferenceCounted {
|
||||
public:
|
||||
IntrusiveReferenceCounted() = default;
|
||||
virtual ~IntrusiveReferenceCounted() = default;
|
||||
void grab() noexcept { ++m_refcount; }
|
||||
void drop() noexcept { if (--m_refcount == 0) delete this; }
|
||||
|
||||
// Preserve own reference count.
|
||||
IntrusiveReferenceCounted(const IntrusiveReferenceCounted &) {}
|
||||
IntrusiveReferenceCounted &operator=(const IntrusiveReferenceCounted &) { return *this; }
|
||||
private:
|
||||
u32 m_refcount = 1;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue