Browse Source

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
tags/REL_5_2_4
Dominik Stadler 8 months ago
parent
commit
e686e84512

+ 9
- 0
poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java View File

import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.extractor.XSSFExportToXml; 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.XSSFMap;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
// and finally ensure that exporting to XML works // and finally ensure that exporting to XML works
exportToXML(wb); 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 // this allows to trigger a heap-dump at this point to see which memory is still allocated
//HeapDump.dumpHeap("/tmp/poi.hprof", false); //HeapDump.dumpHeap("/tmp/poi.hprof", false);



+ 6
- 1
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java View File

* manually add cells with values of "Column1", "Column2" etc first. * manually add cells with values of "Column1", "Column2" etc first.
*/ */
public void updateHeaders() { 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(); CellReference ref = getStartCellReference();
if (ref == null) return; if (ref == null) return;



+ 10
- 0
poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java View File

import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;


} }
} }


@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));
}
}
}
} }

BIN
test-data/spreadsheet/clusterfuzz-testcase-minimized-POIXSSFFuzzer-5185049589579776.xlsx View File


BIN
test-data/spreadsheet/stress.xls View File


Loading…
Cancel
Save