Parijat Biswas created COLLECTIONS-902:
------------------------------------------
Summary: SetUniqueList.removeIf leaves uniqueness set inconsistent
when predicate throws
Key: COLLECTIONS-902
URL: https://issues.apache.org/jira/browse/COLLECTIONS-902
Project: Commons Collections
Issue Type: Bug
Components: List
Affects Versions: 4.5.0
Environment: Linux; Eclipse Temurin JDK 21.0.13; commons-collections4
4.5.0 and master 0d09264a50120494a3f37784874ae661b0ecf0b2.
Reporter: Parijat Biswas
SetUniqueList.removeIf traverses the decorated list and the uniqueness set
separately. If a deterministic predicate removes an element from a LinkedList
and then throws, the first traversal changes the list, but the second traversal
is never reached. The list and set then disagree: contains reports that a
removed element is present, and adding it again is rejected.
Reproduced with commons-collections4 4.5.0 and current master
0d09264a50120494a3f37784874ae661b0ecf0b2. 4.5.0 is the earliest version tested,
not a claim about when the defect was introduced.
{code:java}
SetUniqueList<String> list = SetUniqueList.setUniqueList(
new LinkedList<>(Arrays.asList("a", "b", "c")));
try {
list.removeIf(value -> {
if ("b".equals(value)) {
throw new IllegalStateException("test");
}
return true;
});
} catch (IllegalStateException expected) {
// The predicate's exception is expected.
}
System.out.println(list); // [b, c]
System.out.println(list.asSet()); // [a, b, c]
System.out.println(list.contains("a")); // true
System.out.println(list.add("a")); // false
{code}
The exception may leave a partially changed list, but the uniqueness set should
stay consistent with the elements actually remaining in that list. The current
implementation also evaluates the predicate twice per element in a successful
traversal, which can independently cause inconsistent results for stateful
predicates.
A small fix uses the existing SetUniqueList iterator, whose remove method
updates both structures, and retains the null-filter check even for an empty
list. Three regression tests cover predicate evaluation count, consistency
after an exception, and rejection of a null predicate on an empty list. Two
fail against unchanged source, and all pass with the fix.
Validation on Linux with JDK 21: 22,263 tests across all 254 test classes in
bounded batches, zero failures/errors and 82 existing skips. JaCoCo coverage
gates and verify, RAT, PMD, Checkstyle, japicmp and Javadoc checks passed
afterward. This was split validation, not one uninterrupted default Maven
invocation. No tests were weakened.
Related COLLECTIONS-444 concerns set(index, value), not removeIf. The
SetUniqueList and removeIf Jira searches and current open pull requests did not
reveal a matching fix.
AI assistance: Instinct helped investigate the defect, prepare the fix and
regression tests, and run validation.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)