mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Don't crash in HashTable::clear_with_capacity on an empty table
When calling clear_with_capacity on an empty HashTable/HashMap, a null deref would occur when trying to memset() m_buckets. Checking that it has capacity before clearing fixes the issue.
This commit is contained in:
parent
8f0fdef856
commit
a1300d3797
Notes:
sideshowbarker
2024-07-18 03:23:00 +09:00
Author: https://github.com/Zaggy1024 Commit: https://github.com/SerenityOS/serenity/commit/a1300d3797 Pull-request: https://github.com/SerenityOS/serenity/pull/16018 Reviewed-by: https://github.com/ADKaster ✅
2 changed files with 11 additions and 0 deletions
|
@ -291,6 +291,8 @@ public:
|
|||
}
|
||||
void clear_with_capacity()
|
||||
{
|
||||
if (m_capacity == 0)
|
||||
return;
|
||||
if constexpr (!Detail::IsTriviallyDestructible<T>) {
|
||||
for (auto* bucket : *this)
|
||||
bucket->~T();
|
||||
|
|
|
@ -309,3 +309,12 @@ TEST_CASE(reinsertion)
|
|||
map.remove("__sak");
|
||||
map.set("__sak");
|
||||
}
|
||||
|
||||
TEST_CASE(clear_with_capacity_when_empty)
|
||||
{
|
||||
HashTable<int> map;
|
||||
map.clear_with_capacity();
|
||||
map.set(0);
|
||||
map.set(1);
|
||||
VERIFY(map.size() == 2);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue