False positive? [OBL] Method may fail to clean up stream or resource [OBL_UNSATISFIED_OBLIGATION]

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

False positive? [OBL] Method may fail to clean up stream or resource [OBL_UNSATISFIED_OBLIGATION]

by Tim Keith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Findbugs is reporting [OBL] Method may fail to clean up stream or resource [OBL_UNSATISFIED_OBLIGATION] for this code:

        Reader reader = new InputStreamReader(new FileInputStream(file), UTF8);

        try {
            ...
        } finally {
            reader.close();
        }

Doesn't closing the Reader close the underlying InputStream?

-- Tim

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

Re: False positive? [OBL] Method may fail to clean upstream or resource [OBL_UNSATISFIED_OBLIGATION]

by Dave Brosius-2 :: 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.
Theoretically the InputStreamReader's constructor could throw an exception after the FileInputStream was created. Thus the finally block is never executed, leaving the FileInputStream open.
 
 
----- Original Message -----
Sent: Tuesday, September 30, 2008 7:48 PM
Subject: [FB-Discuss] False positive? [OBL] Method may fail to clean upstream or resource [OBL_UNSATISFIED_OBLIGATION]

Findbugs is reporting [OBL] Method may fail to clean up stream or resource [OBL_UNSATISFIED_OBLIGATION] for this code:

        Reader reader = new InputStreamReader(new FileInputStream(file), UTF8);

        try {
            ...
        } finally {
            reader.close();
        }

Doesn't closing the Reader close the underlying InputStream?

-- Tim


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

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

Re: False positive? [OBL] Method may fail to clean upstream or resource [OBL_UNSATISFIED_OBLIGATION]

by Noel Grandin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So what is the "findbugs approved" version?

Thanks, Noel.

On Wed, Oct 1, 2008 at 04:38, Dave Brosius <dbrosius@...> wrote:
> Theoretically the InputStreamReader's constructor could throw an exception
> after the FileInputStream was created. Thus the finally block is never
> executed, leaving the FileInputStream open.
>
>
_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

Re: False positive? [OBL] Method may fail to clean upstream or resource [OBL_UNSATISFIED_OBLIGATION]

by Thomas Hawtin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Noel Grandin wrote:
> So what is the "findbugs approved" version?

This should pass:

         InputStream fileIn = new FileInputStream(file);
         try {
             Reader reader = new InputStreamReader(fileIn, UTF8);
             ...
         } finally {
             fileIn.close();
         }

Of course, if you are writing it often you can write it once with the
"execute around" idiom.

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

Re: False positive? [OBL] Method may fail to clean upstream or resource [OBL_UNSATISFIED_OBLIGATION]

by Thomas Suckow :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

FileInputStream fis = null;
try
{
    fis = new FileInputStream(file);
    Reader reader = null;
    try
    {
        reader = new InputStreamReader(fis, UTF8);
    } finally {
        if( reader != null ) reader.close();
    }
}
finally
{
    if( fis != null) fis.close();
}


Thomas Suckow



On Wed, Oct 1, 2008 at 8:26 AM, Noel Grandin <noelgrandin@...> wrote:

> So what is the "findbugs approved" version?
>
> Thanks, Noel.
>
> On Wed, Oct 1, 2008 at 04:38, Dave Brosius <dbrosius@...> wrote:
>> Theoretically the InputStreamReader's constructor could throw an exception
>> after the FileInputStream was created. Thus the finally block is never
>> executed, leaving the FileInputStream open.
>>
>>
> _______________________________________________
> Findbugs-discuss mailing list
> Findbugs-discuss@...
> https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss
>
_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss
LightInTheBox - Buy quality products at wholesale price!