|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
Proposed FindBugs patternpublic 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 patternIt 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 public void example1() throws
IOException
Thanks, DS _______________________________________________ Findbugs-discuss mailing list Findbugs-discuss@... https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss |
|
|
|
|
|
Re: Proposed FindBugs patternOn Tue, Jun 10, 2008 at 8:26 AM, Kautz, Don <Donald.Kautz@...> wrote:
But to what level of coverage?
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.
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 cloverThanks, Scott _______________________________________________ Findbugs-discuss mailing list Findbugs-discuss@... https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss |
| Free Forum Powered by Nabble | Forum Help |