Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -452,56 +452,38 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
}

public static int compareResourceVersions(String v1, String v2) {
var v1Length = v1.length();
if (v1Length == 0) {
throw new NonComparableResourceVersionException("Resource version (1) is empty");
}
var v2Length = v2.length();
if (v2Length == 0) {
throw new NonComparableResourceVersionException("Resource version (2) is empty");
int v1Length = validateResourceVersion(v1);
int v2Length = validateResourceVersion(v2);
int comparison = v1Length - v2Length;
if (comparison != 0) {
return comparison;
}
var maxLength = Math.max(v1Length, v2Length);
boolean v1LeadingZero = true;
boolean v2LeadingZero = true;
int comparison = 0;
for (int i = 0; i < maxLength; i++) {
char char1 = 0;
if (i < v1Length) {
char1 = v1.charAt(i);
if (v1LeadingZero) {
if (char1 == '0') {
throw new NonComparableResourceVersionException(
"Resource version (1) cannot begin with 0");
}
v1LeadingZero = false;
}
if (!Character.isDigit(char1)) {
throw new NonComparableResourceVersionException(
"Non numeric characters in resource version (1): " + char1);
}
for (int i = 0; i < v2Length; i++) {
int comp = v1.charAt(i) - v2.charAt(i);
if (comp != 0) {
return comp;
}
if (i < v2Length) {
var char2 = v2.charAt(i);
if (v2LeadingZero) {
if (char2 == '0') {
throw new NonComparableResourceVersionException(
"Resource version (2) cannot begin with 0");
}
v2LeadingZero = false;
}
if (!Character.isDigit(char2)) {
}
return 0;
}

private static final int validateResourceVersion(String v1) {
int v1Length = v1.length();
if (v1Length == 0) {
throw new NonComparableResourceVersionException("Resource version is empty");
}
for (int i = 0; i < v1Length; i++) {
char char1 = v1.charAt(i);
if (char1 == '0') {
if (i == 0) {
throw new NonComparableResourceVersionException(
"Non numeric characters in resource version (2): " + char2);
}
if (char1 == 0) {
comparison = -1;
} else if (comparison == 0) {
comparison = Character.compare(char1, char2);
"Resource version cannot begin with 0: " + v1);
}
Comment on lines +477 to 481
Copy link

Copilot AI Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation logic incorrectly allows '0' characters at any position except the first. This means resource versions like '10', '20', '100' would be valid, but '101' would fail validation when it encounters the '0' at position 1. The condition should only validate the leading zero case, not reject all '0' characters.

Copilot uses AI. Check for mistakes.
} else {
comparison = 1;
} else if (char1 < '0' || char1 > '9') {
throw new NonComparableResourceVersionException(
"Non numeric characters in resource version: " + v1);
}
}
return comparison;
return v1Length;
}
}
Loading