Skip to content

Commit 4122595

Browse files
Wyveraldfmeum
andauthored
[7.6.0] Handle large numeric segments in Bazel module versions (#25632)
Numeric segments can now be unsigned longs rather than just signed integers. Segments that exceed this (large) range now result in an error rather than a crash. Work towards #23461 Closes #24945. PiperOrigin-RevId: 718451995 Change-Id: I141b707e8c28d5fe764b47b4c35f163a3621e4c5 Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent 44cbffb commit 4122595

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/Version.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ abstract static class Identifier implements Comparable<Identifier> {
8787

8888
abstract boolean isDigitsOnly();
8989

90-
abstract int asNumber();
90+
abstract long asNumber();
9191

9292
abstract String asString();
9393

@@ -96,15 +96,19 @@ static Identifier from(String string) throws ParseException {
9696
throw new ParseException("identifier is empty");
9797
}
9898
if (string.chars().allMatch(Character::isDigit)) {
99-
return new AutoValue_Version_Identifier(true, Integer.parseInt(string), string);
99+
try {
100+
return new AutoValue_Version_Identifier(true, Long.parseUnsignedLong(string), string);
101+
} catch (NumberFormatException e) {
102+
throw new ParseException("numeric version segment is too large: " + string, e);
103+
}
100104
} else {
101105
return new AutoValue_Version_Identifier(false, 0, string);
102106
}
103107
}
104108

105109
private static final Comparator<Identifier> COMPARATOR =
106110
comparing(Identifier::isDigitsOnly, trueFirst())
107-
.thenComparingInt(Identifier::asNumber)
111+
.thenComparing((a, b) -> Long.compareUnsigned(a.asNumber(), b.asNumber()))
108112
.thenComparing(Identifier::asString);
109113

110114
@Override

src/test/java/com/google/devtools/build/lib/bazel/bzlmod/VersionTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ public void testPrereleaseVersion() throws Exception {
7070
assertThat(Version.parse("1.0-pre.99")).isLessThan(Version.parse("1.0-pre.2a"));
7171
assertThat(Version.parse("1.0-pre.patch.3")).isLessThan(Version.parse("1.0-pre.patch.4"));
7272
assertThat(Version.parse("1.0--")).isLessThan(Version.parse("1.0----"));
73+
assertThat(Version.parse("2.1.1-develop.bcr.20250113215904"))
74+
.isGreaterThan(Version.parse("2.1.1-develop.bcr.20250113215903"));
7375
}
7476

7577
@Test
76-
public void testParseException() throws Exception {
78+
public void testParseException() {
7779
assertThrows(ParseException.class, () -> Version.parse("-abc"));
7880
assertThrows(ParseException.class, () -> Version.parse("1_2"));
7981
assertThrows(ParseException.class, () -> Version.parse("ßážëł"));
8082
assertThrows(ParseException.class, () -> Version.parse("1.0-pre?"));
83+
assertThrows(
84+
ParseException.class, () -> Version.parse("1.0-11111111111111111111111111111111111111111"));
8185
assertThrows(ParseException.class, () -> Version.parse("1.0-pre///"));
8286
assertThrows(ParseException.class, () -> Version.parse("1..0"));
8387
assertThrows(ParseException.class, () -> Version.parse("1.0-pre..erp"));

0 commit comments

Comments
 (0)