From 15d3c96afdbb73505f128c5d3a6393ed24140c67 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Thu, 11 Oct 2018 09:57:35 +0200 Subject: [PATCH 1/2] Add testcase whether OAHashMap loses keys This demonstrates issue #22928. --- main/tests/test_oa_hash_map.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/main/tests/test_oa_hash_map.cpp b/main/tests/test_oa_hash_map.cpp index 26e728d3aa1..deaba285cf6 100644 --- a/main/tests/test_oa_hash_map.cpp +++ b/main/tests/test_oa_hash_map.cpp @@ -92,6 +92,35 @@ MainLoop *test() { } } + // stress test / test for issue #22928 + { + OAHashMap map; + int dummy; + const int N = 1000; + uint32_t *keys = new uint32_t[N]; + + Math::seed(0); + + // insert a couple of random keys (with a dummy value, which is ignored) + for (int i = 0; i < N; i++) { + keys[i] = Math::rand(); + map.set(keys[i], dummy); + + if (!map.lookup(keys[i], dummy)) + OS::get_singleton()->print("could not find 0x%X despite it was just inserted!\n", unsigned(keys[i])); + } + + // check whether the keys are still present + for (int i = 0; i < N; i++) { + if (!map.lookup(keys[i], dummy)) { + OS::get_singleton()->print("could not find 0x%X despite it has been inserted previously! (not checking the other keys, breaking...)\n", unsigned(keys[i])); + break; + } + } + + delete[] keys; + } + return NULL; } } // namespace TestOAHashMap From 0353182e7ba925d3a162decb6f2327fec95764e2 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Thu, 11 Oct 2018 12:53:32 +0200 Subject: [PATCH 2/2] Fix bug with OAHashMap corruption on insertion When an item has been inserted into an already-occupied slot, and the original inhabitant of that slot was moved on, it was wrongly moved with the inserted-item's key/value instead of its own. This closes #22928. --- core/oa_hash_map.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/oa_hash_map.h b/core/oa_hash_map.h index 3705762d6cb..98404425192 100644 --- a/core/oa_hash_map.h +++ b/core/oa_hash_map.h @@ -125,7 +125,7 @@ private: while (42) { if (hashes[pos] == EMPTY_HASH) { - _construct(pos, hash, p_key, p_value); + _construct(pos, hash, key, value); return; } @@ -136,7 +136,7 @@ private: if (hashes[pos] & DELETED_HASH_BIT) { // we found a place where we can fit in! - _construct(pos, hash, p_key, p_value); + _construct(pos, hash, key, value); return; }