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

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

commit 5fb3244453688b967aa730fbcfdba9ca76fc9e15
Author: Dominik Stadler <[email protected]>
AuthorDate: Thu Oct 22 14:43:04 2026 +0200

    Switch assert to Exception
    
    Can be triggered by invalid input format, so
    let's report it always
    
    Fixes https://issues.oss-fuzz.com/issues/561968961
---
 .../org/apache/poi/hemf/record/emf/HemfMisc.java   | 17 ++++++--
 .../apache/poi/hemf/record/emf/TestHemfMisc.java   | 49 +++++++++++++++++++++-
 2 files changed, 61 insertions(+), 5 deletions(-)

diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
index 2f7ea45218..6698971c39 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
@@ -115,7 +115,11 @@ public class HemfMisc {
 
             if (nPalEntries > 0 && offPalEntries > 0) {
                 int undefinedSpace1 = offPalEntries - (size + HEADER_SIZE);
-                assert (undefinedSpace1 >= 0);
+                if (undefinedSpace1 < 0) {
+                    throw new IllegalArgumentException("Provided record is 
invalid, offPalEntries: " +
+                            offPalEntries + ", size: " + size + ", 
HEADER_SIZE: " + HEADER_SIZE +
+                            ", undefinedSpace: " + undefinedSpace1);
+                }
                 leis.skipFully(undefinedSpace1);
                 size += undefinedSpace1;
 
@@ -125,7 +129,11 @@ public class HemfMisc {
                 }
 
                 int undefinedSpace2 = (int) (recordSize - size - 
LittleEndianConsts.INT_SIZE);
-                assert (undefinedSpace2 >= 0);
+                if (undefinedSpace2 < 0) {
+                    throw new IllegalArgumentException("Provided record is 
invalid, recordSize: " +
+                            recordSize + ", size: " + size + ", INT_SIZE: " + 
LittleEndianConsts.INT_SIZE +
+                            ", undefinedSpace: " + undefinedSpace2);
+                }
                 leis.skipFully(undefinedSpace2);
                 size += undefinedSpace2;
             }
@@ -137,7 +145,10 @@ public class HemfMisc {
             size += LittleEndianConsts.INT_SIZE;
             // some files store the whole file size in sizeLast, other just 
the last record size
             // assert (sizeLast == size+HEADER_SIZE);
-            assert (recordSize == size);
+            if (recordSize != size) {
+                throw new IllegalArgumentException("Provided record size and 
actual size do not match: " + recordSize +
+                        " and " + size);
+            }
 
             return size;
         }
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfMisc.java 
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfMisc.java
index 23113b36bc..8ed5fb708e 100644
--- 
a/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfMisc.java
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfMisc.java
@@ -17,9 +17,11 @@
 
 package org.apache.poi.hemf.record.emf;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 
 import org.apache.poi.hemf.record.emf.HemfMisc.EmfExtCreatePen;
 import org.apache.poi.util.LittleEndian;
@@ -45,10 +47,53 @@ class TestHemfMisc {
         LittleEndian.putInt(data, 20, 0x7);         // penStyle with 
PS_USERSTYLE line dash
         LittleEndian.putInt(data, 40, 0x40000000);  // numStyleEntries
 
-        EmfExtCreatePen record = new EmfExtCreatePen();
+        EmfExtCreatePen penRecord = new EmfExtCreatePen();
         try (LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data))) {
             assertThrows(RecordFormatException.class,
-                    () -> record.init(leis, (long) data.length, (long) 0x5F));
+                    () -> penRecord.init(leis, (long) data.length, (long) 
0x5F));
         }
     }
+
+    @Test
+    void testInit() throws IOException {
+        HemfMisc.EmfEof misc = new HemfMisc.EmfEof();
+        assertEquals(12, misc.init(new LittleEndianInputStream(new 
ByteArrayInputStream(new byte[] {
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+        })), 12, 0));
+    }
+
+    @Test
+    void testInitWithPal() throws IOException {
+        HemfMisc.EmfEof misc = new HemfMisc.EmfEof();
+        assertEquals(16, misc.init(new LittleEndianInputStream(new 
ByteArrayInputStream(new byte[] {
+                1, 0, 0, 0, 16, 0, 0, 0, 99, 98, 97, 96, 0, 0, 0, 0
+        })), 16, 0));
+    }
+
+    @Test
+    void testInitWithPalInvalid1() {
+        HemfMisc.EmfEof misc = new HemfMisc.EmfEof();
+        assertThrows(IllegalArgumentException.class,
+                () -> misc.init(new LittleEndianInputStream(new 
ByteArrayInputStream(new byte[] {
+                1, 0, 0, 0, 1, 0, 0, 0, 99, 98, 0, 0, 0, 0
+        })), 12, 0));
+    }
+
+    @Test
+    void testInitWithPalInvalid2() {
+        HemfMisc.EmfEof misc = new HemfMisc.EmfEof();
+        assertThrows(IllegalArgumentException.class,
+                () -> misc.init(new LittleEndianInputStream(new 
ByteArrayInputStream(new byte[] {
+                1, 0, 0, 0, 16, 0, 0, 0, 99, 98, 97, 96, 0, 0, 0, 0
+        })), 12, 0));
+    }
+
+    @Test
+    void testInitInvalidSize() {
+        HemfMisc.EmfEof misc = new HemfMisc.EmfEof();
+        assertThrows(IllegalArgumentException.class,
+                () -> misc.init(new LittleEndianInputStream(new 
ByteArrayInputStream(new byte[] {
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+        })), 11, 0));
+    }
 }


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

Reply via email to