This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git
The following commit(s) were added to refs/heads/master by this push:
new 8260c190 Reject out-of-range values in LongLocaleConverter (#406)
8260c190 is described below
commit 8260c1902841107e6c2f698f3bcad6926c83f4e2
Author: Dexter.k <[email protected]>
AuthorDate: Wed Jul 1 23:45:59 2026 +0000
Reject out-of-range values in LongLocaleConverter (#406)
* reject out-of-range values in LongLocaleConverter
The parse method narrowed the result with Long.valueOf(result.longValue())
and had no range check, so a value beyond long range was silently clamped to
Long.MAX_VALUE instead of throwing. Add the bounds check, mirroring the sibling
Integer/Byte/Short/Float locale converters.
* use assertThrows in LongLocaleConverter range test
Signed-off-by: Naveed Khan <[email protected]>
---------
Signed-off-by: Naveed Khan <[email protected]>
---
.../locale/converters/LongLocaleConverter.java | 5 +++++
.../converters/LongLocaleConverterTest.java | 19 +++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git
a/src/main/java/org/apache/commons/beanutils2/locale/converters/LongLocaleConverter.java
b/src/main/java/org/apache/commons/beanutils2/locale/converters/LongLocaleConverter.java
index 918222e5..855a5aff 100644
---
a/src/main/java/org/apache/commons/beanutils2/locale/converters/LongLocaleConverter.java
+++
b/src/main/java/org/apache/commons/beanutils2/locale/converters/LongLocaleConverter.java
@@ -78,6 +78,11 @@ public class LongLocaleConverter extends
DecimalLocaleConverter<Long> {
return (Long) result;
}
+ final double doubleValue = result.doubleValue();
+ if (doubleValue < Long.MIN_VALUE || doubleValue > Long.MAX_VALUE) {
+ throw new ConversionException("Supplied number is not of type
Long: " + result);
+ }
+
return Long.valueOf(result.longValue());
}
}
diff --git
a/src/test/java/org/apache/commons/beanutils2/converters/LongLocaleConverterTest.java
b/src/test/java/org/apache/commons/beanutils2/converters/LongLocaleConverterTest.java
index f1c4f5e2..dac385f3 100644
---
a/src/test/java/org/apache/commons/beanutils2/converters/LongLocaleConverterTest.java
+++
b/src/test/java/org/apache/commons/beanutils2/converters/LongLocaleConverterTest.java
@@ -17,6 +17,12 @@
package org.apache.commons.beanutils2.converters;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.text.DecimalFormat;
+
+import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.locale.converters.LongLocaleConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -189,4 +195,17 @@ class LongLocaleConverterTest extends
AbstractLocaleConverterTest<Long> {
convertInvalid(converter, "(C)", defaultValue);
convertNull(converter, "(C)", defaultValue);
}
+
+ /**
+ * Test Long limits
+ */
+ @Test
+ void testLongLimits() {
+ converter =
LongLocaleConverter.builder().setLocale(defaultLocale).get();
+ final DecimalFormat fmt = new DecimalFormat("#");
+ assertEquals(Long.valueOf(Long.MAX_VALUE),
converter.convert(fmt.format(Long.MAX_VALUE)), "Long.MAX_VALUE");
+ assertEquals(Long.valueOf(Long.MIN_VALUE),
converter.convert(fmt.format(Long.MIN_VALUE)), "Long.MIN_VALUE");
+ assertThrows(ConversionException.class, () ->
converter.convert("99999999999999999999"));
+ assertThrows(ConversionException.class, () ->
converter.convert("-99999999999999999999"));
+ }
}