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 d8c11089060..1f75723c66c 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 @@ Licensed to the Apache Software Foundation (ASF) under one or more 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 @@ Licensed to the Apache Software Foundation (ASF) under one or more 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 void handleFile(InputStream stream, String path) throws Exception { // 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 535683be6a0..d9f71894462 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 int getColumnCount() { * 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 6af45425255..1f57111839d 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.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 @@ private void testUseSharedStringsTableWithRichText(boolean compressTempFiles) th } } + @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 00000000000..b14f41fa574 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 eeecaeb553f..b4c297fe284 100644 Binary files a/test-data/spreadsheet/stress.xls and b/test-data/spreadsheet/stress.xls differ