From 789eeb90ee0e4a5b167be3db70baae047d8e7b39 Mon Sep 17 00:00:00 2001 From: Stephan Herrmann Date: Sat, 25 Aug 2012 23:37:02 +0200 Subject: Bug 385415 - Incorrect resource leak detection --- .../compiler/regression/ResourceLeakTests.java | 122 ++++++++++++++++++++- .../compiler/ast/FakedTrackingVariable.java | 3 +- 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java index beeb07b6fc..f991f10594 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java @@ -26,7 +26,7 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; public class ResourceLeakTests extends AbstractRegressionTest { static { -// TESTS_NAMES = new String[] { "test066"}; +// TESTS_NAMES = new String[] { "testBug385415" }; // TESTS_NUMBERS = new int[] { 50 }; // TESTS_RANGE = new int[] { 11, -1 }; } @@ -3710,4 +3710,124 @@ public void test075() { "Resource leak: 'fileReader' is never closed\n" + "----------\n"); } +// Bug 385415 - Incorrect resource leak detection +public void testBug385415() { + Map options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); + runConformTest( + new String[] { + "X.java", + "import java.io.*;\n" + + "public class X {\n" + + " void foo() throws FileNotFoundException {\n" + + " FileReader fileReader = new FileReader(\"somefile\");\n" + + " try {\n" + + " fileReader.close();\n" + + " } catch (Exception e) {\n" + + " e.printStackTrace();\n" + + " return;\n" + + " }\n" + + " }\n" + + "}\n" + }, + "", + null, + true, + null, + options, + null); +} + +// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly +// test case from comment 7 +// Duplicate of Bug 385415 - Incorrect resource leak detection +public void testBug361073c7() { + Map options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); + runConformTest( + new String[] { + "X.java", + "import java.io.*;\n" + + "public class X {\n" + + " public void test() {\n" + + " BufferedReader br = null;\n" + + " try {\n" + + " br = new BufferedReader(new FileReader(\"blah\"));\n" + + " String line = null;\n" + + " while ( (line = br.readLine()) != null ) {\n" + + " if ( line.startsWith(\"error\") )\n" + + " throw new Exception(\"error\"); //Resource leak: 'br' is not closed at this location\n" + + " }\n" + + " } catch (Throwable t) {\n" + + " t.printStackTrace();\n" + + " } finally {\n" + + " if ( br != null ) {\n" + + " try { br.close(); }\n" + + " catch (Throwable e) { br = null; }\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + }, + "", + null, + true, + null, + options, + null); +} + +// Bug 386534 - "Potential resource leak" false positive warning +// DISABLED +public void _testBug386534() { + Map options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); + runConformTest( + new String[] { + "Bug.java", + "import java.io.FileNotFoundException;\n" + + "import java.io.IOException;\n" + + "import java.io.OutputStream;\n" + + "\n" + + "public class Bug {\n" + + " private static final String DETAILS_FILE_NAME = null;\n" + + " private static final String LOG_TAG = null;\n" + + " private static Context sContext;\n" + + " static void saveDetails(byte[] detailsData) {\n" + + " OutputStream os = null;\n" + + " try {\n" + + " os = sContext.openFileOutput(DETAILS_FILE_NAME,\n" + + " Context.MODE_PRIVATE);\n" + + " os.write(detailsData);\n" + + " } catch (IOException e) {\n" + + " Log.w(LOG_TAG, \"Unable to save details\", e);\n" + + " } finally {\n" + + " if (os != null) {\n" + + " try {\n" + + " os.close();\n" + + " } catch (IOException ignored) {\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " static class Context {\n" + + " public static final String MODE_PRIVATE = null;\n" + + " public OutputStream openFileOutput(String detailsFileName,\n" + + " String modePrivate) throws FileNotFoundException{\n" + + " return null;\n" + + " }\n" + + " }\n" + + " static class Log {\n" + + " public static void w(String logTag, String string, IOException e) {\n" + + " }\n" + + " }\n" + + "}\n" + }, + "", + null, + true, + null, + options, + null); +} } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java index 102fd01df5..f65ca7e9fa 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java @@ -587,9 +587,8 @@ public class FakedTrackingVariable extends LocalDeclaration { do { flowInfo.markAsDefinitelyNonNull(current.binding); current.globalClosingState |= CLOSE_SEEN; -//TODO(stephan): this might be useful, but I could not find a test case for it: if (flowContext.initsOnFinally != null) - flowContext.initsOnFinally.markAsDefinitelyNonNull(this.binding); + flowContext.markFinallyNullStatus(this.binding, FlowInfo.NON_NULL); current = current.innerTracker; } while (current != null); } -- cgit v1.2.3