From 9f2ef3434390da0f558224acccfdbdc9194c65e9 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 24 Oct 2024 16:37:25 +0200 Subject: [PATCH] [MNG-8349] Avoid exceptions with invalid modelVersion (#1848) --- .../internal/impl/model/DefaultModelValidator.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java index 56f3fde5f3..7dd0adf4c2 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java @@ -1963,8 +1963,7 @@ public class DefaultModelValidator implements ModelValidator { String[] firstSegments = first.split("\\."); String[] secondSegments = second.split("\\."); for (int i = 0; i < Math.max(firstSegments.length, secondSegments.length); i++) { - int result = Long.valueOf(i < firstSegments.length ? firstSegments[i] : "0") - .compareTo(Long.valueOf(i < secondSegments.length ? secondSegments[i] : "0")); + int result = asLong(i, firstSegments).compareTo(asLong(i, secondSegments)); if (result != 0) { return result; } @@ -1972,6 +1971,14 @@ public class DefaultModelValidator implements ModelValidator { return 0; } + private static Long asLong(int index, String[] segments) { + try { + return Long.valueOf(index < segments.length ? segments[index] : "0"); + } catch (NumberFormatException e) { + return 0L; + } + } + @SuppressWarnings("checkstyle:parameternumber") private boolean validateBannedCharacters( String prefix,