This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 0047629eb7b HDDS-16348. StorageSize.parse fails on values with 
surrounding whitespace (#11167)
0047629eb7b is described below

commit 0047629eb7b583673c70161b04d5200ec7d84a83
Author: Henry Chen <[email protected]>
AuthorDate: Sun Aug 30 21:34:45 2026 +0700

    HDDS-16348. StorageSize.parse fails on values with surrounding whitespace 
(#11167)
    
    Co-authored-by: Mark Tsai <[email protected]>
---
 .../org/apache/hadoop/hdds/conf/StorageSize.java   |  2 +-
 .../apache/hadoop/hdds/conf/TestStorageSize.java   | 65 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletion(-)

diff --git 
a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/StorageSize.java 
b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/StorageSize.java
index 85550e4a446..bf3aef66097 100644
--- 
a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/StorageSize.java
+++ 
b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/StorageSize.java
@@ -86,7 +86,7 @@ public static StorageSize parse(String value) {
         "match. Internal error.");
 
     String valString =
-        sanitizedValue.substring(0, value.length() - suffix.length());
+        sanitizedValue.substring(0, sanitizedValue.length() - suffix.length());
     return new StorageSize(parsedUnit, Double.parseDouble(valString));
 
   }
diff --git 
a/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestStorageSize.java
 
b/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestStorageSize.java
new file mode 100644
index 00000000000..e838a5c6b38
--- /dev/null
+++ 
b/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestStorageSize.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.conf;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+/**
+ * Tests for {@link StorageSize}.
+ */
+class TestStorageSize {
+
+  @Test
+  void parsesValueAndUnit() {
+    StorageSize size = StorageSize.parse("10MB");
+
+    assertEquals(StorageUnit.MB, size.getUnit());
+    assertEquals(10.0, size.getValue());
+  }
+
+  /**
+   * XML config values are commonly indented, so parse() trims its input.
+   * The cases below cover whitespace shorter than, equal to, and longer than
+   * the unit suffix being stripped.
+   */
+  @ParameterizedTest
+  @ValueSource(strings = {" 10MB", "10MB ", " 10MB ", "\n    10MB\n  "})
+  void parsesValueWithSurroundingWhitespace(String value) {
+    StorageSize size = StorageSize.parse(value);
+
+    assertEquals(StorageUnit.MB, size.getUnit());
+    assertEquals(10.0, size.getValue());
+  }
+
+  /**
+   * The default-unit overload only falls back on IllegalArgumentException,
+   * so whitespace long enough to push the offset past the end of the value
+   * used to escape it as a StringIndexOutOfBoundsException.
+   */
+  @Test
+  void parsesWhitespaceWithDefaultUnit() {
+    StorageSize size = StorageSize.parse("\n    5GB\n  ", StorageUnit.BYTES);
+
+    assertEquals(StorageUnit.GB, size.getUnit());
+    assertEquals(5.0, size.getValue());
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to