Proposed FindBugs pattern

View: New views
5 Messages — Rating Filter:   Alert me  

Proposed FindBugs pattern

by JavaOne :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

public void example1() throws IOException                                  
{                                                                          
   StringBuffer stringBuffer = new StringBuffer();                         
   File file = new File( "input.dat" );                                    
   BufferedReader br = null;                                               
                                                                           
   try                                                                     
   {                                                                       
      br = new BufferedReader( new FileReader( file ) );                   
      int length = ( int ) file.length();                                  
      char[] buffer = new char[ length ];                                  
                                                                           
      int read;                                                            
      while ( ( read = br.read( buffer ) ) != -1 )                         
      {                                                                    
         stringBuffer.append( buffer, 0, read );                           
      }                                                                    
      // BUG: Read length could be zero.                                   
      //                                                                   
      // The buffer is created based on the size of a file. If the file    
      // happens to be empty, the buffer will have a length of zero.       
      // Every read into a zero-length buffer will immediately return      
      // zero, causing it to loop endlessly.                               
   }                                                                       
   finally                                                                 
   {                                                                       
      if ( br != null )                                                    
      {                                                                    
         try                                                               
         {                                                                 
            br.close();                                                    
         }                                                                 
         catch ( IOException ex )                                          
         {                                                                 
            System.err.println( ex );                                      
            ex.printStackTrace( System.err );                              
         }                                                                 
      }                                                                    
   }                                                                       
}                                                                          
                                                                           
public void example2() throws IOException                                  
{                                                                          
   File file = new File( "input.dat" );                                    
   FileInputStream fis = null;                                             
                                                                           
   try                                                                     
   {                                                                       
      fis = new FileInputStream( file );                                   
      int length = ( int ) file.length();                                  
      byte[] buffer = new byte[ length ];                                  
                                                                           
      int offset = 0;                                                      
      int read;                                                            
      while ( ( read = fis.read( buffer, offset, length - offset ) ) != -1 )
      {                                                                    
         offset += read;                                                   
      }                                                                    
      // BUG: Read length could be zero.                                   
      //                                                                   
      // This is another example of the pattern. It will loop endlessly    
      // regardless of the length of the input file.                       
   }                                                                       
   finally                                                                 
   {                                                                       
      if ( fis != null )                                                   
      {                                                                    
         try                                                               
         {                                                                 
            fis.close();                                                   
         }                                                                 
         catch ( IOException ex )                                          
         {                                                                 
            System.err.println( ex );                                      
            ex.printStackTrace( System.err );                              
         }                                                                 
      }                                                                    
   }                                                                       
}                                                                          
Thanks,
 
DS

_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

Re: Proposed FindBugs pattern

by Kautz, Don :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

It seems to me that this is the kind of thing that unit tests are designed to catch.  “Give a man a hammer and everything becomes a nail.”

 

Just my (unsolicited) two cents.

 

Don Kautz

 


From: findbugs-discuss-bounces@... [mailto:findbugs-discuss-bounces@...] On Behalf Of JavaOne
Sent: Monday, June 09, 2008 4:38 PM
To: findbugs-discuss@...
Subject: [FB-Discuss] Proposed FindBugs pattern

 

public void example1() throws IOException                                  
{                                                                          
   StringBuffer stringBuffer = new StringBuffer();                         
   File file = new File( "input.dat" );                                    
   BufferedReader br = null;                                               
                                                                           
   try                                                                     
   {                                                                       
      br = new BufferedReader( new FileReader( file ) );                   
      int length = ( int ) file.length();                                  
      char[] buffer = new char[ length ];                                  
                                                                           
      int read;                                                            
      while ( ( read = br.read( buffer ) ) != -1 )                         
      {                                                                    
         stringBuffer.append( buffer, 0, read );                           
      }                                                                    
      // BUG: Read length could be zero.                                   
      //                                                                   
      // The buffer is created based on the size of a file. If the file    
      // happens to be empty, the buffer will have a length of zero.       
      // Every read into a zero-length buffer will immediately return      
      // zero, causing it to loop endlessly.                               
   }                                                                       
   finally                                                                 
   {                                                                       
      if ( br != null )                                                    
      {                                                                    
         try                                                               
         {                                                                 
            br.close();                                                    
         }                                                                 
         catch ( IOException ex )                                          
         {                                                                 
            System.err.println( ex );                                      
            ex.printStackTrace( System.err );                              
         }                                                                 
      }                                                                    
   }                                                                       
}                                                                          
                                                                           
