diff --git a/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java index d8c1108906..1f75723c66 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java @@ -28,6 +28,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.PrintStream; import java.util.HashSet; import java.util.Iterator; @@ -54,6 +55,7 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.extractor.XSSFExportToXml; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFMap; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -115,6 +117,13 @@ public class XSSFFileHandler extends SpreadsheetHandler { // and finally ensure that exporting to XML works exportToXML(wb); + // also try to read and write the sheet via SXSSF + try (SXSSFWorkbook swb = new SXSSFWorkbook(wb)) { + try (OutputStream out = NullOutputStream.INSTANCE) { + swb.write(out); + } + } + // this allows to trigger a heap-dump at this point to see which memory is still allocated //HeapDump.dumpHeap("/tmp/poi.hprof", false); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java index 535683be6a..d9f7189446 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java @@ -803,7 +803,12 @@ public class XSSFTable extends POIXMLDocumentPart implements Table { * manually add cells with values of "Column1", "Column2" etc first. */ public void updateHeaders() { - XSSFSheet sheet = (XSSFSheet)getParent(); + final POIXMLDocumentPart parent = getParent(); + if (!(parent instanceof XSSFSheet)) { + throw new IllegalArgumentException("Had unexpected type of parent: " + (parent == null ? "" : parent.getClass())); + } + + XSSFSheet sheet = (XSSFSheet) parent; CellReference ref = getStartCellReference(); if (ref == null) return; diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java index 6af4542525..1f57111839 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java @@ -33,6 +33,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.time.LocalDate; import java.util.Arrays; @@ -725,4 +726,13 @@ public final class TestSXSSFWorkbook extends BaseTestXWorkbook { } } + @Test + void writeBrokenFile() throws IOException { + try (final Workbook wb = _testDataProvider.openSampleWorkbook("clusterfuzz-testcase-minimized-POIXSSFFuzzer-5185049589579776.xlsx")) { + try (OutputStream out = NullOutputStream.INSTANCE) { + assertThrows(IllegalArgumentException.class, + () -> wb.write(out)); + } + } + } } diff --git a/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIXSSFFuzzer-5185049589579776.xlsx b/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIXSSFFuzzer-5185049589579776.xlsx new file mode 100644 index 0000000000..b14f41fa57 Binary files /dev/null and b/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIXSSFFuzzer-5185049589579776.xlsx differ diff --git a/test-data/spreadsheet/stress.xls b/test-data/spreadsheet/stress.xls index eeecaeb553..b4c297fe28 100644 Binary files a/test-data/spreadsheet/stress.xls and b/test-data/spreadsheet/stress.xls differ