Author: fanningpj
Date: Sat Jun  6 12:38:17 2026
New Revision: 1935062

Log:
avoid int overflow

Added:
   xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/MathUtil.java
Modified:
   xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDateBuilder.java
   xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDurationBuilder.java
   xmlbeans/trunk/src/main/java/org/apache/xmlbeans/QNameCache.java
   
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/LongUTFDataInputStream.java
   
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolderEx.java
   
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDateBuilder.java
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDateBuilder.java  Sat Jun 
 6 12:17:46 2026        (r1935061)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDateBuilder.java  Sat Jun 
 6 12:38:17 2026        (r1935062)
@@ -695,7 +695,7 @@ public final class GDateBuilder implemen
                 int temp = _M;
                 _M = _modulo(temp, 1, 13);
                 if (hasYear()) {
-                    _CY = _CY + (int) _fQuotient(temp, 1, 13);
+                    _CY = Math.toIntExact(_CY + _fQuotient(temp, 1, 13));
                 }
             }
         }
@@ -787,7 +787,7 @@ public final class GDateBuilder implemen
             // fix months first
             int temp = _M;
             _M = _modulo(temp, 1, 13);
-            _CY = _CY + (int) _fQuotient(temp, 1, 13);
+            _CY = Math.toIntExact(_CY + _fQuotient(temp, 1, 13));
 
             // then pull days out
             int extradays = _D - 1;
@@ -867,9 +867,10 @@ public final class GDateBuilder implemen
             }
 
             // Add months and years
-            temp = _M + sign * month;
+            temp = Math.addExact(_M, Math.multiplyExact(sign, month));
             _M = _modulo(temp, 1, 13);
