From 1ff1e84e4afcd4abdf454c584a909423c2a14b03 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 3 Jan 2023 19:52:03 +0000 Subject: Avoid some NullPointerException and ClassCastExceptions found when fuzzing Apache POI This mostly only makes thrown runtime-exceptions a bit more consistent and improves information in exceptions. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1906360 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/ddf/EscherBSERecord.java | 7 ++- .../poi/hssf/extractor/OldExcelExtractor.java | 17 +++++--- .../apache/poi/hssf/model/InternalWorkbook.java | 6 ++- .../hssf/record/aggregates/CFRecordsAggregate.java | 6 ++- .../apache/poi/hssf/usermodel/HSSFPatriarch.java | 50 ++++++++++++++-------- .../poi/hssf/usermodel/HSSFShapeFactory.java | 18 ++++++-- .../apache/poi/poifs/property/PropertyTable.java | 8 +++- 7 files changed, 81 insertions(+), 31 deletions(-) (limited to 'poi') diff --git a/poi/src/main/java/org/apache/poi/ddf/EscherBSERecord.java b/poi/src/main/java/org/apache/poi/ddf/EscherBSERecord.java index 255fa212ec..4e37209115 100644 --- a/poi/src/main/java/org/apache/poi/ddf/EscherBSERecord.java +++ b/poi/src/main/java/org/apache/poi/ddf/EscherBSERecord.java @@ -111,8 +111,13 @@ public final class EscherBSERecord extends EscherRecord { int bytesRead = 0; if (bytesRemaining > 0) { + EscherRecord record = recordFactory.createRecord(data, pos + 36); + if (!(record instanceof EscherBlipRecord)) { + throw new IllegalArgumentException("Did not have a EscherBlipRecord: " + record); + } + // Some older escher formats skip this last record - field_12_blipRecord = (EscherBlipRecord) recordFactory.createRecord( data, pos + 36 ); + field_12_blipRecord = (EscherBlipRecord) record; bytesRead = field_12_blipRecord.fillFields( data, pos + 36, recordFactory ); } pos += 36 + bytesRead; diff --git a/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java b/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java index ac1ca9541c..f6deb15a10 100644 --- a/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java +++ b/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java @@ -44,6 +44,7 @@ import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentNode; +import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.poifs.filesystem.NotOLE2FileException; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -149,14 +150,18 @@ public class OldExcelExtractor implements POITextExtractor { private void open(DirectoryNode directory) throws IOException { DocumentNode book; try { - book = (DocumentNode)directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME); + Entry entry = directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME); + if (!(entry instanceof DocumentNode)) { + throw new IllegalArgumentException("Did not have an Excel 5/95 Book stream: " + entry); + } + book = (DocumentNode) entry; } catch (FileNotFoundException | IllegalArgumentException e) { // some files have "Workbook" instead - book = (DocumentNode)directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES.get(0)); - } - - if (book == null) { - throw new IOException("No Excel 5/95 Book stream found"); + Entry entry = directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES.get(0)); + if (!(entry instanceof DocumentNode)) { + throw new IllegalArgumentException("Did not have an Excel 5/95 Book stream: " + entry); + } + book = (DocumentNode) entry; } ris = new RecordInputStream(directory.createDocumentInputStream(book)); 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 7ca3b143ee..003ad45c6b 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 @@ -830,7 +830,11 @@ public final class InternalWorkbook { xfptr += index; - return ( ExtendedFormatRecord ) records.get(xfptr); + Record record = records.get(xfptr); + if (!(record instanceof ExtendedFormatRecord)) { + throw new IllegalStateException("Did not have a ExtendedFormatRecord: " + record); + } + return (ExtendedFormatRecord) record; } /** diff --git a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java index bcf80e6189..bbfe353873 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java @@ -125,7 +125,11 @@ public final class CFRecordsAggregate extends RecordAggregate implements Generic CFRuleBase[] rules = new CFRuleBase[nRules]; for (int i = 0; i < rules.length; i++) { - rules[i] = (CFRuleBase) rs.getNext(); + Record record = rs.getNext(); + if (!(record instanceof CFRuleBase)) { + throw new IllegalArgumentException("Did not have a CFRuleBase: " + record); + } + rules[i] = (CFRuleBase) record; } return new CFRecordsAggregate(header, rules); diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java index 45ac29a9eb..876b6f245a 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java @@ -32,6 +32,7 @@ import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherDgRecord; import org.apache.poi.ddf.EscherOptRecord; import org.apache.poi.ddf.EscherProperty; +import org.apache.poi.ddf.EscherRecord; import org.apache.poi.ddf.EscherSpRecord; import org.apache.poi.ddf.EscherSpgrRecord; import org.apache.poi.hssf.model.DrawingManager2; @@ -80,9 +81,24 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing