diff options
author | Dominik Stadler <centic@apache.org> | 2023-09-07 12:20:28 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2023-09-07 12:20:28 +0000 |
commit | e686e84512ce1518885750899ad543fb32e03253 (patch) | |
tree | 5b38ee982e843f11923806237042f062a6b26da8 | |
parent | 5c2a89412b08c4b71f2ab79b88f26827028916b4 (diff) | |
download | poi-e686e84512ce1518885750899ad543fb32e03253.tar.gz poi-e686e84512ce1518885750899ad543fb32e03253.zip |
Bug 66425: Avoid a ClassCastException found via oss-fuzz
We try to avoid throwing ClassCastException, but it was possible
to trigger one here with a specially crafted input-file
Also add SXSSFWorkbook.write() to integrationtests
Fixes https://oss-fuzz.com/testcase-detail/5185049589579776
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912162 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java | 9 | ||||
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java | 7 | ||||
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java | 10 | ||||
-rw-r--r-- | test-data/spreadsheet/clusterfuzz-testcase-minimized-POIXSSFFuzzer-5185049589579776.xlsx | bin | 0 -> 9053 bytes | |||
-rw-r--r-- | test-data/spreadsheet/stress.xls | bin | 69120 -> 69632 bytes |
5 files changed, 25 insertions, 1 deletions
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 ? "<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 Binary files differnew file mode 100644 index 0000000000..b14f41fa57 --- /dev/null +++ b/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIXSSFFuzzer-5185049589579776.xlsx diff --git a/test-data/spreadsheet/stress.xls b/test-data/spreadsheet/stress.xls Binary files differindex eeecaeb553..b4c297fe28 100644 --- a/test-data/spreadsheet/stress.xls +++ b/test-data/spreadsheet/stress.xls |