]> source.dussan.org Git - poi.git/commitdiff
Fix some additional file-handle-leaks reported by unit-tests via an enhanced version...
authorDominik Stadler <centic@apache.org>
Fri, 3 Jan 2020 13:11:41 +0000 (13:11 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 3 Jan 2020 13:11:41 +0000 (13:11 +0000)
Also add a test for Bug 64045

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

src/integrationtest/org/apache/poi/TestAllFiles.java
src/ooxml/java/org/apache/poi/xslf/util/EMFHandler.java
src/ooxml/testcases/org/apache/poi/xssf/XSSFMemoryLeakTests.java
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFBEventBasedExcelExtractor.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
test-data/spreadsheet/xlsx-corrupted.xlsx [new file with mode: 0644]

index 2374c9e6d40aa60235ada5f06cae5ed20b58d2f2..8f9ba82a4c18781b9f26c8687845e6b277dca9c7 100644 (file)
@@ -317,6 +317,7 @@ public class TestAllFiles {
         "spreadsheet/poc-shared-strings.xlsx",  // contains shared-string-entity-expansion
         "document/61612a.docx",
         "document/word2.doc",
+        "spreadsheet/xlsx-corrupted.xlsx",
 
         // old Excel files, which we only support simple text extraction of
         "spreadsheet/testEXCEL_2.xls",
index 94fb64447e2b3cd64865bc1b5675d7ab4b41c279..9d7e620bb4819738dab0c2299231618051cf0868 100644 (file)
@@ -43,8 +43,9 @@ class EMFHandler extends MFProxy {
 
     @Override
     public void parse(File file) throws IOException {
-        // stream needs to be kept open
-        parse(file.toURI().toURL().openStream());
+        // stream needs to be kept open until the instance is closed
+        is = file.toURI().toURL().openStream();
+        parse(is);
     }
 
     @Override
@@ -66,7 +67,6 @@ class EMFHandler extends MFProxy {
 //                }
             }
         }
-
     }
 
     protected String getContentType() {
index 958e54554c2259d51d503560daea785571000df3..c987e4a91b6e5792b42ab7469a098c84428d4052 100644 (file)
@@ -17,6 +17,9 @@
 
 package org.apache.poi.xssf;
 
+import org.apache.poi.ooxml.POIXMLException;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.util.MemoryLeakVerifier;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFRow;
@@ -27,12 +30,14 @@ import org.junit.Test;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
 
 /**
  * A test which uses {@link MemoryLeakVerifier} to ensure that certain
@@ -141,4 +146,24 @@ public class XSSFMemoryLeakTests {
 
         wb1.close();
     }
+
+    @Test(expected = POIXMLException.class)
+    public void testFileLeak() throws IOException, InvalidFormatException {
+        File file = XSSFTestDataSamples.getSampleFile("xlsx-corrupted.xlsx");
+        verifier.addObject(file);
+        try (XSSFWorkbook ignored = new XSSFWorkbook(file)) {
+            fail("Should catch exception as the file is corrupted");
+        }
+    }
+
+    @Test(expected = POIXMLException.class)
+    public void testFileLeak2() throws IOException, InvalidFormatException {
+        File file = XSSFTestDataSamples.getSampleFile("xlsx-corrupted.xlsx");
+        verifier.addObject(file);
+        try (OPCPackage pkg = OPCPackage.open(file)) {
+            try (XSSFWorkbook ignored = new XSSFWorkbook(pkg)) {
+                fail("Should catch exception as the file is corrupted");
+            }
+        }
+    }
 }
index 226a76ead65f6f1cac1b1faa6880b2222ce0b8ea..025cf49dc87fac221b556090b5f1bf5243becf94 100644 (file)
@@ -113,17 +113,17 @@ public class TestXSSFBEventBasedExcelExtractor {
             extractor.setIncludeCellComments(true);
             String[] rows = extractor.getText().split("[\r\n]+");
             assertEquals(283, rows.length);
-            BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(),
-                    StandardCharsets.UTF_8);
-            String line = reader.readLine();
-            for (String row : rows) {
-                assertEquals(line, row);
-                line = reader.readLine();
-                while (line != null && line.startsWith("#")) {
+            try (BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(),
+                    StandardCharsets.UTF_8)) {
+                String line = reader.readLine();
+                for (String row : rows) {
+                    assertEquals(line, row);
                     line = reader.readLine();
+                    while (line != null && line.startsWith("#")) {
+                        line = reader.readLine();
+                    }
                 }
             }
         }
     }
-
 }
index f78af2f0442e9f1a6a51f89635ff20cdf3a5fc71..0963a27798d3419f22e02ed72dbad336447a99a2 100644 (file)
@@ -27,6 +27,7 @@ import java.nio.file.Path;
 import java.util.List;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -233,11 +234,12 @@ public final class TestSharedStringsTable {
 
         Path path = XSSFTestDataSamples.getSampleFile("48936-strings.txt").toPath();
 
-        List<String> lst = Files
-            .lines(path, StandardCharsets.UTF_8)
+        Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
+        List<String> lst = lines
             .map(String::trim)
             .filter(((Predicate<String>)String::isEmpty).negate())
             .collect(Collectors.toList());
+        lines.close();
 
         for (String str : lst) {
             s.createRow(i++).createCell(0).setCellValue(str);
index 9eb2ed9dc7646009df87bbee6828c219a22be56a..65eaa7f1735434f236908e9448059cced3c5df5f 100644 (file)
@@ -3455,4 +3455,12 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
             }*/
         }
     }
