Skip to content

Commit

Permalink
Bug 561334 - [resource] leak should be classified as potential
Browse files Browse the repository at this point in the history
without annotation support:
+ mark as shared, to change problem to MandatoryCloseNotShown
+ move this and sibling to irritant PotentiallyUnclosedCloseable
with annotation support:
+ nothing changed, since we assume OWNED_BY_DEFAULT
  • Loading branch information
stephan-herrmann committed Dec 11, 2024
1 parent c2a99e6 commit 3fae212
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public static FlowInfo analyseCloseableAcquisition(BlockScope scope, FlowInfo fl
acquisition.closeTracker = tracker;
}
tracker.acquisition = acquisition;
tracker.globalClosingState |= SHARED_WITH_OUTSIDE;
FlowInfo outsideInfo = flowInfo.copy();
outsideInfo.markAsDefinitelyNonNull(tracker.binding);
tracker.markNullStatus(flowInfo, flowContext, FlowInfo.NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,9 @@ public static int getIrritant(int problemID) {

case IProblem.UnclosedCloseable:
case IProblem.UnclosedCloseableAtExit:
return CompilerOptions.UnclosedCloseable;
case IProblem.MandatoryCloseNotShown:
case IProblem.MandatoryCloseNotShownAtExit:
return CompilerOptions.UnclosedCloseable;
case IProblem.PotentiallyUnclosedCloseable:
case IProblem.PotentiallyUnclosedCloseableAtExit:
return CompilerOptions.PotentiallyUnclosedCloseable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1949,8 +1949,8 @@ class ProblemAttributes {
expectedProblemAttributes.put("LocalVariableIsNeverUsed", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_LOCAL));
expectedProblemAttributes.put("LocalVariableMayBeNull", SKIP);
expectedProblemAttributes.put("MaskedCatch", new ProblemAttributes(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK));
expectedProblemAttributes.put("MandatoryCloseNotShown", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE));
expectedProblemAttributes.put("MandatoryCloseNotShownAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE));
expectedProblemAttributes.put("MandatoryCloseNotShown", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
expectedProblemAttributes.put("MandatoryCloseNotShownAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
expectedProblemAttributes.put("MessageSendWithUnresolvedOwningAnnotation", SKIP);
expectedProblemAttributes.put("MethodButWithConstructorName", new ProblemAttributes(JavaCore.COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME));
expectedProblemAttributes.put("MethodCanBePotentiallyStatic", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected String getTest056e_log() {
protected String getTest056y_log() {
return """
----------
1. ERROR in X.java (at line 4)
1. WARNING in X.java (at line 4)
final FileReader reader31 = new FileReader("file");
^^^^^^^^
Mandatory close of resource 'reader31' has not been shown
Expand Down Expand Up @@ -199,6 +199,25 @@ protected String getTestBug440282_log() {
"----------\n";
}

@Override
String getBug561334_log() {
// 1. info is only reported when annotations are enabled
// 2. warning is more severe since we set OWNED_BY_DEFAULT on the resource from getFoo()
return """
----------
1. INFO in Foo.java (at line 18)
foo = new Foo("Hello, world!");
^^^^^^^^^^^^^^^^^^^^^^^^
Mandatory close of resource '<unassigned Closeable value>' has not been shown
----------
2. WARNING in Foo.java (at line 21)
System.out.println(getFoo().thing);
^^^^^^^^
Resource leak: '<unassigned Closeable value>' is never closed
----------
""";
}

public void testBug411098_comment19_annotated() {
runLeakTestWithAnnotations(
new String[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7295,4 +7295,52 @@ public static void main(String[] args) throws Exception {
""",
options);
}
public void testBug561334() {
Map<String, String> compilerOptions = getCompilerOptions();
compilerOptions.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.INFO);
runLeakWarningTest(
new String[] {
"Foo.java",
"""
public class Foo implements AutoCloseable {
private final String thing;
public Foo(String thing) {
this.thing = thing;
}
@Override
public void close() {
//
}
private static Foo foo;
private static Foo getFoo() {
return foo;
}
public static void main(String[] args) {
foo = new Foo("Hello, world!");
try {
System.out.println(foo.thing);
System.out.println(getFoo().thing);
} finally {
getFoo().close();
}
}
}
"""
},
getBug561334_log(),
compilerOptions);
}
String getBug561334_log() {
return """
----------
1. INFO in Foo.java (at line 21)
System.out.println(getFoo().thing);
^^^^^^^^
Mandatory close of resource '<unassigned Closeable value>' has not been shown
----------
""";
}
}

0 comments on commit 3fae212

Please sign in to comment.