Fix issues found when fuzzing Apache POI via Jazzer

Replace RuntimeException with a more specific types

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903104 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2022-07-29 17:07:15 +00:00
parent 92b8cf9984
commit 4df10cf9dd
16 changed files with 38 additions and 36 deletions

View File

@ -416,7 +416,7 @@ public class XWPFSettings extends POIXMLDocumentPart {
try { try {
ctSettings = SettingsDocument.Factory.parse(inputStream, DEFAULT_XML_OPTIONS).getSettings(); ctSettings = SettingsDocument.Factory.parse(inputStream, DEFAULT_XML_OPTIONS).getSettings();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new IllegalArgumentException("Failed to read data from input-stream", e);
} }
} }

View File

@ -112,7 +112,7 @@ final class RecordOrderer {
if (recClass == WorksheetProtectionBlock.class) { if (recClass == WorksheetProtectionBlock.class) {
return getWorksheetProtectionBlockInsertPos(records); return getWorksheetProtectionBlockInsertPos(records);
} }
throw new RuntimeException("Unexpected record class (" + recClass.getName() + ")"); throw new IllegalArgumentException("Unexpected record class (" + recClass.getName() + ")");
} }
/** /**
@ -182,7 +182,7 @@ final class RecordOrderer {
return i+1; return i+1;
} }
} }
throw new RuntimeException("Did not find insert point for GUTS"); throw new IllegalArgumentException("Did not find insert point for GUTS");
} }
private static boolean isPageBreakPriorRecord(Object rb) { private static boolean isPageBreakPriorRecord(Object rb) {
if (rb instanceof Record) { if (rb instanceof Record) {
@ -242,7 +242,7 @@ final class RecordOrderer {
// DataValidityTable // DataValidityTable
} }
} }
throw new RuntimeException("Did not find Window2 record"); throw new IllegalArgumentException("Did not find Window2 record");
} }
private static int findInsertPosForNewMergedRecordTable(List<RecordBase> records) { private static int findInsertPosForNewMergedRecordTable(List<RecordBase> records) {
@ -265,7 +265,7 @@ final class RecordOrderer {
return i + 1; return i + 1;
} }
} }
throw new RuntimeException("Did not find Window2 record"); throw new IllegalArgumentException("Did not find Window2 record");
} }
@ -332,7 +332,7 @@ final class RecordOrderer {
// ConditionalFormattingTable // ConditionalFormattingTable
case HyperlinkRecord.sid: case HyperlinkRecord.sid:
case UnknownRecord.QUICKTIP_0800: case UnknownRecord.QUICKTIP_0800:
// name of a VBA module // name of a VBA module
case UnknownRecord.CODENAME_1BA: case UnknownRecord.CODENAME_1BA:
return true; return true;
} }
@ -361,7 +361,7 @@ final class RecordOrderer {
} }
} }
// worksheet stream is seriously broken // worksheet stream is seriously broken
throw new RuntimeException("DimensionsRecord not found"); throw new IllegalArgumentException("DimensionsRecord not found");
} }
private static int getGutsRecordInsertPos(List<RecordBase> records) { private static int getGutsRecordInsertPos(List<RecordBase> records) {
@ -374,7 +374,7 @@ final class RecordOrderer {
return i+1; return i+1;
} }
} }
throw new RuntimeException("Did not find insert point for GUTS"); throw new IllegalArgumentException("Did not find insert point for GUTS");
} }
private static boolean isGutsPriorRecord(RecordBase rb) { private static boolean isGutsPriorRecord(RecordBase rb) {
@ -427,7 +427,7 @@ final class RecordOrderer {
return true; return true;
case EOFRecord.sid: case EOFRecord.sid:
// WINDOW2 should always be present, so shouldn't have got this far // WINDOW2 should always be present, so shouldn't have got this far
throw new RuntimeException("Found EOFRecord before WindowTwoRecord was encountered"); throw new IllegalArgumentException("Found EOFRecord before WindowTwoRecord was encountered");
} }
return PageSettingsBlock.isComponentRecord(sid); return PageSettingsBlock.isComponentRecord(sid);
} }

View File

@ -18,6 +18,7 @@
package org.apache.poi.hssf.model; package org.apache.poi.hssf.model;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
/** /**
@ -54,7 +55,7 @@ public final class RecordStream {
public Record getNext() { public Record getNext() {
if(!hasNext()) { if(!hasNext()) {
throw new RuntimeException("Attempt to read past end of record stream"); throw new NoSuchElementException("Attempt to read past end of record stream");
} }
_countRead ++; _countRead ++;
return _list.get(_nextIndex++); return _list.get(_nextIndex++);

View File

@ -61,7 +61,7 @@ public final class RowBlocksReader {
// records from a subsequent sheet. For example, if SharedFormulaRecords // records from a subsequent sheet. For example, if SharedFormulaRecords
// are taken from the wrong sheet, this could cause bug 44449. // are taken from the wrong sheet, this could cause bug 44449.
if (!rs.hasNext()) { if (!rs.hasNext()) {
throw new RuntimeException("Failed to find end of row/cell records"); throw new IllegalStateException("Failed to find end of row/cell records");
} }
Record rec = rs.getNext(); Record rec = rs.getNext();
@ -70,7 +70,7 @@ public final class RowBlocksReader {
case MergeCellsRecord.sid: dest = mergeCellRecords; break; case MergeCellsRecord.sid: dest = mergeCellRecords; break;
case SharedFormulaRecord.sid: dest = shFrmRecords; case SharedFormulaRecord.sid: dest = shFrmRecords;
if (!(prevRec instanceof FormulaRecord)) { if (!(prevRec instanceof FormulaRecord)) {
throw new RuntimeException("Shared formula record should follow a FormulaRecord"); throw new IllegalStateException("Shared formula record should follow a FormulaRecord, but had " + prevRec);
} }
FormulaRecord fr = (FormulaRecord)prevRec; FormulaRecord fr = (FormulaRecord)prevRec;
firstCellRefs.add(new CellReference(fr.getRow(), fr.getColumn())); firstCellRefs.add(new CellReference(fr.getRow(), fr.getColumn()));

View File

@ -85,7 +85,7 @@ public final class ColumnInfoRecord extends StandardRecord {
field_6_reserved = 0; field_6_reserved = 0;
break; break;
default: default:
throw new RuntimeException("Unusual record size remaining=(" + in.remaining() + ")"); throw new IllegalArgumentException("Unusual record size remaining=(" + in.remaining() + ")");
} }
} }

View File

@ -132,11 +132,11 @@ public final class SupBookRecord extends StandardRecord {
// 5.38.3 'Add-In Functions' // 5.38.3 'Add-In Functions'
_isAddInFunctions = true; _isAddInFunctions = true;
if(field_1_number_of_sheets != 1) { if(field_1_number_of_sheets != 1) {
throw new RuntimeException("Expected 0x0001 for number of sheets field in 'Add-In Functions' but got (" throw new IllegalArgumentException("Expected 0x0001 for number of sheets field in 'Add-In Functions' but got ("
+ field_1_number_of_sheets + ")"); + field_1_number_of_sheets + ")");
} }
} else { } else {
throw new RuntimeException("invalid EXTERNALBOOK code (" throw new IllegalArgumentException("invalid EXTERNALBOOK code ("
+ Integer.toHexString(nextShort) + ")"); + Integer.toHexString(nextShort) + ")");
} }
} }

View File

@ -108,7 +108,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
continue; continue;
} }
if (!(rec instanceof CellValueRecordInterface)) { if (!(rec instanceof CellValueRecordInterface)) {
throw new RuntimeException("Unexpected record type (" + rec.getClass().getName() + ")"); throw new IllegalArgumentException("Unexpected record type (" + rec.getClass().getName() + ")");
} }
_valuesAgg.construct((CellValueRecordInterface)rec, rs, svm); _valuesAgg.construct((CellValueRecordInterface)rec, rs, svm);
} }
@ -145,11 +145,11 @@ public final class RowRecordsAggregate extends RecordAggregate {
_valuesAgg.removeAllCellsValuesForRow(rowIndex); _valuesAgg.removeAllCellsValuesForRow(rowIndex);
RowRecord rr = _rowRecords.remove(rowIndex); RowRecord rr = _rowRecords.remove(rowIndex);
if (rr == null) { if (rr == null) {
throw new RuntimeException("Invalid row index (" + rowIndex + ")"); throw new IllegalArgumentException("Invalid row index (" + rowIndex + ")");
} }
if (row != rr) { if (row != rr) {
_rowRecords.put(rowIndex, rr); _rowRecords.put(rowIndex, rr);
throw new RuntimeException("Attempt to remove row that does not belong to this sheet"); throw new IllegalArgumentException("Attempt to remove row that does not belong to this sheet");
} }
// Clear the cached values // Clear the cached values
@ -215,7 +215,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
try { try {
return _rowRecordValues[startIndex].getRowNumber(); return _rowRecordValues[startIndex].getRowNumber();
} catch(ArrayIndexOutOfBoundsException e) { } catch(ArrayIndexOutOfBoundsException e) {
throw new RuntimeException("Did not find start row for block " + block); throw new IllegalArgumentException("Did not find start row for block " + block);
} }
} }
@ -232,7 +232,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
try { try {
return _rowRecordValues[endIndex].getRowNumber(); return _rowRecordValues[endIndex].getRowNumber();
} catch(ArrayIndexOutOfBoundsException e) { } catch(ArrayIndexOutOfBoundsException e) {
throw new RuntimeException("Did not find end row for block " + block); throw new IllegalArgumentException("Did not find end row for block " + block);
} }
} }

View File

@ -74,7 +74,8 @@ public final class SharedValueManager {
} }
} }
if (_numberOfFormulas >= _frAggs.length) { if (_numberOfFormulas >= _frAggs.length) {
throw new RuntimeException("Too many formula records for shared formula group"); throw new IllegalStateException("Too many formula records for shared formula group: " + _numberOfFormulas +
", expecting less than " + _frAggs.length);
} }
_frAggs[_numberOfFormulas++] = agg; _frAggs[_numberOfFormulas++] = agg;
} }
@ -153,7 +154,7 @@ public final class SharedValueManager {
public SharedFormulaRecord linkSharedFormulaRecord(CellReference firstCell, FormulaRecordAggregate agg) { public SharedFormulaRecord linkSharedFormulaRecord(CellReference firstCell, FormulaRecordAggregate agg) {
SharedFormulaGroup result = findFormulaGroupForCell(firstCell); SharedFormulaGroup result = findFormulaGroupForCell(firstCell);
if(null == result) { if(null == result) {
throw new RuntimeException("Failed to find a matching shared formula record"); throw new IllegalArgumentException("Failed to find a matching shared formula record for cell: " + firstCell);
} }
result.add(agg); result.add(agg);
return result.getSFR(); return result.getSFR();

View File

@ -41,7 +41,7 @@ public final class CellRangeAddress8Bit extends CellRangeAddressBase {
private static int readUShortAndCheck(LittleEndianInput in) { private static int readUShortAndCheck(LittleEndianInput in) {
if (in.available() < ENCODED_SIZE) { if (in.available() < ENCODED_SIZE) {
// Ran out of data // Ran out of data
throw new RuntimeException("Ran out of data reading CellRangeAddress"); throw new IllegalArgumentException("Ran out of data reading CellRangeAddress, available: " + in.available());
} }
return in.readUShort(); return in.readUShort();
} }

View File

@ -34,8 +34,8 @@ public class StandardEncryptionVerifier extends EncryptionVerifier implements En
protected StandardEncryptionVerifier(LittleEndianInput is, StandardEncryptionHeader header) { protected StandardEncryptionVerifier(LittleEndianInput is, StandardEncryptionHeader header) {
int saltSize = is.readInt(); int saltSize = is.readInt();
if (saltSize!=16) { if (saltSize != 16) {
throw new RuntimeException("Salt size != 16 !?"); throw new IllegalArgumentException("Salt size != 16: " + saltSize);
} }
byte[] salt = new byte[16]; byte[] salt = new byte[16];

View File

@ -263,7 +263,7 @@ public final class DocumentInputStream extends InputStream implements LittleEndi
throw new IllegalStateException("cannot perform requested operation on a closed stream"); throw new IllegalStateException("cannot perform requested operation on a closed stream");
} }
if (requestedSize > _document_size - _current_offset) { if (requestedSize > _document_size - _current_offset) {
throw new RuntimeException("Buffer underrun - requested " + requestedSize throw new IllegalStateException("Buffer underrun - requested " + requestedSize
+ " bytes but " + (_document_size - _current_offset) + " was available"); + " bytes but " + (_document_size - _current_offset) + " was available");
} }
} }
@ -276,7 +276,7 @@ public final class DocumentInputStream extends InputStream implements LittleEndi
@Override @Override
public void readFully(byte[] buf, int off, int len) { public void readFully(byte[] buf, int off, int len) {
if (len < 0) { if (len < 0) {
throw new RuntimeException("Can't read negative number of bytes"); throw new IllegalArgumentException("Can't read negative number of bytes, but had: " + len);
} }
checkAvaliable(len); checkAvaliable(len);

View File

@ -75,7 +75,7 @@ public final class ConstantValueParser {
in.readInt(); in.readInt();
return ErrorConstant.valueOf(errCode); return ErrorConstant.valueOf(errCode);
} }
throw new RuntimeException("Unknown grbit value (" + grbit + ")"); throw new IllegalArgumentException("Unknown grbit value (" + grbit + ")");
} }
private static Object readBoolean(LittleEndianInput in) { private static Object readBoolean(LittleEndianInput in) {
@ -87,7 +87,7 @@ public final class ConstantValueParser {
return Boolean.TRUE; return Boolean.TRUE;
} }
// Don't tolerate unusual boolean encoded values (unless it becomes evident that they occur) // Don't tolerate unusual boolean encoded values (unless it becomes evident that they occur)
throw new RuntimeException("unexpected boolean encoding (" + val + ")"); throw new IllegalArgumentException("unexpected boolean encoding (" + val + ")");
} }
public static int getEncodedSize(Object[] values) { public static int getEncodedSize(Object[] values) {

View File

@ -71,7 +71,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
temp.add(ptg); temp.add(ptg);
} }
if(pos != size) { if(pos != size) {
throw new RuntimeException("Ptg array size mismatch"); throw new IllegalArgumentException("Ptg array size mismatch");
} }
if (hasArrayPtgs) { if (hasArrayPtgs) {
Ptg[] result = toPtgArray(temp); Ptg[] result = toPtgArray(temp);
@ -167,7 +167,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
case IntPtg.sid: return new IntPtg(in); // 0x1e case IntPtg.sid: return new IntPtg(in); // 0x1e
case NumberPtg.sid: return new NumberPtg(in); // 0x1f case NumberPtg.sid: return new NumberPtg(in); // 0x1f
} }
throw new RuntimeException("Unexpected base token id (" + id + ")"); throw new IllegalArgumentException("Unexpected base token id (" + id + ")");
} }
private static Ptg[] toPtgArray(List<Ptg> l) { private static Ptg[] toPtgArray(List<Ptg> l) {
@ -254,7 +254,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
public final void setClass(byte thePtgClass) { public final void setClass(byte thePtgClass) {
if (isBaseToken()) { if (isBaseToken()) {
throw new RuntimeException("setClass should not be called on a base token"); throw new IllegalStateException("setClass should not be called on a base token");
} }
ptgClass = thePtgClass; ptgClass = thePtgClass;
} }
@ -279,7 +279,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
case Ptg.CLASS_VALUE: return 'V'; case Ptg.CLASS_VALUE: return 'V';
case Ptg.CLASS_ARRAY: return 'A'; case Ptg.CLASS_ARRAY: return 'A';
} }
throw new RuntimeException("Unknown operand class (" + ptgClass + ")"); throw new IllegalArgumentException("Unknown operand class (" + ptgClass + ")");
} }
public abstract byte getDefaultOperandClass(); public abstract byte getDefaultOperandClass();

View File

@ -64,7 +64,7 @@ public class CellRangeAddress extends CellRangeAddressBase {
private static int readUShortAndCheck(RecordInputStream in) { private static int readUShortAndCheck(RecordInputStream in) {
if (in.remaining() < ENCODED_SIZE) { if (in.remaining() < ENCODED_SIZE) {
// Ran out of data // Ran out of data
throw new RuntimeException("Ran out of data reading CellRangeAddress"); throw new IllegalArgumentException("Ran out of data reading CellRangeAddress");
} }
return in.readUShort(); return in.readUShort();
} }

View File

@ -76,7 +76,7 @@ public class LittleEndianByteArrayInputStream extends ByteArrayInputStream imple
protected void checkPosition(int i) { protected void checkPosition(int i) {
if (i > count - pos) { if (i > count - pos) {
throw new RuntimeException("Buffer overrun, having " + count + " bytes in the stream and position is at " + pos + throw new IllegalStateException("Buffer overrun, having " + count + " bytes in the stream and position is at " + pos +
", but trying to increment position by " + i); ", but trying to increment position by " + i);
} }
} }

View File

@ -2457,7 +2457,7 @@ final class TestBugs extends BaseTestBugzillaIssues {
RuntimeException.class, RuntimeException.class,
() -> new PropertySet(new DocumentInputStream(entry)) () -> new PropertySet(new DocumentInputStream(entry))
); );
assertEquals("Can't read negative number of bytes", ex.getMessage()); assertEquals("Can't read negative number of bytes, but had: -218103608", ex.getMessage());
} }
} }