Summary
ToolchainDiscoverer.getCanonicalPath() has a recursive fallback that can cause infinite recursion or stack overflow for root paths where path.getParent() returns null.
Location
ToolchainDiscoverer.java:267-273
https://github.com/apache/maven-toolchains-plugin/blob/master/src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java#L267-L273
Code
private static Path getCanonicalPath(Path path) {
try {
return path.toRealPath();
} catch (IOException e) {
return getCanonicalPath(path.getParent()).resolve(path.getFileName());
}
}
Problem
- If
path is a root directory (e.g. / on Linux or C:\ on Windows), path.getParent() returns null. The recursive call getCanonicalPath(null) throws NPE.
- If
path.getParent() itself fails with IOException, this creates infinite recursion leading to stack overflow.
- The recursive approach also has no depth limit, so deeply nested paths that fail
toRealPath() will recurse until stack overflow.
Impact
JDK discovery scanning directories like / or other root-relative paths could crash Maven with a stack overflow or NPE instead of gracefully skipping the problematic path.
Suggested Fix
Replace recursion with iteration:
private static Path getCanonicalPath(Path path) {
try {
return path.toRealPath();
} catch (IOException e) {
Path parent = path.getParent();
if (parent == null) {
return path;
}
return getCanonicalPath(parent).resolve(path.getFileName());
}
}
Or better, use a non-recursive loop with null checks to eliminate the recursion entirely.
Summary
ToolchainDiscoverer.getCanonicalPath()has a recursive fallback that can cause infinite recursion or stack overflow for root paths wherepath.getParent()returns null.Location
ToolchainDiscoverer.java:267-273https://github.com/apache/maven-toolchains-plugin/blob/master/src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java#L267-L273
Code
Problem
pathis a root directory (e.g./on Linux orC:\on Windows),path.getParent()returnsnull. The recursive callgetCanonicalPath(null)throws NPE.path.getParent()itself fails with IOException, this creates infinite recursion leading to stack overflow.toRealPath()will recurse until stack overflow.Impact
JDK discovery scanning directories like
/or other root-relative paths could crash Maven with a stack overflow or NPE instead of gracefully skipping the problematic path.Suggested Fix
Replace recursion with iteration:
Or better, use a non-recursive loop with null checks to eliminate the recursion entirely.