...
This compliant solution releases all acquired resources, regardless of any exceptions that might occur. Even though dereferencing bufRead
might result in an exception, the FileInputStream
object is closed as required (if it was created in the first place).
Code Block | ||
---|---|---|
| ||
try { final FileInputStream stream = nullnew FileInputStream(fileName); BufferedReader bufRead = null; try { stream = new FileInputStream(fileName); final BufferedReader bufRead = new BufferedReader(new InputStreamReader(stream)); String line; while ((line = bufRead.readLine()) != null) { sendLine(line); } } catch (IOException e) { // forward to handler } } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // forward to handler } } } } catch (IOException e) { // forward to handler } |
Compliant Solution (Java 1.7, try-with-resources)
...
FIO42-C. Ensure files are properly closed when they are no longer needed | |||||
FIO42-CPP. Ensure files are properly closed when they are no longer needed | |||||
| CWE-404 "Improper Resource Shutdown or Release" | ||||
| CWE-459 "Incomplete Cleanup" | ||||
| CWE-770 "Allocation of Resources Without Limits or Throttling" | ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="345808f80adc07dd-461db535-49554855-ab379bd2-be972a4fcf5dc30d9963291a"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE-405 | http://cwe.mitre.org/data/definitions/405.html] "Asymmetric Resource Consumption (Amplification)" | ]]></ac:plain-text-body></ac:structured-macro> |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="124862462f39405f-2e68ddc7-4e7d426b-8d8ba686-8a28b9d7d50d4600471a82c5"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Class Object | http://java.sun.com/javase/6/docs/api/java/lang/Object.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5478120e36740d3e-d6a49a5e-425644ea-80db9b9a-c679515032371339293bc6ef"><ac:plain-text-body><![CDATA[ | [[Goetz 2006b | AA. Bibliography#Goetz 06b]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="35c711ad36d70223-faf1dc9b-4f9f49b4-b78ba4a4-3092d712c069459993c0cdd7"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. Bibliography#J2SE 11]] | The try-with-resources Statement | ]]></ac:plain-text-body></ac:structured-macro> |
...