java-topology/defects/maven/patch/maven-0003-cycle-index-map.patch

42 lines
2.1 KiB
Diff

# UNDF: UNDF-2026-000000164
diff --git a/impl/maven-core/src/main/java/org/apache/maven/project/Graph.java b/impl/maven-core/src/main/java/org/apache/maven/project/Graph.java
index d69655a..d0dc27d 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/project/Graph.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/project/Graph.java
@@ -70,7 +70,7 @@ List<String> visitAll() {
}
List<String> findCycle(Vertex vertex) {
- return visitCycle(Collections.singleton(vertex), new HashMap<>(), new LinkedList<>());
+ return visitCycle(Collections.singleton(vertex), new HashMap<>(), new LinkedList<>(), new HashMap<>());
}
private static List<String> visitAll(
@@ -87,20 +87,24 @@ private static List<String> visitAll(
}
private static List<String> visitCycle(
- Collection<Vertex> children, Map<Vertex, DfsState> stateMap, LinkedList<String> cycle) {
+ Collection<Vertex> children, Map<Vertex, DfsState> stateMap, LinkedList<String> cycle,
+ Map<String, Integer> cycleIndexMap) {
for (Vertex v : children) {
DfsState state = stateMap.putIfAbsent(v, DfsState.VISITING);
if (state == null) {
+ // CWE-407 fix: track label→index in cycleIndexMap for O(1) lookup
+ cycleIndexMap.put(v.label, cycle.size());
cycle.addLast(v.label);
- List<String> ret = visitCycle(v.children, stateMap, cycle);
+ List<String> ret = visitCycle(v.children, stateMap, cycle, cycleIndexMap);
if (ret != null) {
return ret;
}
cycle.removeLast();
+ cycleIndexMap.remove(v.label);
stateMap.put(v, DfsState.VISITED);
} else if (state == DfsState.VISITING) {
// we are already visiting this vertex, this mean we have a cycle
- int pos = cycle.lastIndexOf(v.label);
+ int pos = cycleIndexMap.get(v.label);
List<String> ret = cycle.subList(pos, cycle.size());
ret.add(v.label);
return ret;