apache / apache/maven-antrun-plugin
VersionMapper throws StringIndexOutOfBoundsException and strips wrong version occurrence
- Dominant language
- Java
- Stars
- 36
- Forks
- 20
- Avg merge
- 1h 48m
- Merged PRs (30d)
- 1
Description
## Summary
`VersionMapper` both crashes and produces wrong results in edge cases:
- `StringIndexOutOfBoundsException` when the version string is found at index 0 of the filename.
- It removes the **first** occurrence of the version string, so when the version also appears inside the artifactId (or another segment), the wrong part of the filename is stripped.
## Affected code
`src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java` lines 40-56 (master @ `441382c`)
```java
public String[] mapFileName(String sourceFileName) {
String originalFileName = new File(sourceFileName).getName();
for (String version : versions) {
int index = originalFileName.indexOf(version);
if (index >= 0) {
String baseFilename = originalFileName.substring(0, index - 1);
String extension = originalFileName.substring(index + version.length());
...
return new String[] {path + baseFilename + extension};
}
}
return new String[] {sourceFileName};
}
```
## Reproduction (verified against the built artifact)
- Version found at index 0: `setFrom("1.0")`, `mapFileName("1.0.jar")` →
`StringIndexOutOfBoundsException: begin 0, end -1, length 7` (line 46, `substring(0, index - 1)` with `index == 0`).
- Version occurring inside the artifactId: `setFrom("1.0")`, `mapFileName("a-1.0-b-1.0.jar")` → `a-b-1.0.jar`. The first occurrence (in the artifactId) is stripped instead of the trailing version suffix; the intended result is `a-1.0-b.jar`.
## Problem
`indexOf` finds the first occurrence, not the trailing `-(-).` segment the mapper is documented to strip. Combined with the unguarded `substring(0, index - 1)`, both a crash and silent wrong renames are possible.
## Expected behavior
Match the version segment anchored to the trailing `-` (e.g. `-` or `-(-)` before the extension), and guard against `index == 0`.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.