Copilot commented on code in PR #719:
URL:
https://github.com/apache/commons-collections/pull/719#discussion_r3677680438
##########
src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java:
##########
@@ -134,6 +211,37 @@ public synchronized Object merge(final Object key, final
Object value,
return merge;
}
+ /**
+ * Creates an iterator over the keys in insertion order whose {@link
Iterator#remove()} also removes the mapping.
+ *
+ * @return A new iterator.
+ */
+ private Iterator<Object> orderedKeysIterator() {
+ final Iterator<Object> iterator = orderedKeys.iterator();
+ return new Iterator<Object>() {
+
+ private Object last;
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public Object next() {
+ last = iterator.next();
+ return last;
+ }
+
+ @Override
+ public void remove() {
+ // Not remove(Object), which would edit orderedKeys while this
iterator walks it.
+ iterator.remove();
+ OrderedProperties.super.remove(last);
+ }
Review Comment:
`orderedKeysIterator().remove()` mutates `orderedKeys` via
`iterator.remove()` without holding the `OrderedProperties` monitor, while
other `orderedKeys` mutations are done inside `synchronized` methods. This can
race with `toString()`, `keys()`, `put()`, etc. (which assume `orderedKeys` is
only modified under that monitor) and can temporarily desync order/mappings or
trigger `ConcurrentModificationException` during otherwise-synchronized
operations.
Synchronize the combined ordered-key + backing-map removal on
`OrderedProperties.this` so all `orderedKeys` writes stay under the same lock.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]