Skip to content

Commit

Permalink
Copy an Entry to avoid undefined behavior after the Map is modified.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 690334433
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 27, 2024
1 parent b650b9f commit ea06095
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ public void testEntrySetRetainAll() {
}

Set<Entry<K, V>> entrySet = map.entrySet();
Set<Entry<K, V>> entriesToRetain = singleton(entrySet.iterator().next());
Entry<K, V> originalEntry = entrySet.iterator().next();
// Copy the Entry, as discussed in testEntrySetRemoveAll.
Set<Entry<K, V>> entriesToRetain =
singleton(mapEntry(originalEntry.getKey(), originalEntry.getValue()));
if (supportsRemove) {
boolean shouldRemove = (entrySet.size() > entriesToRetain.size());
boolean didRemove = entrySet.retainAll(entriesToRetain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ public void testEntrySetRetainAll() {
}

Set<Entry<K, V>> entrySet = map.entrySet();
Set<Entry<K, V>> entriesToRetain = singleton(entrySet.iterator().next());
Entry<K, V> originalEntry = entrySet.iterator().next();
// Copy the Entry, as discussed in testEntrySetRemoveAll.
Set<Entry<K, V>> entriesToRetain =
singleton(mapEntry(originalEntry.getKey(), originalEntry.getValue()));
if (supportsRemove) {
boolean shouldRemove = (entrySet.size() > entriesToRetain.size());
boolean didRemove = entrySet.retainAll(entriesToRetain);
Expand Down

0 comments on commit ea06095

Please sign in to comment.