+
+    @Test(expected = POIXMLException.class)
+    public void test64045() throws IOException, InvalidFormatException {
+        File file = XSSFTestDataSamples.getSampleFile("xlsx-corrupted.xlsx");
+        try (XSSFWorkbook ignored = new XSSFWorkbook(file)) {
+            fail("Should catch exception as the file is corrupted");
+        }
+    }
 }
index 29f6e22f1924d84e8a6d2cc923c0e5418848614d..060f665bc77cdf2663e0f5f575895a9b5b2b0ebd 100644 (file)
@@ -2914,43 +2914,40 @@ public final class TestBugs extends BaseTestBugzillaIssues {
 
     @Test
     public void test46515() throws IOException {
-        Workbook wb = HSSFTestDataSamples.openSampleWorkbook("46515.xls");
-
-        // Get structure from webservice
-        String urlString = "http://poi.apache.org/components/spreadsheet/images/calendar.jpg";
-        URL structURL = new URL(urlString);
-        BufferedImage bimage;
-        try {
-            bimage = ImageIO.read(structURL);
-        } catch (IOException e) {
-            Assume.assumeNoException("Downloading a jpg from poi.apache.org should work", e);
-            return;
-        } finally {
-            wb.close();
-        }
+        try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook("46515.xls")) {
+
+            // Get structure from webservice
+            String urlString = "http://poi.apache.org/components/spreadsheet/images/calendar.jpg";
+            URL structURL = new URL(urlString);
+            BufferedImage bimage;
+            try {
+                bimage = ImageIO.read(structURL);
+            } catch (IOException e) {
+                Assume.assumeNoException("Downloading a jpg from poi.apache.org should work", e);
+                return;
+            }
 
-        // Convert BufferedImage to byte[]
-        ByteArrayOutputStream imageBAOS = new ByteArrayOutputStream();
-        ImageIO.write(bimage, "jpeg", imageBAOS);
-        imageBAOS.flush();
-        byte[] imageBytes = imageBAOS.toByteArray();
-        imageBAOS.close();
-
-        // Pop structure into Structure HSSFSheet
-        int pict = wb.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
-        Sheet sheet = wb.getSheet("Structure");
-        assertNotNull("Did not find sheet", sheet);
-        HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
-        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 10, 22);
-        anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
-        patriarch.createPicture(anchor, pict);
-
-        // Write out destination file
+            // Convert BufferedImage to byte[]
+            ByteArrayOutputStream imageBAOS = new ByteArrayOutputStream();
+            ImageIO.write(bimage, "jpeg", imageBAOS);
+            imageBAOS.flush();
+            byte[] imageBytes = imageBAOS.toByteArray();
+            imageBAOS.close();
+
+            // Pop structure into Structure HSSFSheet
+            int pict = wb.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
+            Sheet sheet = wb.getSheet("Structure");
+            assertNotNull("Did not find sheet", sheet);
+            HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
+            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 10, 22);
+            anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
+            patriarch.createPicture(anchor, pict);
+
+            // Write out destination file
 //        FileOutputStream fileOut = new FileOutputStream("/tmp/46515.xls");
 //        wb.write(fileOut);
 //        fileOut.close();
-
-        wb.close();
+        }
     }
 
     @Test
diff --git a/test-data/spreadsheet/xlsx-corrupted.xlsx b/test-data/spreadsheet/xlsx-corrupted.xlsx
new file mode 100644 (file)
index 0000000..0e363e7
Binary files /dev/null and b/test-data/spreadsheet/xlsx-corrupted.xlsx differ