Maven PMD Plugin
  1. Maven PMD Plugin
  2. MPMD-146

An incomplete fix for the resource leak bugs in CpdReport.java

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 3.0
    • Fix Version/s: 3.0
    • Component/s: PMD
    • Labels:
      None
    • Number of attachments :
      1

      Description

      The fix revision 730089 was aimed to remove an resource leak bug on the OutputStreamWriter object "writer" (created in line 188) in the method "writeNonHtml()" of the file "/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java" ,

      but it is incomplete.

      There are some problems:
      1. when "writer" is not created successfully at line 189 but the FileOutputStream object "tStream" is created successfully at line 188,"tStream" will be leaked.

      The best way to close such resource objects is putting such close operations for all resource objects in the finaly block of a try-catch-finally structure.

      Although a later fix (rev935316) tried to fix it, the problem still exists in the head revision. The buggy code is copied as bellows (the object "tStream" needs to be closed at line 220):

      void writeNonHtml( CPD cpd )
      throws MavenReportException
      {
      Renderer r = createRenderer();

      if ( r == null )

      { return; }

      String buffer = r.render( cpd.getMatches() );
      Writer writer = null;
      try

      { targetDirectory.mkdirs(); File targetFile = new File( targetDirectory, "cpd." + format ); 220 FileOutputStream tStream = new FileOutputStream( targetFile ); 221 writer = new OutputStreamWriter( tStream, getOutputEncoding() ); writer.write( buffer ); writer.close(); File siteDir = getReportOutputDirectory(); siteDir.mkdirs(); FileUtils.copyFile( targetFile, new File( siteDir, "cpd." + format ) ); }

      catch ( IOException ioe )

      { throw new MavenReportException( ioe.getMessage(), ioe ); }

      finally

      { 235 IOUtil.close( writer ); }

      }

        Activity

        Hide
        Andreas Dangel added a comment -

        Simple fix - closes the file output stream in the finally block, too.

        Show
        Andreas Dangel added a comment - Simple fix - closes the file output stream in the finally block, too.
        Hide
        Olivier Lamy added a comment -

        fixed.
        Thanks

        Show
        Olivier Lamy added a comment - fixed. Thanks

          People

          • Assignee:
            Olivier Lamy
            Reporter:
            Guangtai Liang
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: