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 a8511f45b2 POIFS: reject negative block offsets in 
ChainLoopDetector.claim (#1550)
a8511f45b2 is described below

commit a8511f45b2ea4a7d0955022a258a6c366c5d4a6a
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Nov 9 07:50:14 2026 +0100

    POIFS: reject negative block offsets in ChainLoopDetector.claim (#1550)
    
    * POIFS: reject negative block offsets in ChainLoopDetector.claim
    
    claim() only checked the upper bound, so a negative sector, such as
    a negative FAT sector entry in the header, caused an
    ArrayIndexOutOfBoundsException. Throw an IllegalStateException
    instead, like the existing loop check.
    
    Fixes #1548
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * Update expected exceptions for negative block offsets
    
    Two fuzzer files now fail with "Invalid block offset" instead of an
    ArrayIndexOutOfBoundsException.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../apache/poi/poifs/filesystem/BlockStore.java    |  3 +++
 .../poi/poifs/filesystem/TestPOIFSFileSystem.java  | 25 ++++++++++++++++++++++
 test-data/poi-integration-exceptions.csv           |  4 ++--
 3 files changed, 30 insertions(+), 2 deletions(-)

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 d9b1106048..4826f31e01 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
@@ -107,6 +107,9 @@ public abstract class BlockStore {
           used_blocks = new boolean[numBlocks];
        }
        protected void claim(int offset) {
+          if(offset < 0) {
+             throw new IllegalStateException("Invalid block offset " + offset);
+          }
           if(offset >= used_blocks.length) {
              // They're writing, and have had new blocks requested
              //  for the write to proceed. That means they're into
diff --git 
a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java 
b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
index 3667a232e0..0726759786 100644
--- a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
+++ b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
@@ -38,9 +38,11 @@ import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.poifs.storage.BATBlock;
 import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.LittleEndian;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
 
 /**
  * Tests for the POIFSFileSystem
@@ -168,6 +170,29 @@ final class TestPOIFSFileSystem {
         }
     }
 
+    /**
+     * A negative sector in the header's list of FAT sectors is rejected
+     *  (GitHub #1548)
+     */
+    @ParameterizedTest
+    @ValueSource(ints = { Integer.MIN_VALUE, POIFSConstants.UNUSED_BLOCK, 
POIFSConstants.FAT_SECTOR_BLOCK })
+    void testNegativeFATSector(int fatSector) throws Exception {
+        byte[] data;
+        try (POIFSFileSystem fs = new POIFSFileSystem()) {
+            UnsynchronizedByteArrayOutputStream baos = 
UnsynchronizedByteArrayOutputStream.builder().get();
+            fs.writeFilesystem(baos);
+            data = baos.toByteArray();
+        }
+        // First entry of the FAT sector list in the header
+        LittleEndian.putInt(data, 0x4C, fatSector);
+
+        IllegalStateException ex = assertThrows(
+            IllegalStateException.class,
+            () -> new POIFSFileSystem(new ByteArrayInputStream(data))
+        );
+        assertContains(ex.getMessage(), "Invalid block offset " + fatSector);
+    }
+
     /**
      * Tests that we can write and read a file that contains XBATs
      *  as well as regular BATs.
diff --git a/test-data/poi-integration-exceptions.csv 
b/test-data/poi-integration-exceptions.csv
index 12ffa1d3a8..6fa65df22d 100644
--- a/test-data/poi-integration-exceptions.csv
+++ b/test-data/poi-integration-exceptions.csv
@@ -181,7 +181,7 @@ 
document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-4947285593948160.doc,handl
 
document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-4947285593948160.doc,"handle,additional",HPSF,,java.util.NoSuchElementException,Can't
 read past the end of the stream,
 
document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5440721166139392.doc,"handle,extract",HWPF,,java.lang.IllegalArgumentException,Had
 unexpected type of entry for name,
 
document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5440721166139392.doc,extract,HPSF,,java.lang.IllegalArgumentException,Had
 unexpected type of entry for name,
-document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5440721166139392.doc,"handle,additional",HPSF,,java.lang.ArrayIndexOutOfBoundsException,,
+document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5440721166139392.doc,"handle,additional",HPSF,,java.lang.IllegalStateException,Invalid
 block offset,
 
slideshow/clusterfuzz-testcase-minimized-POIHSLFFuzzer-6710128412590080.ppt,"handle,extract",HSLF,,java.lang.IllegalArgumentException,Had
 unexpected type of entry for name,
 
slideshow/clusterfuzz-testcase-minimized-POIHSLFFuzzer-6710128412590080.ppt,extract,HPSF,,java.lang.IllegalArgumentException,Had
 unexpected type of entry for name,
 
slideshow/clusterfuzz-testcase-minimized-POIHSLFFuzzer-6710128412590080.ppt,additional,HPSF,,java.lang.IllegalStateException,Buffer
 overrun,
@@ -307,7 +307,7 @@ 
spreadsheet/clusterfuzz-testcase-minimized-POIXSSFFuzzer-4828727001088000.xlsx,h
 
spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls,handle,HPSF,,java.lang.IndexOutOfBoundsException,,
 
spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls,additional,"HSSF,HPSF",,java.lang.IndexOutOfBoundsException,,
 
document/clusterfuzz-testcase-minimized-POIXWPFFuzzer-6120975439364096.docx,"handle,extract","XWPF,OPC",,java.io.IOException,has
 more data than its declared size,
-spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-5816431116615680.xls,"handle,additional",HPSF,,java.lang.ArrayIndexOutOfBoundsException,,
+spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-5816431116615680.xls,"handle,additional",HPSF,,java.lang.IllegalStateException,Invalid
 block offset,
 
spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-5816431116615680.xls,additional,HSSF,,org.apache.poi.util.RecordFormatException,Not
 enough data,
 
spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-5816431116615680.xls,handle,HSSF,,java.lang.IllegalArgumentException,Sheet
 index,
 
diagram/clusterfuzz-testcase-minimized-POIVisioFuzzer-4513310052515840.vsdx,extract,"XDGF,OPC",,java.io.IOException,org.apache.poi.ooxml.POIXMLException,


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

Reply via email to