You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by st...@apache.org on 2026/07/14 20:29:38 UTC

(johnzon) branch master updated: adding more checks for numbers with huge exponents

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

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new 08884bbe adding more checks for numbers with huge exponents
08884bbe is described below

commit 08884bbefba1d2f5c4670d610b22ce670bbfdd6c
Author: Mark Struberg <st...@apache.org>
AuthorDate: Tue Jul 14 22:28:28 2026 +0200

    adding more checks for numbers with huge exponents
    
    based on the original report by fanningpj - txs!
---
 .../org/apache/johnzon/core/JsonNumberImpl.java    |  3 ++
 .../apache/johnzon/core/JsonReaderImplTest.java    | 58 ++++++++++++++++++++++
 .../johnzon/mapper/NumberSerializationTest.java    | 38 ++++++++++++++
 3 files changed, 99 insertions(+)

diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
index e5ebebd1..158a1c35 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
@@ -55,6 +55,7 @@ final class JsonNumberImpl implements JsonNumber, Serializable {
 
     @Override
     public int intValueExact() {
+        maxBigDecimalScale.accept(value);
         checkFractionalPart();
         return value.intValueExact();
     }
@@ -66,6 +67,7 @@ final class JsonNumberImpl implements JsonNumber, Serializable {
 
     @Override
     public long longValueExact() {
+        maxBigDecimalScale.accept(value);
         checkFractionalPart();
         return value.longValueExact();
     }
@@ -89,6 +91,7 @@ final class JsonNumberImpl implements JsonNumber, Serializable {
 
     @Override
     public BigDecimal bigDecimalValue() {
+        maxBigDecimalScale.accept(value);
         return value;
     }
 
diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java
index dc5f2436..a84c37f4 100644
--- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java
@@ -25,11 +25,13 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
+import java.math.BigInteger;
 import java.nio.charset.Charset;
 import java.util.Collections;
 import java.util.HashMap;
@@ -144,6 +146,62 @@ public class JsonReaderImplTest {
         assertEquals(json, newJson);
     }
 
+    @Test
+    public void parseBigInteger() {
+        String json = "1e15";
+        JsonValue jsonValue = Json.createReader(new StringReader(json)).readValue();
+        String newJson = jsonValue.toString();
+        assertEquals("1E+15", newJson);
+
+        assertTrue(jsonValue instanceof JsonNumber);
+        final BigInteger bigInteger = ((JsonNumber) jsonValue).bigIntegerValue();
+        assertEquals("1000000000000000", bigInteger.toString());
+    }
+
+
+    @Test
+    public void parseBigIntegerFailOnTooLongExponent() {
+        String json = "1e150000000";
+        JsonValue jsonValue = Json.createReader(new StringReader(json)).readValue();
+        String newJson = jsonValue.toString();
+        assertEquals("1E+150000000", newJson);
+
+        assertTrue(jsonValue instanceof JsonNumber);
+
+        try {
+            ((JsonNumber) jsonValue).bigIntegerValue();
+            fail();
+        } catch (ArithmeticException ae) {
+            // all fine
+        }
+
+        try {
+            ((JsonNumber) jsonValue).bigIntegerValueExact();
+            fail();
+        } catch (ArithmeticException ae) {
+            // all fine
+        }
+
+        final long longVal = ((JsonNumber) jsonValue).longValue();
+        final int intVal = ((JsonNumber) jsonValue).intValue();
+        final double doubleVal = ((JsonNumber) jsonValue).doubleValue();
+
+        try {
+            final long lve = ((JsonNumber) jsonValue).longValueExact();
+            fail();
+        } catch (ArithmeticException ae) {
+            // all fine
+        }
+
+        try {
+            final long lve = ((JsonNumber) jsonValue).intValueExact();
+            fail();
+        } catch (ArithmeticException ae) {
+            // all fine
+        }
+
+    }
+
     /**
      * Test came up during the TCK adoption.
      */
diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/NumberSerializationTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/NumberSerializationTest.java
index e18fa564..4f2b01d7 100644
--- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/NumberSerializationTest.java
+++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/NumberSerializationTest.java
@@ -22,7 +22,9 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import java.io.StringReader;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 
@@ -83,6 +85,42 @@ public class NumberSerializationTest {
                 disabledAdapters.get(new AdapterKey(BigDecimal.class, String.class)));
     }
 
+    @Test
+    public void bigIntegerLongExponent() {
+        String poc = "1e200000000";
+        Mapper mapper = new MapperBuilder().build();
+        {
+            final Object number = mapper.readObject(new StringReader(poc), byte.class);
+            assertNotNull(number);
+        }
+
+        {
+            final Object number = mapper.readObject(new StringReader(poc), long.class);
+            assertNotNull(number);
+        }
+
+        {
+            final Object number = mapper.readObject(new StringReader(poc), int.class);
+            assertNotNull(number);
+        }
+
+        try {
+            final Object number = mapper.readObject(new StringReader(poc), BigInteger.class);
+            fail();
+        } catch (ArithmeticException ae) {
+            // all fine
+        }
+
+        try {
+            final BigDecimal number = mapper.readObject(new StringReader(poc), BigDecimal.class);
+            final String strval = number.toBigIntegerExact().toString();
+            fail();
+        } catch (ArithmeticException ae) {
+            // all fine
+        }
+
+    }
+
     @Test
     public void bigIntegerStringAdapterFlagControlsBigInteger() {
         // Default: BigInteger adapter is ON (useBigIntegerStringAdapter=true)