-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCloseReader.ql
More file actions
44 lines (40 loc) · 1.23 KB
/
CloseReader.ql
File metadata and controls
44 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @name Potential input resource leak
* @description A resource that is opened for reading but not closed may cause a resource
* leak.
* @kind problem
* @problem.severity warning
* @precision high
* @id java/input-resource-leak
* @tags quality
* reliability
* performance
* efficiency
* resources
* external/cwe/cwe-404
* external/cwe/cwe-772
*/
import CloseType
predicate readerType(RefType t) {
exists(RefType sup | sup = t.getAnAncestor() |
sup.hasQualifiedName("java.io", ["Reader", "InputStream"]) or
sup.hasQualifiedName("java.util.zip", "ZipFile")
)
}
predicate safeReaderType(RefType t) {
exists(RefType sup | sup = t.getAnAncestor() |
sup.hasQualifiedName("java.io", ["CharArrayReader", "StringReader", "ByteArrayInputStream"])
or
// Note: It is unclear which specific class this is supposed to match
sup.hasName("StringInputStream")
)
}
from ClassInstanceExpr cie, RefType t
where
cie.getFile().isJavaSourceFile() and
badCloseableInit(cie) and
cie.getType() = t and
readerType(t) and
not safeReaderType(typeInDerivation(cie)) and
not noNeedToClose(cie)
select cie, "This " + t.getName() + " is not always closed on method exit."