]> source.dussan.org Git - poi.git/commitdiff
Handle empty files correctly when looking for FileMagic to avoid NegativeArraySizeExc...
authorDominik Stadler <centic@apache.org>
Mon, 27 Jan 2020 22:54:15 +0000 (22:54 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 27 Jan 2020 22:54:15 +0000 (22:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1873232 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/filesystem/FileMagic.java
src/testcases/org/apache/poi/poifs/filesystem/TestFileMagic.java

index a1035a88661c1e2b155b306b9b94f7af5ab5844a..501db481a65007f2151f6b5de8090c16b2ad2b73 100644 (file)
@@ -113,7 +113,7 @@ public enum FileMagic {
     final static int MAX_PATTERN_LENGTH = 44;
 
     final byte[][] magic;
-    
+
     FileMagic(long magic) {
         this.magic = new byte[1][8];
         LittleEndian.putLong(this.magic[0], 0, magic);
@@ -122,7 +122,7 @@ public enum FileMagic {
     FileMagic(byte[]... magic) {
         this.magic = magic;
     }
-    
+
     FileMagic(String... magic) {
         this.magic = new byte[magic.length][];
         int i=0;
@@ -172,6 +172,9 @@ public enum FileMagic {
             // read as many bytes as possible, up to the required number of bytes
             byte[] data = new byte[MAX_PATTERN_LENGTH];
             int read = IOUtils.readFully(fis, data, 0, MAX_PATTERN_LENGTH);
+            if(read == -1) {
+                return FileMagic.UNKNOWN;
+            }
 
             // only use the bytes that could be read
             data = Arrays.copyOf(data, read);
index 3022abe6fd6778ee49fb2245e9a3e309da53ba20..30e0059f080f27be44986c185a171754dc077736 100644 (file)
@@ -107,8 +107,15 @@ public class TestFileMagic {
     @Test
     public void testShortFile() throws IOException {
         // having a file shorter than 8 bytes previously caused an exception
-        byte[] data = new byte[] { -1, -40, -1, -32, 0 };
+        fetchMagicFromData(new byte[] { -1, -40, -1, -32, 0 });
+        fetchMagicFromData(new byte[] { -1, -40, -1, -32 });
+        fetchMagicFromData(new byte[] { -1, -40, -1 });
+        fetchMagicFromData(new byte[] { -1, -40 });
+        fetchMagicFromData(new byte[] { -1 });
+        fetchMagicFromData(new byte[0]);
+    }
 
+    private void fetchMagicFromData(byte[] data) throws IOException {
         File file = TempFile.createTempFile("TestFileMagic", ".bin");
         try {
             try (FileOutputStream fos = new FileOutputStream(file)) {