From 09564f72d17e4151765e2d44bf85f7717ae1eefd Mon Sep 17 00:00:00 2001 From: Jialin Chen Date: Wed, 21 Oct 2026 13:46:36 +0800 Subject: [PATCH] Clear removed values in IntMap IntMap.remove and the remove method of its iterators cleared the key of a removed entry but not its value. The map kept a reference to the value, containsValue still found it, findKey returned 0 for it, and after key 0 was removed with an iterator, put(0, value) returned it as the old value. Also, get(0, defaultValue) returned null instead of the default value if the map had no key 0, since get was inlined in #876. --- .../esotericsoftware/kryo/util/IntMap.java | 5 +- .../kryo/util/IntMapTest.java | 59 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/com/esotericsoftware/kryo/util/IntMapTest.java diff --git a/src/com/esotericsoftware/kryo/util/IntMap.java b/src/com/esotericsoftware/kryo/util/IntMap.java index 16ebc71ba..6e525a36b 100644 --- a/src/com/esotericsoftware/kryo/util/IntMap.java +++ b/src/com/esotericsoftware/kryo/util/IntMap.java @@ -195,7 +195,7 @@ public V get (int key) { } public V get (int key, @Null V defaultValue) { - if (key == 0) return hasZeroValue ? zeroValue : null; + if (key == 0) return hasZeroValue ? zeroValue : defaultValue; for (int i = place(key);; i = i + 1 & mask) { int other = keyTable[i]; if (other == 0) return defaultValue; @@ -230,6 +230,7 @@ public V remove (int key) { next = next + 1 & mask; } keyTable[i] = 0; + valueTable[i] = null; size--; return oldValue; } @@ -515,6 +516,7 @@ public void remove () { int i = currentIndex; if (i == INDEX_ZERO && map.hasZeroValue) { map.hasZeroValue = false; + map.zeroValue = null; } else if (i < 0) { throw new IllegalStateException("next must be called before remove."); } else { @@ -531,6 +533,7 @@ public void remove () { next = next + 1 & mask; } keyTable[i] = 0; + valueTable[i] = null; if (i != currentIndex) --nextIndex; } currentIndex = INDEX_ILLEGAL; diff --git a/test/com/esotericsoftware/kryo/util/IntMapTest.java b/test/com/esotericsoftware/kryo/util/IntMapTest.java new file mode 100644 index 000000000..900e6c1c2 --- /dev/null +++ b/test/com/esotericsoftware/kryo/util/IntMapTest.java @@ -0,0 +1,59 @@ +/* Copyright (c) 2008-2026, Nathan Sweet + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the distribution. + * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + +package com.esotericsoftware.kryo.util; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class IntMapTest { + // A removed value is not kept in the map, also if it is removed with an iterator or its key is 0. + @Test + void testRemove () { + IntMap map = new IntMap<>(); + map.put(1, "one"); + assertEquals("one", map.remove(1)); + assertFalse(map.containsValue("one", false)); + assertEquals(-1, map.findKey("one", false, -1)); + + map.put(0, "zero"); + map.put(1, "one"); + for (IntMap.Entries entries = map.entries(); entries.hasNext();) { + entries.next(); + entries.remove(); + } + assertEquals(0, map.size); + assertFalse(map.containsValue("zero", true)); + assertFalse(map.containsValue("one", false)); + assertNull(map.put(0, "new")); + } + + @Test + void testGetDefaultValue () { + IntMap map = new IntMap<>(); + assertEquals("default", map.get(0, "default")); + assertEquals("default", map.get(1, "default")); + map.put(0, null); + map.put(1, null); + assertNull(map.get(0, "default")); + assertNull(map.get(1, "default")); + } +}