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
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
index 407af72..5c9098f 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
@@ -416,7 +416,7 @@
         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);
         }
     }
 
diff --git a/poi/src/main/java/org/apache/poi/hssf/model/RecordOrderer.java b/poi/src/main/java/org/apache/poi/hssf/model/RecordOrderer.java
index 3d9eaa2..c627561 100644
--- a/poi/src/main/java/org/apache/poi/hssf/model/RecordOrderer.java
+++ b/poi/src/main/java/org/apache/poi/hssf/model/RecordOrderer.java
@@ -112,7 +112,7 @@
         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 @@
                 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 @@
                 // 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 @@
                     return i + 1;
             }
         }
-        throw new RuntimeException("Did not find Window2 record");
+        throw new IllegalArgumentException("Did not find Window2 record");
     }
 
 
@@ -332,7 +332,7 @@
             // 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 @@
             }
         }
         // 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 @@
                 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 @@
                 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);
     }
diff --git a/poi/src/main/java/org/apache/poi/hssf/model/RecordStream.java b/poi/src/main/java/org/apache/poi/hssf/model/RecordStream.java
index e0a546c..af57a55 100644
--- a/poi/src/main/java/org/apache/poi/hssf/model/RecordStream.java
+++ b/poi/src/main/java/org/apache/poi/hssf/model/RecordStream.java
@@ -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 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++);
diff --git a/poi/src/main/java/org/apache/poi/hssf/model/RowBlocksReader.java b/poi/src/main/java/org/apache/poi/hssf/model/RowBlocksReader.java
index 946d2c6..b348f28 100644
--- a/poi/src/main/java/org/apache/poi/hssf/model/RowBlocksReader.java
+++ b/poi/src/main/java/org/apache/poi/hssf/model/RowBlocksReader.java
@@ -61,7 +61,7 @@
             // 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 @@
                 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()));
diff --git a/poi/src/main/java/org/apache/poi/hssf/record/ColumnInfoRecord.java b/poi/src/main/java/org/apache/poi/hssf/record/ColumnInfoRecord.java
index 6cbdc79..f9e1a33 100644
--- a/poi/src/main/java/org/apache/poi/hssf/record/ColumnInfoRecord.java
+++ b/poi/src/main/java/org/apache/poi/hssf/record/ColumnInfoRecord.java
@@ -85,7 +85,7 @@
                 field_6_reserved  = 0;
                 break;
             default:
-                throw new RuntimeException("Unusual record size remaining=(" + in.remaining() + ")");
+                throw new IllegalArgumentException("Unusual record size remaining=(" + in.remaining() + ")");
         }
     }
 
diff --git a/poi/src/main/java/org/apache/poi/hssf/record/SupBookRecord.java b/poi/src/main/java/org/apache/poi/hssf/record/SupBookRecord.java
index 240c7b4..bdf88e1 100644
--- a/poi/src/main/java/org/apache/poi/hssf/record/SupBookRecord.java
+++ b/poi/src/main/java/org/apache/poi/hssf/record/SupBookRecord.java
@@ -132,11 +132,11 @@
             // 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) + ")");
         }
      }
diff --git a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java
index d37c911..2f6a8bd 100644
--- a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java
+++ b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java
@@ -108,7 +108,7 @@
                 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 @@
         _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 @@
         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 @@
         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);
       }
     }
 
diff --git a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java
index 45e3c12..0a22a74 100644
--- a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java
+++ b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java
@@ -74,7 +74,8 @@
                 }
             }
             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 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();
diff --git a/poi/src/main/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java b/poi/src/main/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java
index a3d5389..b43fdff 100644
--- a/poi/src/main/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java
+++ b/poi/src/main/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java
@@ -41,7 +41,7 @@
     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();
     }
diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptionVerifier.java b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptionVerifier.java
index d30fe99..52f285e 100644
--- a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptionVerifier.java
+++ b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptionVerifier.java
@@ -34,8 +34,8 @@
     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];
diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java b/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java
index 42add5e..a4e76fa 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java
@@ -263,7 +263,7 @@
             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 @@
     @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);
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/constant/ConstantValueParser.java b/poi/src/main/java/org/apache/poi/ss/formula/constant/ConstantValueParser.java
index 9464ecb..432eb3b 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/constant/ConstantValueParser.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/constant/ConstantValueParser.java
@@ -75,7 +75,7 @@
                 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 @@
                 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) {
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/ptg/Ptg.java b/poi/src/main/java/org/apache/poi/ss/formula/ptg/Ptg.java
index 52c5daf..5f53737 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/ptg/Ptg.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/ptg/Ptg.java
@@ -71,7 +71,7 @@
             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 @@
             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 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 @@
             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();
diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellRangeAddress.java b/poi/src/main/java/org/apache/poi/ss/util/CellRangeAddress.java
index 4d4d8af..91a6027 100644
--- a/poi/src/main/java/org/apache/poi/ss/util/CellRangeAddress.java
+++ b/poi/src/main/java/org/apache/poi/ss/util/CellRangeAddress.java
@@ -64,7 +64,7 @@
     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();
     }
diff --git a/poi/src/main/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java b/poi/src/main/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
index 69b5678..e5c0b25 100644
--- a/poi/src/main/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
+++ b/poi/src/main/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
@@ -76,7 +76,7 @@
 
     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);
         }
     }
diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java
index 4f216c6..4a11860 100644
--- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -2457,7 +2457,7 @@
                 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());
         }
     }