From 50698a0db4621e34969146f089e019fa93d29dc4 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 22 Oct 2021 20:15:47 +0200 Subject: [PATCH] AK: Prevent accidental misuse of BumpAllocator In particular, we implicitly required that the caller initializes the returned instances themselves (solved by making UniformBumpAllocator::allocate call the constructor), and BumpAllocator itself cannot handle classes that are not trivially deconstructible (solved by deleting the method). Co-authored-by: Ali Mohammad Pur --- AK/BumpAllocator.h | 14 ++++++-------- Userland/Libraries/LibRegex/RegexMatcher.cpp | 9 ++++----- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/AK/BumpAllocator.h b/AK/BumpAllocator.h index bce07c4fae..8fc9bfa36b 100644 --- a/AK/BumpAllocator.h +++ b/AK/BumpAllocator.h @@ -51,12 +51,6 @@ public: return (void*)aligned_ptr; } - template - T* allocate() - { - return (T*)allocate(sizeof(T), alignof(T)); - } - void deallocate_all() { if (!m_head_chunk) @@ -160,9 +154,13 @@ public: destroy_all(); } - T* allocate() + template + T* allocate(Args&&... args) { - return Allocator::template allocate(); + auto ptr = (T*)Allocator::allocate(sizeof(T), alignof(T)); + if (!ptr) + return nullptr; + return new (ptr) T { forward(args)... }; } void deallocate_all() diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp index b0c8708bad..dbc5b98cbe 100644 --- a/Userland/Libraries/LibRegex/RegexMatcher.cpp +++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp @@ -333,13 +333,12 @@ public: ALWAYS_INLINE void append(T value) { - auto new_node = m_allocator.allocate(); - VERIFY(new_node); - auto node_ptr = new (new_node) Node { move(value), nullptr, nullptr }; + auto node_ptr = m_allocator.allocate(move(value)); + VERIFY(node_ptr); if (!m_first) { - m_first = new_node; - m_last = new_node; + m_first = node_ptr; + m_last = node_ptr; return; }