Skip to content

Commit

Permalink
+ recognize first param of WrapperResource-ctor as implicitly @owning
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-herrmann committed Jan 9, 2024
1 parent 289c60d commit a98d151
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,9 @@ public boolean hasPolymorphicSignature(Scope scope) {
public boolean ownsParameter(int i) {
if (this.parameterFlowBits != null)
return (this.parameterFlowBits[i] & PARAM_OWNING) != 0;
if (i == 0 && this.parameters.length > 0 && this.declaringClass.hasTypeBit(TypeIds.BitWrapperCloseable)) {
return this.parameters[0].hasTypeBit(TypeIds.BitAutoCloseable);
}
return false;
}
public boolean notownsParameter(int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1289,4 +1289,52 @@ void test3(String name) throws Exception {
false);

}
public void testSubclassingWrapperResource() {
runLeakTestWithAnnotations(
new String[] {
"p1/C.java",
"""
package p1;
import org.eclipse.jdt.annotation.*;
import java.io.*;
public class C extends BufferedInputStream {
public C(@Owning InputStream input) {
super(input); // should not complain, super param is implicilty @Owning
}
static void test1(@Owning InputStream input) {
C c = new C(input);
}
static void test2(@Owning InputStream input) throws Exception {
C c = new C(input);
input.close(); // now C is resource-less
}
static void test3(String name) throws Exception {
FileInputStream fis = new FileInputStream(name);
C c = new C(fis);
if (name == null)
fis.close();
}
}
"""
},
"""
----------
1. ERROR in p1\\C.java (at line 9)
C c = new C(input);
^
Resource leak: 'c' is never closed
----------
2. INFO in p1\\C.java (at line 12)
C c = new C(input);
^
Resource 'c' should be managed by try-with-resource
----------
3. ERROR in p1\\C.java (at line 17)
C c = new C(fis);
^
Potential resource leak: 'c' may not be closed
----------
""",
null);
}
}

0 comments on commit a98d151

Please sign in to comment.