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 1fcdff6c2a72f6e5b70ae7c2a2f00c9968256c5c
Author: Dominik Stadler <[email protected]>
AuthorDate: Sun Nov 1 10:20:29 2026 +0100

    Check for proper record type
    
    Avoid a potential ClassCastException which can be triggered
    via broken input
    
    Fixes https://issues.oss-fuzz.com/issues/42535450
---
 .../apache/poi/hssf/model/InternalWorkbook.java    |  5 ++
 .../poi/hssf/model/TestInternalWorkbook.java       | 84 ++++++++++++++++++++++
 2 files changed, 89 insertions(+)

diff --git a/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java 
b/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java
index 426c873e4b..31cfcb1841 100644
--- a/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java
+++ b/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java
@@ -804,6 +804,11 @@ public final class InternalWorkbook {
 
         Record rec = records.get(records.getTabpos());
 
+        if (!(rec instanceof TabIdRecord)) {
+            throw new IllegalStateException("Invalid tab-pos " + 
records.getTabpos() +
+                    ", found wrong type of record: " + rec);
+        }
+
         TabIdRecord tir = ( TabIdRecord ) rec;
         short[]     tia = new short[ boundsheets.size() ];
 
diff --git 
a/poi/src/test/java/org/apache/poi/hssf/model/TestInternalWorkbook.java 
b/poi/src/test/java/org/apache/poi/hssf/model/TestInternalWorkbook.java
new file mode 100644
index 0000000000..cb63cdc016
--- /dev/null
+++ b/poi/src/test/java/org/apache/poi/hssf/model/TestInternalWorkbook.java
@@ -0,0 +1,84 @@
+/* ====================================================================
+   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.poi.hssf.model;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.poi.hssf.record.CountryRecord;
+import org.apache.poi.hssf.record.EOFRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.SSTRecord;
+import org.apache.poi.hssf.record.TabIdRecord;
+import org.junit.jupiter.api.Test;
+
+class TestInternalWorkbook {
+    @Test
+    void testRecordsNoSheet() {
+        List<Record> wbRecords = new ArrayList<>();
+
+        // Finally we need an EoF record
+        wbRecords.add(EOFRecord.instance);
+
+        InternalWorkbook wb = InternalWorkbook.createWorkbook(wbRecords);
+        assertNotNull(wb);
+
+        assertThrows(IllegalStateException.class,
+                () -> wb.setSheetName(0, "test"));
+    }
+
+    @Test
+    void testRecordsWithSheet() {
+        List<Record> wbRecords = new ArrayList<>();
+
+        wbRecords.add(new CountryRecord());
+        wbRecords.add(new SSTRecord());
+        wbRecords.add(new TabIdRecord());
+
+        // Finally we need an EoF record
+        wbRecords.add(EOFRecord.instance);
+
+        InternalWorkbook wb = InternalWorkbook.createWorkbook(wbRecords);
+        assertNotNull(wb);
+
+        wb.setSheetName(0, "test");
+    }
+
+    @Test
+    void testRecordsInvalidTabPos() {
+        List<Record> wbRecords = new ArrayList<>();
+
+        wbRecords.add(new CountryRecord());
+        wbRecords.add(new SSTRecord());
+        wbRecords.add(new TabIdRecord());
+
+        // Finally we need an EoF record
+        wbRecords.add(EOFRecord.instance);
+
+        InternalWorkbook wb = InternalWorkbook.createWorkbook(wbRecords);
+        assertNotNull(wb);
+
+        wb.getWorkbookRecordList().setTabpos(1);
+
+        assertThrows(IllegalStateException.class,
+                () -> wb.setSheetName(0, "test"));
+    }
+}
\ No newline at end of file


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

Reply via email to