Fix cases of resource leaks not detected when the resource indirectly implements Closeable

Summary:public
Before this diff, the Java frontend was not adding the definition of the inherited interfaces to the type environment, thus failing to answer questions like "does type X implements Closeable". Infer was therefore missing to detect resource leaks when the resource was indirectly implementing Closeable via an intermediate interface.

Reviewed By: sblackshear

Differential Revision: D3067555

fb-gh-sync-id: 86d0760
shipit-source-id: 86d0760
master
jrm 9 years ago committed by Facebook Github Bot 6
parent fb5c5c8515
commit e734c1873d

@ -312,6 +312,7 @@ and create_sil_type program tenv cn =
| None -> dummy_type cn | None -> dummy_type cn
| Some node -> | Some node ->
let create_super_list interface_names = let create_super_list interface_names =
IList.iter (fun cn -> ignore (get_class_type_no_pointer program tenv cn)) interface_names;
IList.map typename_of_classname interface_names in IList.map typename_of_classname interface_names in
let superclasses, instance_fields, static_fields, struct_annotations = let superclasses, instance_fields, static_fields, struct_annotations =
match node with match node with

@ -399,6 +399,11 @@
"file": "codetoanalyze/java/infer/ResourceLeaks.java", "file": "codetoanalyze/java/infer/ResourceLeaks.java",
"procedure": "void ResourceLeaks.jarOutputStreamLeak()" "procedure": "void ResourceLeaks.jarOutputStreamLeak()"
}, },
{
"bug_type": "RESOURCE_LEAK",
"file": "codetoanalyze/java/infer/CloseableAsResourceExample.java",
"procedure": "void CloseableAsResourceExample.leakFoundWhenIndirectlyImplementingCloseable()"
},
{ {
"bug_type": "RESOURCE_LEAK", "bug_type": "RESOURCE_LEAK",
"file": "codetoanalyze/java/infer/CursorLeaks.java", "file": "codetoanalyze/java/infer/CursorLeaks.java",

@ -399,6 +399,11 @@
"file": "infer/tests/codetoanalyze/java/infer/ResourceLeaks.java", "file": "infer/tests/codetoanalyze/java/infer/ResourceLeaks.java",
"procedure": "void ResourceLeaks.jarOutputStreamLeak()" "procedure": "void ResourceLeaks.jarOutputStreamLeak()"
}, },
{
"bug_type": "RESOURCE_LEAK",
"file": "infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java",
"procedure": "void CloseableAsResourceExample.leakFoundWhenIndirectlyImplementingCloseable()"
},
{ {
"bug_type": "RESOURCE_LEAK", "bug_type": "RESOURCE_LEAK",
"file": "infer/tests/codetoanalyze/java/infer/CursorLeaks.java", "file": "infer/tests/codetoanalyze/java/infer/CursorLeaks.java",

@ -149,4 +149,14 @@ public class CloseableAsResourceExample {
return null; return null;
} }
interface MyCloseable extends Closeable {}
class MyResource implements MyCloseable {
public void close() {}
}
void leakFoundWhenIndirectlyImplementingCloseable() {
MyResource res = new MyResource();
}
} }

@ -43,6 +43,7 @@ public class CloseableAsResourceTest {
"notClosingWrapper", "notClosingWrapper",
"failToCloseWithCloseQuietly", "failToCloseWithCloseQuietly",
"sourceOfNullWithResourceLeak", "sourceOfNullWithResourceLeak",
"leakFoundWhenIndirectlyImplementingCloseable",
}; };
assertThat( assertThat(
"Results should not contain resource leak errors", "Results should not contain resource leak errors",

Loading…
Cancel
Save