diff options
author | Jude Melton-Houghton <jwmhjwmh@gmail.com> | 2022-09-11 13:28:37 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-11 19:28:37 +0200 |
commit | fe13f9dfd12c0a7f08355b83e34e7dec1bfdd86d (patch) | |
tree | 265ac5b7ce70fc411ce9436dfbae152b4ce5bd6b /src/util | |
parent | 7486f184c3c800d462cf783a0f10289dcf9ebec6 (diff) | |
download | minetest-fe13f9dfd12c0a7f08355b83e34e7dec1bfdd86d.tar.xz |
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/util')
-rw-r--r-- | src/util/pointer.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/util/pointer.h b/src/util/pointer.h index b659cea0e..f4b70f822 100644 --- a/src/util/pointer.h +++ b/src/util/pointer.h @@ -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; +}; |