From e64c9b095e67aae3b35f001a7dd98e61fa5faf7b Mon Sep 17 00:00:00 2001 From: Eric Eilebrecht Date: Fri, 12 Jan 2024 11:52:31 -0800 Subject: [PATCH] Fix stack overflow in TreapMapBuilder.clear() --- .../kotlin/com/certora/collect/TreapMapBuilder.kt | 2 ++ .../test/kotlin/com/certora/collect/TreapMapTest.kt | 13 +++++++++++++ .../test/kotlin/com/certora/collect/TreapSetTest.kt | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/collect/src/main/kotlin/com/certora/collect/TreapMapBuilder.kt b/collect/src/main/kotlin/com/certora/collect/TreapMapBuilder.kt index 8c6e09c..29233b5 100644 --- a/collect/src/main/kotlin/com/certora/collect/TreapMapBuilder.kt +++ b/collect/src/main/kotlin/com/certora/collect/TreapMapBuilder.kt @@ -15,6 +15,8 @@ internal class TreapMapBuilder<@Treapable K, V>( override fun get(key: K): V? = map.get(key) override fun getOrDefault(key: K, defaultValue: @UnsafeVariance V): V = map.getOrDefault(key, defaultValue) + override fun clear() { map = map.clear() } + override fun remove(key: K): V? { val oldMap = map map = map.remove(key) diff --git a/collect/src/test/kotlin/com/certora/collect/TreapMapTest.kt b/collect/src/test/kotlin/com/certora/collect/TreapMapTest.kt index 50cfc67..0eb8e4e 100644 --- a/collect/src/test/kotlin/com/certora/collect/TreapMapTest.kt +++ b/collect/src/test/kotlin/com/certora/collect/TreapMapTest.kt @@ -122,6 +122,19 @@ abstract class TreapMapTest { } } + @Test + fun clear() { + val m = makeMap() + val k = makeKey(1) + val v = Object() + m.put(k, v) + assertEquals(1, m.size) + assertEquals(v, m[k]) + m.clear() + assertEquals(0, m.size) + assertNull(m[k]) + } + @Test fun addMany() { val b = makeBaseline() diff --git a/collect/src/test/kotlin/com/certora/collect/TreapSetTest.kt b/collect/src/test/kotlin/com/certora/collect/TreapSetTest.kt index c494c5e..04bb170 100644 --- a/collect/src/test/kotlin/com/certora/collect/TreapSetTest.kt +++ b/collect/src/test/kotlin/com/certora/collect/TreapSetTest.kt @@ -125,6 +125,18 @@ abstract class TreapSetTest { } } + @Test + fun clear() { + val s = makeSet() + val k = makeKey(1) + s.add(k) + assertEquals(1, s.size) + assertTrue(k in s) + s.clear() + assertEquals(0, s.size) + assertFalse(k in s) + } + fun randomHashObjectMaybeNull(rand: Random): TestKey? { val r = rand.nextInt() if (nullKeysAllowed) {