-            _CY = _CY + sign * year + (int) _fQuotient(temp, 1, 13);
+            _CY = Math.addExact(_CY, Math.multiplyExact(
+                    sign, Math.toIntExact(year + _fQuotient(temp, 1, 13))));
 
             // In new month, day may need to be pegged before proceeding
             if (hasDay()) {
@@ -1136,7 +1137,7 @@ public final class GDateBuilder implemen
      * modulo(a, b) = a - fQuotient(a,b)*b
      */
     private static int _mod(long a, int b, long quotient) {
-        return (int) (a - quotient * b);
+        return Math.toIntExact(a - quotient * b);
     }
 
     /**

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDurationBuilder.java
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDurationBuilder.java      
Sat Jun  6 12:17:46 2026        (r1935061)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/GDurationBuilder.java      
Sat Jun  6 12:38:17 2026        (r1935062)
@@ -322,7 +322,7 @@ public class GDurationBuilder implements
      * modulo(a, b) = a - fQuotient(a,b)*b
      */
     private static int _mod(long a, int b, long quotient) {
-        return (int) (a - quotient * b);
+        return Math.toIntExact(a - quotient * b);
     }
 
 

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/QNameCache.java
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/QNameCache.java    Sat Jun 
 6 12:17:46 2026        (r1935061)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/QNameCache.java    Sat Jun 
 6 12:38:17 2026        (r1935062)
@@ -15,6 +15,8 @@
 
 package org.apache.xmlbeans;
 
+import org.apache.xmlbeans.impl.util.MathUtil;
+
 import javax.xml.namespace.QName;
 
 /**
@@ -47,7 +49,7 @@ public final class QNameCache
     
         this.loadFactor = loadFactor;
         this.hashmask = capacity - 1;
-        threshold = (int)(capacity * loadFactor);
+        threshold = MathUtil.safeFloatToInt(capacity * loadFactor);
         table = new QName[capacity];
     }
 
@@ -126,7 +128,7 @@ public final class QNameCache
 
         table = newTable;
         hashmask = newHashmask;
-        threshold = (int) (newLength * loadFactor);
+        threshold = MathUtil.safeFloatToInt(newLength * loadFactor);
     }
     
     private static int hash(String uri, String localName, String prefix)

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/LongUTFDataInputStream.java
==============================================================================
--- 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/LongUTFDataInputStream.java
      Sat Jun  6 12:17:46 2026        (r1935061)
+++ 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/LongUTFDataInputStream.java
      Sat Jun  6 12:38:17 2026        (r1935062)
@@ -68,7 +68,7 @@ public class LongUTFDataInputStream exte
 
         final int[] readLen = { 0 }, readBuf = { 0 }, fillBuf = { 0 };
         while (readLen[0] < utfLen) {
-            int c = (int)give.onebyte(readBuf, fillBuf, readLen) & 0xff;
+            int c = Math.toIntExact(give.onebyte(readBuf, fillBuf, readLen) & 
0xff);
             switch (c >> 4) {
                 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                     /* 0xxxxxxx*/

Added: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/MathUtil.java
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/MathUtil.java    
Sat Jun  6 12:38:17 2026        (r1935062)
@@ -0,0 +1,67 @@
+/* ====================================================================
+   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.
+==================================================================== */
+
+/* ====================================================================
+   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.xmlbeans.impl.util;
+
+/**
+ * Utility methods for dealing with conversions
+ */
+public class MathUtil {
+    private MathUtil() {}
+
+    public static int safeFloatToInt(float f) {
+        if (Float.isNaN(f)) {
+            throw new IllegalArgumentException("Cannot convert NaN to int");
+        }
+        if (Float.isInfinite(f)) {
+            throw new IllegalArgumentException("Cannot convert infinity to 
int");
+        }
+        if (f > Integer.MAX_VALUE || f < Integer.MIN_VALUE) {
+            throw new IllegalArgumentException("Value out of range: " + f);
+        }
+        return (int) f;
+    }
+
+    public static int safeDoubleToInt(double d) {
+        if (Double.isNaN(d)) {
+            throw new IllegalArgumentException("Cannot convert NaN to int");
+        }
+        if (Double.isInfinite(d)) {
+            throw new IllegalArgumentException("Cannot convert infinity to 
int");
+        }
+        if (d > Integer.MAX_VALUE || d < Integer.MIN_VALUE) {
+            throw new IllegalArgumentException("Value out of range: " + d);
+        }
+        return (int) d;
+    }
+}

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolderEx.java
==============================================================================
--- 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolderEx.java
   Sat Jun  6 12:17:46 2026        (r1935061)
+++ 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolderEx.java
   Sat Jun  6 12:38:17 2026        (r1935062)
@@ -154,7 +154,7 @@ public abstract class JavaIntHolderEx ex
             case SchemaType.SIZE_BIG_INTEGER:
                 return ((XmlObjectBase) o).getBigIntegerValue().intValue();
             case SchemaType.SIZE_LONG:
-                return (int) ((XmlObjectBase) o).getLongValue();
+                return Math.toIntExact(((XmlObjectBase) o).getLongValue());
             default:
                 return ((XmlObjectBase) o).getIntValue();
         }

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java
==============================================================================
--- 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java 
    Sat Jun  6 12:17:46 2026        (r1935061)
+++ 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java 
    Sat Jun  6 12:38:17 2026        (r1935062)
@@ -15,6 +15,7 @@
 
 package org.apache.xmlbeans.impl.values;
 
+import net.sf.saxon.expr.Component;
 import org.apache.xmlbeans.*;
 import org.apache.xmlbeans.impl.common.*;
 import org.apache.xmlbeans.impl.schema.SchemaTypeImpl;
@@ -1379,7 +1380,7 @@ public abstract class XmlObjectBase impl
         if (l < Integer.MIN_VALUE) {
             throw new XmlValueOutOfRangeException();
         }
-        return (int) l;
+        return Math.toIntExact(l);
     }
 
     private static final BigInteger _max = BigInteger.valueOf(Long.MAX_VALUE);


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

Reply via email to