public void example2() throws IOException                                  
{                                                                          
   File file = new File( "input.dat" );                                    
   FileInputStream fis = null;                                             
                                                                           
   try                                                                     
   {                                                                       
      fis = new FileInputStream( file );                                   
      int length = ( int ) file.length();                                  
      byte[] buffer = new byte[ length ];                                  
                                                                           
      int offset = 0;                                                      
      int read;                                                            
      while ( ( read = fis.read( buffer, offset, length - offset ) ) != -1 )
      {                                                                    
         offset += read;                                                   
      }                                                                    
      // BUG: Read length could be zero.                                   
      //                                                                   
      // This is another example of the pattern. It will loop endlessly    
      // regardless of the length of the input file.                       
   }                                                                       
   finally                                                                 
   {                                                                       
      if ( fis != null )                                                   
      {                                                                    
         try                                                               
         {                                                                 
            fis.close();                                                   
         }                                                                 
         catch ( IOException ex )                                          
         {                                                                 
            System.err.println( ex );                                      
            ex.printStackTrace( System.err );                              
         }                                                                 
      }                                                                    
   }                                                                       
}                                                                          

Thanks,

 

DS


_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

Parent Message unknown Re: Proposed FindBugs pattern

by Kautz, Don :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

In our shop unit tests are mandatory.  But there are still plenty of issues that FindBugs can turn up that would not be caught by the unit tests.  Much of the Correctness and Dodgy categories and all of the Performance category deal with issues of robustness. On the other hand, static analysis can never ensure that the code performs as intended.  Unit tests serve as a safety net to catch errors that may be introduced during refactoring.  But static analysis hones our programming practice to make it more efficient.

 

So, no, I don’t agree that the purpose of static analysis is to find bugs absent test coverage because the test coverage shouldn’t be absent.

 

Don


From: Nils Kilden-Pedersen [mailto:nilskp@...]
Sent: Tuesday, June 10, 2008 8:49 AM
To: Kautz, Don
Cc: findbugs-discuss@...
Subject: Re: [FB-Discuss] Proposed FindBugs pattern

 

On Tue, Jun 10, 2008 at 6:55 AM, Kautz, Don <Donald.Kautz@...> wrote:

It seems to me that this is the kind of thing that unit tests are designed to catch.  "Give a man a hammer and everything becomes a nail."

Why use FindBugs at all then? Anything FindBugs can detect can be caught by unit tests. Isn't the purpose of static analysis to find potential bugs absent test coverage?


_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

Re: Proposed FindBugs pattern

by Nils Kilden-Pedersen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jun 10, 2008 at 8:26 AM, Kautz, Don <Donald.Kautz@...> wrote:

In our shop unit tests are mandatory.  

But to what level of coverage?
 

But there are still plenty of issues that FindBugs can turn up that would not be caught by the unit tests.  Much of the Correctness and Dodgy categories and all of the Performance category deal with issues of robustness. On the other hand, static analysis can never ensure that the code performs as intended.  Unit tests serve as a safety net to catch errors that may be introduced during refactoring.  But static analysis hones our programming practice to make it more efficient.

I'm not necessarily disagreeing with you on that, but the proposed FindBugs check was for something that can be notoriously hard to unit test, namely an exceptional condition.
 

So, no, I don't agree that the purpose of static analysis is to find bugs absent test coverage because the test coverage shouldn't be absent.

While completely correct, assumes lack of human laziness, error, impossible condition, etc. that prevents us from achieving 100% unit test coverage.


_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

Findbugs timeout if running after clover

by Scott Wolk-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Findbugs timeout if running after clover We use findbugs and clover as part of our cruise control CI for nightly builds. I’ve found if findbugs is run after clover then it times out. I was thinking that it most likely due to findbugs not having clover.jar in it’s classpath. Has anyone else seen this behavior?

Thanks,
Scott

_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss
LightInTheBox - Buy quality products at wholesale price