diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFChartSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFChartSheet.java index b9944a58d94..798cd4df7b6 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFChartSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFChartSheet.java @@ -90,8 +90,9 @@ protected void write(OutputStream out) throws IOException { XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); xmlOptions.setSaveSyntheticDocumentElement( new QName(CTChartsheet.type.getName().getNamespaceURI(), "chartsheet")); - chartsheet.save(out, xmlOptions); - + if (chartsheet != null) { + chartsheet.save(out, xmlOptions); + } } private static byte[] blankWorksheet(){ diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/TestWorkbookFactory.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/TestWorkbookFactory.java index 200407e3efa..b306dc7712a 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/TestWorkbookFactory.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/TestWorkbookFactory.java @@ -351,4 +351,12 @@ private boolean openHSSFFile() { return false; } } + + @Test + void testEncryptionNullPointerException() throws IOException { + assertThrows(IllegalArgumentException.class, + () -> WorkbookFactory.create(POIDataSamples.getSpreadSheetInstance(). + openResourceAsStream( + "crash-9bf3cd4bd6f50a8a9339d363c2c7af14b536865c.xlsx"))); + } } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java b/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java index 587d72eca6c..9a911656604 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java @@ -252,9 +252,15 @@ public static Cipher getCipher(Key key, CipherAlgorithm cipherAlgorithm, Chainin if (cipherAlgorithm == CipherAlgorithm.rc4) { cipher = Cipher.getInstance(cipherAlgorithm.jceId); } else if (cipherAlgorithm.needsBouncyCastle) { + if (chain == null) { + throw new IllegalArgumentException("Did not have a chain for cipher " + cipherAlgorithm); + } registerBouncyCastle(); cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding, "BC"); } else { + if (chain == null) { + throw new IllegalArgumentException("Did not have a chain for cipher " + cipherAlgorithm); + } cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding); } diff --git a/test-data/spreadsheet/crash-274d6342e4842d61be0fb48eaadad6208ae767ae.xlsx b/test-data/spreadsheet/crash-274d6342e4842d61be0fb48eaadad6208ae767ae.xlsx new file mode 100644 index 00000000000..0707db6706b Binary files /dev/null and b/test-data/spreadsheet/crash-274d6342e4842d61be0fb48eaadad6208ae767ae.xlsx differ diff --git a/test-data/spreadsheet/crash-9bf3cd4bd6f50a8a9339d363c2c7af14b536865c.xlsx b/test-data/spreadsheet/crash-9bf3cd4bd6f50a8a9339d363c2c7af14b536865c.xlsx new file mode 100644 index 00000000000..5c9421f6280 Binary files /dev/null and b/test-data/spreadsheet/crash-9bf3cd4bd6f50a8a9339d363c2c7af14b536865c.xlsx differ diff --git a/test-data/spreadsheet/stress.xls b/test-data/spreadsheet/stress.xls index 4436b980e08..fb15d8174a4 100644 Binary files a/test-data/spreadsheet/stress.xls and b/test-data/spreadsheet/stress.xls differ