Uploaded image for project: 'Maven PMD Plugin'
  1. Maven PMD Plugin
  2. MPMD-146

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

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 3.0
    • 3.0
    • PMD
    • None

    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 ); }

      }

      Attachments

        Activity

          People

            olamy Olivier Lamy
            guangtai Guangtai Liang
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: