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.git


The following commit(s) were added to refs/heads/trunk by this push:
     new dbf468f25e more int overflow possibilities (#1102)
dbf468f25e is described below

commit dbf468f25e8ce2057b844d358d6e0803570874e3
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Jun 1 23:50:10 2026 +0100

    more int overflow possibilities (#1102)
---
 .../java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java | 8 ++++----
 .../java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java     | 3 ++-
 .../org/apache/poi/poifs/crypt/standard/StandardEncryptor.java    | 2 +-
 poi/src/main/java/org/apache/poi/poifs/filesystem/BlockStore.java | 2 +-
 .../main/java/org/apache/poi/poifs/filesystem/POIFSDocument.java  | 2 +-
 .../java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java     | 2 +-
 .../main/java/org/apache/poi/poifs/property/PropertyTable.java    | 2 +-
 poi/src/main/java/org/apache/poi/util/HexDump.java                | 2 +-
 poi/src/main/java/org/apache/poi/util/Units.java                  | 7 ++++---
 9 files changed, 16 insertions(+), 14 deletions(-)

diff --git 
a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java 
b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java
index e75e598e66..9a64146440 100644
--- a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java
+++ b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java
@@ -57,7 +57,7 @@ public abstract class ChunkedCipherInputStream extends 
LittleEndianInputStream {
         this.chunk = IOUtils.safelyAllocate(cs, 
CryptoFunctions.MAX_RECORD_LENGTH);
         this.plain = IOUtils.safelyAllocate(cs, 
CryptoFunctions.MAX_RECORD_LENGTH);
         this.chunkBits = Integer.bitCount(chunk.length-1);
-        this.lastIndex = (int)(pos >> chunkBits);
+        this.lastIndex = Math.toIntExact(pos >> chunkBits);
         this.cipher = initCipherForBlock(null, lastIndex);
     }
 
@@ -111,7 +111,7 @@ public abstract class ChunkedCipherInputStream extends 
LittleEndianInputStream {
             }
             count = Math.min(avail, Math.min(count, len));
 
-            System.arraycopy(readPlain ? plain : chunk, (int)(pos & 
chunkMask), b, off, count);
+            System.arraycopy(readPlain ? plain : chunk, Math.toIntExact(pos & 
chunkMask), b, off, count);
 
             off += count;
             len -= count;
@@ -148,7 +148,7 @@ public abstract class ChunkedCipherInputStream extends 
LittleEndianInputStream {
      * @return the remaining byte until EOF
      */
     private int remainingBytes() {
-        return (int)(size - pos);
+        return Math.toIntExact(size - pos);
     }
 
     @Override
@@ -172,7 +172,7 @@ public abstract class ChunkedCipherInputStream extends 
LittleEndianInputStream {
 
     private void nextChunk() throws GeneralSecurityException, IOException {
         if (chunkSize != -1) {
-            int index = (int) (pos >> chunkBits);
+            int index = Math.toIntExact(pos >> chunkBits);
             initCipherForBlock(cipher, index);
 
             if (lastIndex != index) {
diff --git 
a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java 
b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java
index a6881bc5bc..5aa8885958 100644
--- a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java
+++ b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java
@@ -49,6 +49,7 @@ import org.apache.poi.poifs.crypt.HashAlgorithm;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.MathUtil;
 
 /**
  * Decryptor implementation for Agile Encryption
@@ -180,7 +181,7 @@ public class AgileDecryptor extends Decryptor {
     }
 
     protected static int getNextBlockSize(int inputLen, int blockSize) {
-        return (int)Math.ceil(inputLen / (double)blockSize) * blockSize;
+        return MathUtil.safeDoubleToInt(Math.ceil(inputLen / 
(double)blockSize) * blockSize);
     }
 
     /* package */ static byte[] hashInput(AgileEncryptionVerifier ver, byte[] 
pwHash, byte[] blockKey, byte[] inputKey, int cipherMode) {
diff --git 
a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java 
b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java
index 1fd68da8f0..ca91095ea0 100644
--- 
a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java
+++ 
b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java
@@ -181,7 +181,7 @@ public class StandardEncryptor extends Encryptor {
         }
 
         void writeToPOIFS() throws IOException {
-            int oleStreamSize = 
(int)(fileOut.length()+LittleEndianConsts.LONG_SIZE);
+            int oleStreamSize = 
Math.toIntExact(fileOut.length()+LittleEndianConsts.LONG_SIZE);
             dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, this);
             // TODO: any properties???
         }
diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/BlockStore.java 
b/poi/src/main/java/org/apache/poi/poifs/filesystem/BlockStore.java
index 178ff3aa8b..0484c90117 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/BlockStore.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/BlockStore.java
@@ -90,7 +90,7 @@ public abstract class BlockStore {
            }
 
           int blkSize = getBlockStoreBlockSize();
-          int numBlocks = (int)(rawSize / blkSize);
+          int numBlocks = Math.toIntExact(rawSize / blkSize);
           if ((rawSize % blkSize) != 0) {
               numBlocks++;
           }
diff --git 
a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSDocument.java 
b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSDocument.java
index e25d4b8fae..a8d3bd7d10 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSDocument.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSDocument.java
@@ -140,7 +140,7 @@ public final class POIFSDocument implements POIFSViewable, 
Iterable<ByteBuffer>
             length = IOUtils.copy(bis, os);
 
             // Pad to the end of the block with -1s
-            int usedInBlock = (int) (length % _block_size);
+            int usedInBlock = Math.toIntExact(length % _block_size);
             if (usedInBlock != 0 && usedInBlock != _block_size) {
                 int toBlockEnd = _block_size - usedInBlock;
                 byte[] padding = IOUtils.safelyAllocate(toBlockEnd, 
POIFSFileSystem.getMaxRecordLength());
diff --git 
a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java 
b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
index 2aef6d819f..9d373d2448 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
@@ -339,7 +339,7 @@ public class POIFSFileSystem extends BlockStore
             // don't allow huge allocations with invalid header-values
             IOUtils.safelyAllocateCheck(maxSize, MAX_ALLOCATION_SIZE);
 
-            ByteBuffer data = ByteBuffer.allocate((int) maxSize);
+            ByteBuffer data = ByteBuffer.allocate(Math.toIntExact(maxSize));
 
             // Copy in the header
             headerBuffer.position(0);
diff --git a/poi/src/main/java/org/apache/poi/poifs/property/PropertyTable.java 
b/poi/src/main/java/org/apache/poi/poifs/property/PropertyTable.java
index 562413e0c6..5af0725d17 100644
--- a/poi/src/main/java/org/apache/poi/poifs/property/PropertyTable.java
+++ b/poi/src/main/java/org/apache/poi/poifs/property/PropertyTable.java
@@ -181,7 +181,7 @@ public final class PropertyTable implements BATManaged {
     public int countBlocks() {
        long rawSize = _properties.size() * (long)POIFSConstants.PROPERTY_SIZE;
        int blkSize = _bigBigBlockSize.getBigBlockSize();
-       int numBlocks = (int)(rawSize / blkSize);
+       int numBlocks = Math.toIntExact(rawSize / blkSize);
        if ((rawSize % blkSize) != 0) {
            numBlocks++;
        }
diff --git a/poi/src/main/java/org/apache/poi/util/HexDump.java 
b/poi/src/main/java/org/apache/poi/util/HexDump.java
index a9a48c6836..f3ce580e4e 100644
--- a/poi/src/main/java/org/apache/poi/util/HexDump.java
+++ b/poi/src/main/java/org/apache/poi/util/HexDump.java
@@ -294,7 +294,7 @@ public final class HexDump {
         char[] buf = new char[nDigits];
         long acc = value;
         for(int i=nDigits-1; i>=0; i--) {
-            int digit = (int) (acc & 0x0F);
+            int digit = Math.toIntExact(acc & 0x0F);
             buf[i] = (char) (digit < 10 ? ('0' + digit) : ('A' + digit - 10));
             acc >>>= 4;
         }
diff --git a/poi/src/main/java/org/apache/poi/util/Units.java 
b/poi/src/main/java/org/apache/poi/util/Units.java
index bf8b1ccbc0..a7132ff81c 100644
--- a/poi/src/main/java/org/apache/poi/util/Units.java
+++ b/poi/src/main/java/org/apache/poi/util/Units.java
@@ -20,6 +20,7 @@ import java.awt.geom.Dimension2D;
 import java.awt.geom.Rectangle2D;
 
 import static org.apache.poi.util.MathUtil.safeDoubleToInt;
+import static org.apache.poi.util.MathUtil.safeFloatToInt;
 
 public class Units {
     /**
@@ -70,7 +71,7 @@ public class Units {
      * One character is defined as the widest value for the integers 0-9 in the
      * default font.
      */
-    public static final int EMU_PER_CHARACTER = (int) (EMU_PER_PIXEL * 
DEFAULT_CHARACTER_WIDTH);
+    public static final int EMU_PER_CHARACTER = safeFloatToInt(EMU_PER_PIXEL * 
DEFAULT_CHARACTER_WIDTH);
 
     /**
      * Converts points to EMUs
@@ -87,7 +88,7 @@ public class Units {
      * @return EMUs
      */
     public static int pixelToEMU(int pixels) {
-        return pixels*EMU_PER_PIXEL;
+        return Math.multiplyExact(pixels, EMU_PER_PIXEL);
     }
 
     /**
@@ -184,7 +185,7 @@ public class Units {
     }
 
     public static int charactersToEMU(double characters) {
-        return (int) characters * EMU_PER_CHARACTER;
+        return MathUtil.safeDoubleToInt(characters * EMU_PER_CHARACTER);
     }
 
     /**


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

Reply via email to