Skip to content

Commit ea06095

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Copy an Entry to avoid undefined behavior after the Map is modified.
RELNOTES=n/a PiperOrigin-RevId: 690334433
1 parent b650b9f commit ea06095

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

android/guava-testlib/src/com/google/common/collect/testing/MapInterfaceTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,10 @@ public void testEntrySetRetainAll() {
678678
}
679679

680680
Set<Entry<K, V>> entrySet = map.entrySet();
681-
Set<Entry<K, V>> entriesToRetain = singleton(entrySet.iterator().next());
681+
Entry<K, V> originalEntry = entrySet.iterator().next();
682+
// Copy the Entry, as discussed in testEntrySetRemoveAll.
683+
Set<Entry<K, V>> entriesToRetain =
684+
singleton(mapEntry(originalEntry.getKey(), originalEntry.getValue()));
682685
if (supportsRemove) {
683686
boolean shouldRemove = (entrySet.size() > entriesToRetain.size());
684687
boolean didRemove = entrySet.retainAll(entriesToRetain);

guava-testlib/src/com/google/common/collect/testing/MapInterfaceTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,10 @@ public void testEntrySetRetainAll() {
678678
}
679679

680680
Set<Entry<K, V>> entrySet = map.entrySet();
681-
Set<Entry<K, V>> entriesToRetain = singleton(entrySet.iterator().next());
681+
Entry<K, V> originalEntry = entrySet.iterator().next();
682+
// Copy the Entry, as discussed in testEntrySetRemoveAll.
683+
Set<Entry<K, V>> entriesToRetain =
684+
singleton(mapEntry(originalEntry.getKey(), originalEntry.getValue()));
682685
if (supportsRemove) {
683686
boolean shouldRemove = (entrySet.size() > entriesToRetain.size());
684687
boolean didRemove = entrySet.retainAll(entriesToRetain);

0 commit comments

Comments
 (0)