FindBugs OBL_UNSATISFIED_OBLIGATION
Asked Answered
C

2

7

I'm trying to find bugs in on of our legacy code using findBugs. In one the methods, findBugs is giving OBL_UNSATISFIED_OBLIGATION error. I have verified that all streams are properly closed. Here is the code snippet:

FileWriter fw = null;
FileReader fr = null;
try {
    if (!new File(filePath).exists()) {
        requiredStrings = CommandUtils.invoke(filename);
        fw = new FileWriter(filePath);
        fw.write(requiredStrings);
    } else {               
        StringBuilder sb = new StringBuilder();
        fr = new FileReader(filePath);

        char[] buffer = new char[BLOCK_READ_SIZE];
        int bytesRead;
        while (-1 != (bytesRead = fr.read(buffer, 0, BLOCK_READ_SIZE))) {
            sb.append(buffer, 0, bytesRead);
        }
        requiredStrings = sb.toString();
    }
} finally {
    if (fw != null) {
        fw.close();
    }
    if (fr != null) {
        fr.close();
    }
}
return requiredStrings;

The error says that Obligation to clean up resurces in not discharged, Path continues at ....line.... Remaining obligations {Reader x 1, Writer x-1}

Conjuration answered 21/9, 2013 at 18:10 Comment(2)
Maybe it's complaining that you don't catch exceptions from close() so that fr might be left unclosed if fw.close() throws. Also, " the false-positive suppression heuristics for this bug pattern have not been extensively tuned, so reports about false positives are helpful to us."Confessedly
See this:[Java closing connections and findbugs][1] [1]: #4398886Adest
S
0

You have to catch IO exceptions throwed by FileReader and FileWriter when they are closing. You can do that in Java 7 and upper with try with resources

try (FileWriter fw = new FileWriter(filePath); FileReader fr = new FileReader(filePath)) {
    /*your code here*/
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Or by the old way

    FileWriter fw = null;
    FileReader fr = null;
    try {
        /*your code here*/
        fw = new FileWriter(filePath);
        /*your code here*/
        fr = new FileReader(filePath);
        /*your code here*/
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fw != null) {
                fw.close();
            }
            if (fr != null) {
                fr.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Stilu answered 29/10, 2017 at 19:56 Comment(0)
S
0

In a method, it opens a InputStream for reading, if there is a chance the method quits without closing this InputStream object, FindBugs would complain like fail to clean up java.io.InputStream on checked exception. For example:

void readProperties() throws FooException{
    InputStream is= ...
    PropertyFactory.getInstance().loadXXXFromPropertyStream(is); // it throws FooException
    is.close(); // maybe never called for a FooException leaving inputstream is open.
}
Sheers answered 9/3, 2018 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.