Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1755

potential resource leak bug in test code

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • 1.4.12, 1.3.12
    • None
    • logback-core
    • None

    Description

      There is a potential resource leak bug in the implementation of the CompressTest#copy() method.

      This method copies the contents of one file to another. However, the input and output streams will not be closed if an exception is thrown during the write operation. One solution is to use try-with-resources so that it is guaranteed that the streams will be close in the event an exception is thrown.

       

      public static void copy(File src, File dst) throws IOException {
        try (InputStream in = new FileInputStream(src);
             OutputStream out = new FileOutputStream(dst)) {
             // Transfer bytes from in to out
             byte[] buf = new byte[1024];
             int len;
             while ((len = in.read(buf)) > 0) {
               out.write(buf, 0, len);
             }
         }
      }
      

      The priority for this issue is minor since it affects only test code. Can someone kindly confirm if this is an issue?

      Context

      We came across this issue while investigating open-source projects to see if they contain code that is similar to (or reused from) Stack Overflow and whether the code on Stack Overflow underwent any bug or security fixes. We found that the code in the CompressTest{}#copy() method is somewhat similar to the one in the first version of this answer. The issue was subsequently fixed in the second version, however, the fix is not reflected in this method.

       

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            alf.jallow Alfusainey Jallow
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: