3breadt / dd-plist

A java library providing support for ASCII, XML and binary property lists.
Other
258 stars 94 forks source link

Improper (or missing) handling of resources (FileInputStream/FileOutputStream) #46

Closed EliyahuStern closed 6 years ago

EliyahuStern commented 6 years ago

The parser / writer methods receiving a File object, creates streams and never / improperly close them, causing resource leaks.

For instance: BinaryPropertyListParser - The FileInputStream is never closed. BinaryPropertyListWriter - The FileOutputStream is closed, but with no try\finally block.

These open files can cause all sort of weird behaviors, such as running out of file descriptors, or inability to move/delete the file after reading/writing.

It is required (since it is Java 1.5) to use the try-finally pattern to close the streams:

    InputStream is = null;
    try {
         is = new FileInputStream(file);
         // read the file
    } finally {
         if (is != null) is.close();
    }
EliyahuStern commented 6 years ago

Suggested test case:

public static void testDeleteAfterParse() throws Exception {
    File f = new File("test-files/toDelete1.plist");
    NSDictionary d = (NSDictionary)PropertyListParser.parse(f);
    assertTrue(f.delete());
}
3breadt commented 6 years ago

Thank you for bringing this to my attention. I adjusted all places where input and output streams are handled making sure file resources are released.

EliyahuStern commented 6 years ago

Happy to help. Thanks for publishing and maintaining such a useful repo!