You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/09/02 04:12:20 UTC
(maven) branch master updated: Fix version comparison and parsing inconsistencies in maven-artifact
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new 8018e58648 Fix version comparison and parsing inconsistencies in maven-artifact
8018e58648 is described below
commit 8018e58648e0417c9e204bdb33d87a9fb5d5f155
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Wed Sep 2 06:12:13 2026 +0200
Fix version comparison and parsing inconsistencies in maven-artifact
Three fixes to ComparableVersion and DefaultArtifactVersion:
1. CombinationItem.compareTo transitivity: when the string part is a
release-equivalent qualifier (ga, final, release), delegate to the
digit part so that 1-ga1 != 1 and 1-ga1 < 1-ga2, restoring the
Comparable contract and preventing exact range [1] from matching
1-gaN.
2. DefaultArtifactVersion hashCode/equals consistency: add
orderingHashCode() that mirrors compareTo semantics — trailing
null-equivalent items stripped, release-equivalent qualifiers hash
identically — so that versions comparing equal produce equal hashes.
3. ComparableVersion max length bound: reject version strings longer
than 256 characters to prevent stack overflow from deeply nested
parsing and quadratic BigInteger costs.
Includes comprehensive tests for transitivity, hashCode consistency,
exact range matching, and the length bound.
---
.../artifact/versioning/ComparableVersion.java | 76 +++++++++++++++++++++-
.../versioning/DefaultArtifactVersion.java | 5 +-
.../artifact/versioning/ComparableVersionTest.java | 30 +++++++++
.../versioning/DefaultArtifactVersionTest.java | 20 ++++++
.../artifact/versioning/VersionRangeTest.java | 10 +++
5 files changed, 139 insertions(+), 2 deletions(-)
diff --git a/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java b/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java
index df3c034528..0dc57db07b 100644
--- a/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java
+++ b/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java
@@ -68,6 +68,16 @@ public class ComparableVersion implements Comparable<ComparableVersion> {
private static final int MAX_LONGITEM_LENGTH = 18;
+ /**
+ * Maximum accepted length of a version string. Version strings routinely come from external
+ * repository metadata. Without a bound, every {@code -} separator nests another list whose
+ * comparison, equality, hash code, and canonicalization recurse one frame per level, and digit
+ * runs longer than {@value #MAX_LONGITEM_LENGTH} characters are parsed into {@link BigInteger}
+ * at quadratic cost. 256 characters is far beyond any real-world version identifier while
+ * keeping the nesting depth (at most about half the length) and numeric items small.
+ */
+ private static final int MAX_VERSION_LENGTH = 256;
+
private String value;
private String canonical;
@@ -450,7 +460,14 @@ private static class CombinationItem implements Item {
public int compareTo(Item item) {
if (item == null) {
// 1-rc1 < 1, 1-ga1 > 1
- return stringPart.compareTo(item);
+ int result = stringPart.compareTo(item);
+ if (result == 0) {
+ // the string part is equivalent to the release qualifier ("ga", "final", "release"),
+ // so the digit part decides: 1-ga1 > 1. Returning 0 here would break compareTo
+ // transitivity, since 1-ga1 < 1-ga2 while both would compare equal to 1.
+ return digitPart.compareTo(null);
+ }
+ return result;
}
int result = 0;
switch (item.getType()) {
@@ -640,8 +657,18 @@ public ComparableVersion(String version) {
parseVersion(version);
}
+ /**
+ * @throws IllegalArgumentException if the version string is longer than {@value #MAX_VERSION_LENGTH}
+ * characters, to bound the parsing, comparison and canonicalization cost of arbitrarily large input
+ */
@SuppressWarnings("checkstyle:innerassignment")
public final void parseVersion(String version) {
+ if (version.length() > MAX_VERSION_LENGTH) {
+ throw new IllegalArgumentException("Version string is too long (" + version.length() + " > "
+ + MAX_VERSION_LENGTH + " characters): "
+ + version.substring(0, 32) + "...");
+ }
+
this.value = version;
items = new ListItem();
@@ -808,6 +835,53 @@ public int hashCode() {
return items.hashCode();
}
+ /**
+ * Returns a hash code consistent with the ordering defined by {@link #compareTo(ComparableVersion)}:
+ * two versions that compare as equal get the same value even when their parsed representations
+ * differ, e.g. {@code 1-ga} ({@code [1, [ga]]}) and {@code 1} ({@code [1]}).
+ * <p>
+ * {@link #hashCode()} cannot provide this: it is structural, matching the structural
+ * {@link #equals(Object)} of this class. This method exists for classes such as
+ * {@link DefaultArtifactVersion} whose {@code equals} is defined as {@code compareTo == 0} and
+ * whose {@code hashCode} must therefore follow ordering equality (two equal objects must have
+ * equal hash codes).
+ *
+ * @return a hash code such that {@code a.compareTo(b) == 0} implies {@code a.orderingHashCode() == b.orderingHashCode()}
+ */
+ int orderingHashCode() {
+ return orderingHash(items);
+ }
+
+ private static int orderingHash(Item item) {
+ return switch (item.getType()) {
+ case Item.LIST_ITEM -> {
+ ListItem list = (ListItem) item;
+ int end = list.size();
+ // trailing items that compare as equal to null do not affect ordering: 1-ga == 1
+ while (end > 0 && list.get(end - 1).compareTo(null) == 0) {
+ end--;
+ }
+ int hash = 1;
+ for (int i = 0; i < end; i++) {
+ hash = 31 * hash + orderingHash(list.get(i));
+ }
+ yield hash;
+ }
+ // qualifiers that compare as equal ("ga", "final", "release" and the empty qualifier) must hash alike
+ case Item.STRING_ITEM ->
+ StringItem.comparableQualifier(((StringItem) item).value).hashCode();
+ case Item.COMBINATION_ITEM -> {
+ CombinationItem combination = (CombinationItem) item;
+ yield 31
+ * StringItem.comparableQualifier(combination.stringPart.value)
+ .hashCode()
+ + orderingHash(combination.digitPart);
+ }
+ // numeric items only compare as equal to items of the same type with the same value
+ default -> item.hashCode();
+ };
+ }
+
// CHECKSTYLE_OFF: LineLength
/**
diff --git a/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java b/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java
index 5d2264f245..840f2b9ed8 100644
--- a/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java
+++ b/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java
@@ -41,7 +41,10 @@ public DefaultArtifactVersion(String version) {
@Override
public int hashCode() {
- return 11 + comparable.hashCode();
+ // equals is defined as compareTo == 0, so the hash must follow ordering equality:
+ // "1-ga" and "1" are equal here and must hash alike, which the structural
+ // ComparableVersion.hashCode() does not guarantee
+ return 11 + comparable.orderingHashCode();
}
@Override
diff --git a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionTest.java b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionTest.java
index 11561aa166..1998657614 100644
--- a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionTest.java
+++ b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionTest.java
@@ -23,6 +23,7 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -187,6 +188,18 @@ void testVersionsHaveSameOrderButAreNotEqual() {
checkVersionsHaveSameOrder("1FINAL", "1");
}
+ @Test
+ void testReleaseQualifierWithDigitOrdering() {
+ // a release qualifier followed by a digit sorts above the bare version ("1-rc1 < 1, 1-ga1 > 1")
+ // and must not compare equal to it: otherwise 1-ga1 == 1 == 1-ga2 while 1-ga1 < 1-ga2,
+ // breaking compareTo transitivity, and an exact pin [1] would admit 1-gaN
+ checkVersionsOrder(new String[] {"1-rc1", "1", "1-ga1", "1-final2", "1-ga3", "1-release9", "1-ga11", "1-sp"});
+
+ // a release qualifier without a digit (or with a zero digit) still sorts as the bare version
+ checkVersionsHaveSameOrder("1-ga", "1");
+ checkVersionsHaveSameOrder("1-ga0", "1");
+ }
+
@Test
void testVersionComparing() {
checkVersionsOrder("1", "2");
@@ -448,6 +461,23 @@ void testLocaleIndependent() {
}
}
+ @Test
+ void testVersionLengthIsBounded() {
+ // versions up to the cap parse and compare normally, including deeply nested ones
+ String deep = "1" + "-1".repeat(127); // 255 characters, ~127 nested lists
+ ComparableVersion c1 = new ComparableVersion(deep);
+ ComparableVersion c2 = new ComparableVersion(deep);
+ assertEquals(0, c1.compareTo(c2), "expected a deeply nested version to compare equal to itself");
+ assertTrue(c1.compareTo(new ComparableVersion("1")) > 0, "expected 1-1-... > 1");
+
+ // beyond the cap the parser refuses instead of doing unbounded work
+ // (comparison recurses one frame per '-' level; long digit runs cost quadratic BigInteger parsing)
+ String deepTooLong = "1" + "-1".repeat(150);
+ assertThrows(IllegalArgumentException.class, () -> new ComparableVersion(deepTooLong));
+ String digitsTooLong = "1".repeat(257);
+ assertThrows(IllegalArgumentException.class, () -> new ComparableVersion(digitsTooLong));
+ }
+
@Test
void testReuse() {
ComparableVersion c1 = new ComparableVersion("1");
diff --git a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java
index c1530cbe20..19f5e34867 100644
--- a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java
+++ b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java
@@ -180,6 +180,19 @@ void testHashCode() {
assertEquals(v1.hashCode(), v2.hashCode());
}
+ @Test
+ void testHashCodeConsistentWithEquals() {
+ // equals is defined as compareTo == 0, so every compareTo-equal spelling
+ // must produce the same hash code (Object contract)
+ assertEqualsAndHash("1", "1-ga");
+ assertEqualsAndHash("1", "1-final");
+ assertEqualsAndHash("1", "1-release");
+ assertEqualsAndHash("1", "1.0-ga");
+ assertEqualsAndHash("1", "1-0");
+ assertEqualsAndHash("1", "1.0.0");
+ assertEqualsAndHash("1-SNAPSHOT", "1.0-SNAPSHOT");
+ }
+
@Test
void testEqualsNullSafe() {
assertFalse(newArtifactVersion("1").equals(null));
@@ -190,6 +203,13 @@ void testEqualsTypeSafe() {
assertFalse(newArtifactVersion("1").equals("non-an-artifact-version-instance"));
}
+ private void assertEqualsAndHash(String left, String right) {
+ ArtifactVersion v1 = newArtifactVersion(left);
+ ArtifactVersion v2 = newArtifactVersion(right);
+ assertTrue(v1.equals(v2), left + " and " + right + " should be equal");
+ assertEquals(v1.hashCode(), v2.hashCode(), left + " and " + right + " should have the same hash code");
+ }
+
private void assertVersionOlder(String left, String right) {
assertTrue(
newArtifactVersion(left).compareTo(newArtifactVersion(right)) < 0,
diff --git a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java
index 8ccf5bcca4..33b52be233 100644
--- a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java
+++ b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java
@@ -155,6 +155,16 @@ void testRange() throws InvalidVersionSpecificationException, OverConstrainedVer
assertTrue(range.containsVersion(new DefaultArtifactVersion("5.0.9.0")));
}
+ @Test
+ void testExactRangeMatchesOnlyTheExactVersion() throws InvalidVersionSpecificationException {
+ // an exact-version pin must only match the pinned version, not a differently spelled
+ // one that happens to compare equal to it (see ComparableVersionTest#testReleaseQualifierWithDigitOrdering)
+ VersionRange range = VersionRange.createFromVersionSpec("[1.2.3]");
+ assertTrue(range.containsVersion(new DefaultArtifactVersion("1.2.3")));
+ assertFalse(range.containsVersion(new DefaultArtifactVersion("1.2.3-ga2")));
+ assertFalse(range.containsVersion(new DefaultArtifactVersion("1.2.3-final1")));
+ }
+
@Test
void testSameUpperAndLowerBoundRoundtrip() throws InvalidVersionSpecificationException {
VersionRange range = VersionRange.createFromVersionSpec("[1.0]");