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

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 8361fa4f count the zeros of a negative scale in the decimal 
totalDigits check (#154)
8361fa4f is described below

commit 8361fa4fe172f5d1634194b5cfdaaaec45ac8819
Author: PJ Fanning <[email protected]>
AuthorDate: Tue Nov 3 11:21:11 2026 +0100

    count the zeros of a negative scale in the decimal totalDigits check (#154)
    
    A BigDecimal set from Java code can carry a negative scale: 1E+5 has
    unscaled value 1 and scale -5, but is 100000, six digits. The check
    counted only the unscaled digits, so it passed totalDigits=3.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../xmlbeans/impl/values/JavaDecimalHolderEx.java  |  6 ++++++
 .../checkin/TotalDigitsFractionValidateTest.java   | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git 
a/src/main/java/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java 
b/src/main/java/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java
index bec3d5cc..003d3640 100644
--- a/src/main/java/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java
+++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java
@@ -141,6 +141,12 @@ public abstract class JavaDecimalHolderEx extends 
JavaDecimalHolder {
                 if (v.signum() != 0 && fractionDigits > len) {
                     len = fractionDigits;
                 }
+
+                // a negative scale stands for trailing zeros of the integer 
part:
+                // 1E+5 has unscaled value 1 but is the six digit 100000
+                if (v.signum() != 0 && vScale < 0) {
+                    len -= vScale;
+                }
             }
 
             if (len > tdf) {
diff --git a/src/test/java/misc/checkin/TotalDigitsFractionValidateTest.java 
b/src/test/java/misc/checkin/TotalDigitsFractionValidateTest.java
index 91f65484..4ba47d5f 100644
--- a/src/test/java/misc/checkin/TotalDigitsFractionValidateTest.java
+++ b/src/test/java/misc/checkin/TotalDigitsFractionValidateTest.java
@@ -16,20 +16,25 @@
 package misc.checkin;
 
 import org.apache.xmlbeans.SchemaTypeLoader;
+import org.apache.xmlbeans.SimpleValue;
 import org.apache.xmlbeans.XmlBeans;
 import org.apache.xmlbeans.XmlError;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException;
 import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
+import javax.xml.namespace.QName;
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class TotalDigitsFractionValidateTest {
 
@@ -90,4 +95,21 @@ public class TotalDigitsFractionValidateTest {
         assertInvalid("1234");
         assertInvalid("1.234");
     }
+
+    private static void setValue(BigDecimal value) throws Exception {
+        XmlObject doc = loader.parse("<t:d xmlns:t='urn:t'>1</t:d>", null,
+            new XmlOptions().setValidateOnSet());
+        SimpleValue d = (SimpleValue) doc.selectChildren(new QName("urn:t", 
"d"))[0];
+        d.setBigDecimalValue(value);
+    }
+
+    @Test
+    void negativeScaleCountsIntegerZeros() throws Exception {
+        // 1E+5 has unscaled value 1 and scale -5, but is 100000: six digits
+        assertThrows(XmlValueOutOfRangeException.class, () -> setValue(new 
BigDecimal("1E+5")));
+        assertThrows(XmlValueOutOfRangeException.class, () -> setValue(new 
BigDecimal("-12E+2")));
+        setValue(new BigDecimal("1E+2"));
+        setValue(new BigDecimal("12E+1"));
+        setValue(new BigDecimal("0E+5"));
+    }
 }


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

Reply via email to