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

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


The following commit(s) were added to refs/heads/master by this push:
     new e22fa0be108 [fix][misc] Throw TypeConversionException when 
ByteUnitToIntegerConverter's long->int conversion overflows (#25901)
e22fa0be108 is described below

commit e22fa0be108d22ebcd45b84336a2aa22930980f3
Author: 陈家名 <[email protected]>
AuthorDate: Thu Jun 4 02:34:06 2026 +0800

    [fix][misc] Throw TypeConversionException when ByteUnitToIntegerConverter's 
long->int conversion overflows (#25901)
---
 .../picocli/ByteUnitToIntegerConverter.java        |  2 +-
 .../pulsar/cli/converters/ByteConversionTest.java  | 38 +++++++++++++++++++---
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git 
a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/picocli/ByteUnitToIntegerConverter.java
 
b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/picocli/ByteUnitToIntegerConverter.java
index 2e5a15c9d9c..c9de6b286c7 100644
--- 
a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/picocli/ByteUnitToIntegerConverter.java
+++ 
b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/picocli/ByteUnitToIntegerConverter.java
@@ -27,7 +27,7 @@ public class ByteUnitToIntegerConverter implements 
ITypeConverter<Integer> {
     public Integer convert(String value) throws Exception {
         try {
             long l = validateSizeString(value);
-            return (int) l;
+            return Math.toIntExact(l);
         } catch (Exception e) {
             throw new TypeConversionException(e.getMessage());
         }
diff --git 
a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java
 
b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java
index 6e7a2e6d7e3..4d7f4561550 100644
--- 
a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java
+++ 
b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java
@@ -44,6 +44,20 @@ public class ByteConversionTest {
         };
     }
 
+    @DataProvider
+    public static Object[][] successfulByteUnitIntegerConverterTestCases() {
+        return new Object[][] {
+                {"4096", 4096},
+                {"1000", 1000},
+                {"100K", 102400},
+                {"100k", 102400},
+                {"100M", 104857600},
+                {"100m", 104857600},
+                {"1G", 1073741824},
+                {"1g", 1073741824},
+        };
+    }
+
     @DataProvider
     public static Object[][] failingByteUnitUtilTestCases() {
         return new Object[][] {
@@ -54,6 +68,16 @@ public class ByteConversionTest {
         };
     }
 
+    @DataProvider
+    public static Object[][] overflowingByteUnitIntegerConverterTestCases() {
+        return new Object[][] {
+                {"2G"},
+                {"2g"},
+                {"100G"},
+                {"100T"},
+        };
+    }
+
     @Test(dataProvider = "successfulByteUnitUtilTestCases")
     public void testSuccessfulByteUnitUtilConversion(String input, long 
expected) {
         assertEquals(ByteUnitUtil.validateSizeString(input), expected);
@@ -65,11 +89,10 @@ public class ByteConversionTest {
         assertEquals(converter.convert(input), Long.valueOf(expected));
     }
 
-    @Test(dataProvider = "successfulByteUnitUtilTestCases")
-    public void testSuccessfulByteUnitIntegerConverter(String input, long 
expected) throws Exception {
+    @Test(dataProvider = "successfulByteUnitIntegerConverterTestCases")
+    public void testSuccessfulByteUnitIntegerConverter(String input, int 
expected) throws Exception {
         ByteUnitToIntegerConverter converter = new 
ByteUnitToIntegerConverter();
-        // Since the converter returns an Integer, we need to cast expected to 
int
-        assertEquals(converter.convert(input), Integer.valueOf((int) 
expected));
+        assertEquals(converter.convert(input), Integer.valueOf(expected));
     }
 
     @Test(dataProvider = "failingByteUnitUtilTestCases")
@@ -88,5 +111,10 @@ public class ByteConversionTest {
         ByteUnitToIntegerConverter converter = new 
ByteUnitToIntegerConverter();
         assertThrows(TypeConversionException.class, () -> 
converter.convert(input));
     }
-}
 
+    @Test(dataProvider = "overflowingByteUnitIntegerConverterTestCases")
+    public void testOverflowingByteUnitIntegerConverter(String input) {
+        ByteUnitToIntegerConverter converter = new 
ByteUnitToIntegerConverter();
+        assertThrows(TypeConversionException.class, () -> 
converter.convert(input));
+    }
+}

Reply via email to