aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-10-10 09:58:02 +0000
committerPJ Fanning <fanningpj@apache.org>2021-10-10 09:58:02 +0000
commit2514e4d0ab925a4de8baa4ed4bcdbb90ca2713ab (patch)
treef7fdf0803511a51f89ec8672dad6c2e26a5b3347
parent69b2f313741845d42f8a756db83cc5a9fbd4b8a9 (diff)
downloadpoi-2514e4d0ab925a4de8baa4ed4bcdbb90ca2713ab.tar.gz
poi-2514e4d0ab925a4de8baa4ed4bcdbb90ca2713ab.zip
[bug-65452] fix issue where WorkbookFactory.create(File, ...) returns null if file type not recognised
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894097 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java23
-rw-r--r--poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java10
-rw-r--r--test-data/spreadsheet/workbook.xml1
3 files changed, 26 insertions, 8 deletions
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
index ae0770f555..e65c92e48e 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
@@ -3623,4 +3623,25 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
}
}
}
-}
+
+ @Test
+ void testBug65452() throws IOException {
+ File file = XSSFTestDataSamples.getSampleFile("workbook.xml");
+ try (FileInputStream fis = new FileInputStream(file)) {
+ try {
+ Workbook wb = WorkbookFactory.create(fis);
+ if (wb != null) wb.close();
+ fail("WorkbookFactory.create should have failed");
+ } catch (IOException ie) {
+ assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage());
+ }
+ }
+ try {
+ Workbook wb = WorkbookFactory.create(file);
+ if (wb != null) wb.close();
+ fail("WorkbookFactory.create should have failed");
+ } catch (IOException ie) {
+ assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage());
+ }
+ }
+} \ No newline at end of file
diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
index 0b910f379f..823bbf5c4e 100644
--- a/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
+++ b/poi/src/main/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
@@ -19,11 +19,7 @@ package org.apache.poi.ss.usermodel;
import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
@@ -291,9 +287,9 @@ public final class WorkbookFactory {
ooxmlEnc = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
}
return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly));
+ } else {
+ throw new IOException("Can't open workbook - unsupported file type: "+fm);
}
-
- return null;
}
diff --git a/test-data/spreadsheet/workbook.xml b/test-data/spreadsheet/workbook.xml
new file mode 100644
index 0000000000..61531b0ee8
--- /dev/null
+++ b/test-data/spreadsheet/workbook.xml
@@ -0,0 +1 @@
+<?xml version="1.0"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel"><TEST_REPORT><Author>EXAMPLE</Author></TEST_REPORT><Styles><Style ss:ID="Header"><Font ss:Bold="1"/><Interior ss:Color="#92D050" ss:Pattern="Solid"/><Alignment ss:Vertical="Center" ss:WrapText="1"/><Borders><Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/></Borders></Style><Style ss:ID="Data"><Borders><Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/></Borders></Style><Style ss:ID="Data1"><Alignment ss:Horizontal="Right"/><NumberFormat ss:Format="Standard"/><Borders><Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/></Borders></Style></Styles><Worksheet ss:Name="Sheet1"><Table x:FullColumns="1" x:FullRows="1"><Column ss:Width="120"/><Column ss:Width="120"/><Column ss:Width="120"/><Row/><Row ss:AutoFitHeight="1"><Cell ss:StyleID="Header"><Data ss:Type="String">Konto</Data></Cell><Cell ss:StyleID="Header"><Data ss:Type="String">Zuordnung</Data></Cell><Cell ss:StyleID="Header"><Data ss:Type="String">Nummer</Data></Cell></Row><Row><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/></Row><Row><Cell ss:StyleID="Data"><Data ss:Type="String">KONTO0001</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String">123456789</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String">PROJEKT 1</Data></Cell></Row><Row><Cell ss:StyleID="Data"><Data ss:Type="String">KONTO0001</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String">123456789</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String"/></Cell></Row></Table></Worksheet></Workbook> \ No newline at end of file