https://issues.apache.org/bugzilla/show_bug.cgi?id=52720

             Bug #: 52720
           Summary: An incomplete fix for the resource leak bugs in
                    ManagerBase.java
           Product: Tomcat 6
           Version: unspecified
          Platform: PC
            Status: NEW
          Severity: critical
          Priority: P2
         Component: Catalina
        AssignedTo: dev@tomcat.apache.org
        ReportedBy: liangg...@sei.pku.edu.cn
    Classification: Unclassified


The fix revision 777567 was aimed to remove an resource leak bug on the 
DataInputStream object "randomIS"in the method "run" of the file
"/tomcat/tc6.0.x/trunk/java/org/apache/catalina/session/ManagerBase.java" , but
it is incomplete. 

When the statements at lines 256 throw any eception, the object "randomIS" can
not be closed as expected. The best way to close such resource object is
putting such close operations in the finaly block of a try-catch-finally
structure.

Besides that, when DataInputStream is created unsuccessfully but the  temp
FileInputStream object is created successfully at lines 250,  the temp
FileInputStream object will be leak. 

The Same problem is also existed in the method of "setRandomFile".  "randomIS"
should also be closed in the finally block of a try-catch-finally clause, and
the temp FileInputStream object should also be closed explicitly. 

The buggy code is copies as bellows: 


 private class PrivilegedSetRandomFile
            implements PrivilegedAction<DataInputStream>{

        public PrivilegedSetRandomFile(String s) {
            devRandomSource = s;
        }

        public DataInputStream run(){
            try {
                File f=new File( devRandomSource );
                if( ! f.exists() ) return null;
 250              randomIS= new DataInputStream( new FileInputStream(f));
                randomIS.readLong();
                if( log.isDebugEnabled() )
                    log.debug( "Opening " + devRandomSource );
                return randomIS;
            } catch (IOException ex){
                log.warn("Error reading " + devRandomSource, ex);
 257               if (randomIS != null) {
                    try {
                        randomIS.close();
                    } catch (Exception e) {
                        log.warn("Failed to close randomIS.");
                    }
                }
                devRandomSource = null;
                randomIS=null;
                return null;
            }
        }
    }


  public void setRandomFile( String s ) {
        // as a hack, you can use a static file - and generate the same
        // session ids ( good for strange debugging )
        if (Globals.IS_SECURITY_ENABLED){
            randomIS = AccessController.doPrivileged(new
PrivilegedSetRandomFile(s));
        } else {
            try{
                devRandomSource=s;
                File f=new File( devRandomSource );
                if( ! f.exists() ) return;
                randomIS= new DataInputStream( new FileInputStream(f));
                randomIS.readLong();
                if( log.isDebugEnabled() )
                    log.debug( "Opening " + devRandomSource );
            } catch( IOException ex ) {
553            log.warn("Error reading " + devRandomSource, ex);
                if (randomIS != null) {
                    try {
                        randomIS.close();
                    } catch (Exception e) {
                        log.warn("Failed to close randomIS.");
                    }
                }
                devRandomSource = null;
                randomIS=null;
            }
        }
    }

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to