]> source.dussan.org Git - poi.git/commitdiff
Fix issues found when fuzzing Apache POI via Jazzer
authorDominik Stadler <centic@apache.org>
Fri, 29 Jul 2022 17:07:15 +0000 (17:07 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 29 Jul 2022 17:07:15 +0000 (17:07 +0000)
Replace RuntimeException with a more specific types

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903104 13f79535-47bb-0310-9956-ffa450edef68

16 files changed:
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
poi/src/main/java/org/apache/poi/hssf/model/RecordOrderer.java
poi/src/main/java/org/apache/poi/hssf/model/RecordStream.java
poi/src/main/java/org/apache/poi/hssf/model/RowBlocksReader.java
poi/src/main/java/org/apache/poi/hssf/record/ColumnInfoRecord.java
poi/src/main/java/org/apache/poi/hssf/record/SupBookRecord.java
poi/src/main/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java
poi/src/main/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java
poi/src/main/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java
poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptionVerifier.java
poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java
poi/src/main/java/org/apache/poi/ss/formula/constant/ConstantValueParser.java
poi/src/main/java/org/apache/poi/ss/formula/ptg/Ptg.java
poi/src/main/java/org/apache/poi/ss/util/CellRangeAddress.java
poi/src/main/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java

index 407af7232f57d44a8e24d738058c24bc623bd9a2..5c9098fb53da80392d9decace5f605b90eb1fbfd 100644 (file)
@@ -416,7 +416,7 @@ public class XWPFSettings extends POIXMLDocumentPart {
         try {
             ctSettings = SettingsDocument.Factory.parse(inputStream, DEFAULT_XML_OPTIONS).getSettings();
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            throw new IllegalArgumentException("Failed to read data from input-stream", e);
         }
     }
 
index 3d9eaa2669627dea1f9a455af46ffa53102ec3d5..c627561b615e075f06ec10dc1ced6780529ea2fa 100644 (file)
@@ -112,7 +112,7 @@ final class RecordOrderer {
         if (recClass == WorksheetProtectionBlock.class) {
             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;
             }
         }
-        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) {
         if (rb instanceof Record) {
@@ -242,7 +242,7 @@ final class RecordOrderer {
                 // DataValidityTable
             }
         }
-        throw new RuntimeException("Did not find Window2 record");
+        throw new IllegalArgumentException("Did not find Window2 record");
     }
 
     private static int findInsertPosForNewMergedRecordTable(List<RecordBase> records) {
@@ -265,7 +265,7 @@ final class RecordOrderer {
                     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
             case HyperlinkRecord.sid:
             case UnknownRecord.QUICKTIP_0800:
-            // name of a VBA module    
+            // name of a VBA module
             case UnknownRecord.CODENAME_1BA:
                 return true;
         }
@@ -361,7 +361,7 @@ final class RecordOrderer {
             }
         }
         // worksheet stream is seriously broken
-        throw new RuntimeException("DimensionsRecord not found");
+        throw new IllegalArgumentException("DimensionsRecord not found");
     }
 
     private static int getGutsRecordInsertPos(List<RecordBase> records) {
@@ -374,7 +374,7 @@ final class RecordOrderer {
                 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) {
@@ -427,7 +427,7 @@ final class RecordOrderer {
                 return true;
             case EOFRecord.sid:
                 // 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);
     }
index e0a546cbbb4c9c39977540be04c0e86119b58ff4..af57a55716a5d020ab768356c12ebd1255992eb3 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.hssf.model;
 
 import java.util.List;
+import java.util.NoSuchElementException;
 
 import org.apache.poi.hssf.record.Record;
 /**
@@ -54,7 +55,7 @@ public final class RecordStream {
 
     public Record getNext() {
         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 ++;
         return _list.get(_nextIndex++);
index 946d2c610fc3db7c9ab1e2061d1361d2daae2971..b348f28a658ce9f6e897ba36675b3be6738da517 100644 (file)
@@ -61,7 +61,7 @@ public final class RowBlocksReader {
             // records from a subsequent sheet.  For example, if SharedFormulaRecords
             // are taken from the wrong sheet, this could cause bug 44449.
             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();
@@ -70,7 +70,7 @@ public final class RowBlocksReader {
                 case MergeCellsRecord.sid:    dest = mergeCellRecords; break;
                 case SharedFormulaRecord.sid: dest = shFrmRecords;
                     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;
                     firstCellRefs.add(new CellReference(fr.getRow(), fr.getColumn()));
index 6cbdc796915d5db9b4a7139fc08fefbdc16621e8..f9e1a330ff94ff91bcf25a945d7e15946deccfdb 100644 (file)
@@ -85,7 +85,7 @@ public final class ColumnInfoRecord extends StandardRecord {
                 field_6_reserved  = 0;
                 break;
             default:
-                throw new RuntimeException("Unusual record size remaining=(" + in.remaining() + ")");
+                throw new IllegalArgumentException("Unusual record size remaining=(" + in.remaining() + ")");
         }
     }
 
index 240c7b4c35755909040768b049b5d43ad833cf50..bdf88e1cc3822eb1c05c52336ee78d16b5d11b52 100644 (file)
@@ -132,11 +132,11 @@ public final class SupBookRecord extends StandardRecord {
             // 5.38.3 'Add-In Functions'
             _isAddInFunctions = true;
             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 + ")");
             }
         } else {
-            throw new RuntimeException("invalid EXTERNALBOOK code ("
+            throw new IllegalArgumentException("invalid EXTERNALBOOK code ("
                      + Integer.toHexString(nextShort) + ")");
         }
      }
index d37c911c7260e0052c69b341e016b01c30a5f26f..2f6a8bd2e5837792964a4b291ede81b78e539124 100644 (file)
@@ -108,7 +108,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
                 continue;
             }
             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);
         }
@@ -145,11 +145,11 @@ public final class RowRecordsAggregate extends RecordAggregate {
         _valuesAgg.removeAllCellsValuesForRow(rowIndex);
         RowRecord rr = _rowRecords.remove(rowIndex);
         if (rr == null) {
-            throw new RuntimeException("Invalid row index (" + rowIndex + ")");
+            throw new IllegalArgumentException("Invalid row index (" + rowIndex + ")");
         }
         if (row != 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
@@ -215,7 +215,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
         try {
             return _rowRecordValues[startIndex].getRowNumber();
         } 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 {
             return _rowRecordValues[endIndex].getRowNumber();
         } 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);
       }
     }
 
index 45e3c12c44c810ff427de03229f71685a797ccce..0a22a74f677e93570674b28bc82cd9fcd936e214 100644 (file)
@@ -74,7 +74,8 @@ public final class SharedValueManager {
                 }
             }
             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;
         }
@@ -153,7 +154,7 @@ public final class SharedValueManager {
     public SharedFormulaRecord linkSharedFormulaRecord(CellReference firstCell, FormulaRecordAggregate agg) {
         SharedFormulaGroup result = findFormulaGroupForCell(firstCell);
         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);
         return result.getSFR();
index a3d53894c0e1f8543c43a07cee5fa228f7676c6f..b43fdffd58c3d91893fb2b38ca5517d43b7519cc 100644 (file)
@@ -41,7 +41,7 @@ public final class CellRangeAddress8Bit extends CellRangeAddressBase {
     private static int readUShortAndCheck(LittleEndianInput in) {
         if (in.available() < ENCODED_SIZE) {
             // 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();
     }
index d30fe998b2a655769157fdc2755db81947f333d8..52f285eee30f115af517ff87bffe21079373ffa4 100644 (file)
@@ -34,8 +34,8 @@ public class StandardEncryptionVerifier extends EncryptionVerifier implements En
     protected StandardEncryptionVerifier(LittleEndianInput is, StandardEncryptionHeader header) {
         int saltSize = is.readInt();
 
-        if (saltSize!=16) {
-            throw new RuntimeException("Salt size != 16 !?");
+        if (saltSize != 16) {
+            throw new IllegalArgumentException("Salt size != 16: " + saltSize);
         }
 
         byte[] salt = new byte[16];
index 42add5e3425c1e01e17d9618bb5172ada03315c3..a4e76fab89ea897093c586c3df9705eac12618f5 100644 (file)
@@ -263,7 +263,7 @@ public final class DocumentInputStream extends InputStream implements LittleEndi
             throw new IllegalStateException("cannot perform requested operation on a closed stream");
         }
         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");
         }
     }
@@ -276,7 +276,7 @@ public final class DocumentInputStream extends InputStream implements LittleEndi
     @Override
     public void readFully(byte[] buf, int off, int len) {
         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);
index 9464ecbdb4b737d1796b550eadd204149b4a1850..432eb3b8977f2904ad5a4d286e38d5bc427647b4 100644 (file)
@@ -75,7 +75,7 @@ public final class ConstantValueParser {
                 in.readInt();
                 return ErrorConstant.valueOf(errCode);
         }
-        throw new RuntimeException("Unknown grbit value (" + grbit + ")");
+        throw new IllegalArgumentException("Unknown grbit value (" + grbit + ")");
     }
 
     private static Object readBoolean(LittleEndianInput in) {
@@ -87,7 +87,7 @@ public final class ConstantValueParser {
                 return Boolean.TRUE;
         }
         // 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) {
index 52c5daf5e870c035fc961edc7ee62f4f50758aee..5f53737c94751284f743f1504804c42e4a811aca 100644 (file)
@@ -71,7 +71,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
             temp.add(ptg);
         }
         if(pos != size) {
-            throw new RuntimeException("Ptg array size mismatch");
+            throw new IllegalArgumentException("Ptg array size mismatch");
         }
         if (hasArrayPtgs) {
             Ptg[] result = toPtgArray(temp);
@@ -167,7 +167,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
             case IntPtg.sid:          return new IntPtg(in);          // 0x1e
             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) {
@@ -254,7 +254,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
 
     public final void setClass(byte thePtgClass) {
         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;
     }
@@ -279,7 +279,7 @@ public abstract class Ptg implements Duplicatable, GenericRecord {
             case Ptg.CLASS_VALUE: return 'V';
             case Ptg.CLASS_ARRAY: return 'A';
         }
-        throw new RuntimeException("Unknown operand class (" + ptgClass + ")");
+        throw new IllegalArgumentException("Unknown operand class (" + ptgClass + ")");
     }
 
     public abstract byte getDefaultOperandClass();
index 4d4d8af62ad313fe915595dac4da7263e0e6b363..91a60273a4942df915d8991efb786939bacbcc8e 100644 (file)
@@ -64,7 +64,7 @@ public class CellRangeAddress extends CellRangeAddressBase {
     private static int readUShortAndCheck(RecordInputStream in) {
         if (in.remaining() < ENCODED_SIZE) {
             // 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();
     }
index 69b5678ee00584429d18a018871fffba77129e1f..e5c0b259a06d7cccd7baf17aa625c15e6e2db643 100644 (file)
@@ -76,7 +76,7 @@ public class LittleEndianByteArrayInputStream extends ByteArrayInputStream imple
 
     protected void checkPosition(int i) {
         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);
         }
     }
index 4f216c68488af0be285cbe15bd3c3a403cbc9673..4a11860161035ecfce4c77298ab23705260bd44d 100644 (file)
@@ -2457,7 +2457,7 @@ final class TestBugs extends BaseTestBugzillaIssues {
                 RuntimeException.class,
                 () -> 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());
         }
     }