-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCloseWriter.ql
More file actions
38 lines (35 loc) · 982 Bytes
/
CloseWriter.ql
File metadata and controls
38 lines (35 loc) · 982 Bytes
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
/**
* @name Potential output resource leak
* @description A resource that is opened for writing but not closed may cause a resource
* leak.
* @kind problem
* @problem.severity error
* @precision high
* @id java/output-resource-leak
* @tags efficiency
* correctness
* resources
* external/cwe/cwe-404
* external/cwe/cwe-772
*/
import CloseType
predicate writerType(RefType t) {
exists(RefType sup | sup = t.getASupertype*() |
sup.hasName("Writer") or
sup.hasName("OutputStream")
)
}
predicate safeWriterType(RefType t) {
exists(RefType sup | sup = t.getASupertype*() |
sup.hasName("StringWriter") or
sup.hasName("ByteArrayOutputStream")
)
}
from ClassInstanceExpr cie, RefType t
where
badCloseableInit(cie) and
cie.getType() = t and
writerType(t) and
not safeWriterType(typeInDerivation(cie)) and
not noNeedToClose(cie)
select cie, "This " + t.getName() + " is not always closed on method exit."