Skip to content

VersionMapper throws StringIndexOutOfBoundsException and strips wrong version occurrence #374

Description

@elharo

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)

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 -<version>(-<classifier>).<type> 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. -<version> or -<version>(-<classifier>) before the extension), and guard against index == 0.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions