From 8e7cd35345544b04bbafc48498c4dd31138269e5 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Wed, 8 Nov 2023 18:41:38 +0100 Subject: [PATCH] Fix Version constructor from string --- .../java/org/eclipse/leshan/core/LwM2m.java | 17 +++++++++++------ .../org/eclipse/leshan/core/VersionTest.java | 3 +++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/LwM2m.java b/leshan-core/src/main/java/org/eclipse/leshan/core/LwM2m.java index 1e93f7e2d9..304d6ef3c2 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/LwM2m.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/LwM2m.java @@ -144,20 +144,25 @@ public static Version getDefault() { public static String validate(String version) { if (version == null || version.isEmpty()) return "version MUST NOT be null or empty"; - String[] versionPart = version.split("\\."); - if (versionPart.length != 2) { + // We use split with limit = -1 to be sure "split" will discard trailing empty strings + String[] versionParts = version.split("\\.", -1); + if (versionParts.length != 2) { return String.format("version (%s) MUST be composed of 2 parts", version); } for (int i = 0; i < 2; i++) { + String versionPart = versionParts[i]; try { - short parsedShort = Short.parseShort(versionPart[i]); + if (versionPart.length() > 1 && versionPart.startsWith("0")) { + return String.format("version (%s) part %d (%s) must not be prefixed by 0", version, i + 1, + versionPart); + } + short parsedShort = Short.parseShort(versionParts[i]); if (parsedShort < 0) { return String.format("version (%s) part %d (%s) must not be negative", version, i + 1, - versionPart[i]); + versionPart); } } catch (Exception e) { - return String.format("version (%s) part %d (%s) is not a valid short", version, i + 1, - versionPart[i]); + return String.format("version (%s) part %d (%s) is not a valid short", version, i + 1, versionPart); } } return null; diff --git a/leshan-core/src/test/java/org/eclipse/leshan/core/VersionTest.java b/leshan-core/src/test/java/org/eclipse/leshan/core/VersionTest.java index 0bfdf74e40..205347a1b9 100644 --- a/leshan-core/src/test/java/org/eclipse/leshan/core/VersionTest.java +++ b/leshan-core/src/test/java/org/eclipse/leshan/core/VersionTest.java @@ -85,6 +85,9 @@ private static Stream illegal_arguments() { return Stream.of(args(() -> new Version(null), "version MUST NOT be null or empty"), args(() -> new Version(""), "version MUST NOT be null or empty"), args(() -> new Version("foo"), "version (foo) MUST be composed of 2 parts"), + args(() -> new Version("0001.0"), "version (0001.0) part 1 (0001) must not be prefixed by 0"), + args(() -> new Version("1.02"), "version (1.02) part 2 (02) must not be prefixed by 0"), + args(() -> new Version("1.0."), "version (1.0.) MUST be composed of 2 parts"), args(() -> new Version("1.0.0"), "version (1.0.0) MUST be composed of 2 parts"), args(() -> new Version("-1.0"), "version (-1.0) part 1 (-1) must not be negative"), args(() -> new Version("1.-1"), "version (1.-1) part 2 (-1) must not be negative"),