-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnot-using-ByteArrayOutputStream-writeTo.ql
More file actions
35 lines (31 loc) · 1.27 KB
/
Copy pathnot-using-ByteArrayOutputStream-writeTo.ql
File metadata and controls
35 lines (31 loc) · 1.27 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
/**
* Finds usage of `ByteArrayOutputStream` where the written data is obtained
* using `toByteArray()` and then later written to another `OutputStream`.
* In these cases `ByteArrayOutputStream.writeTo` should be preferred because
* unlike `toByteArray()` it avoids creating a copy of the internal buffer.
*
* @kind problem
*/
import java
import semmle.code.java.dataflow.DataFlow
class ToByteArrayMethod extends Method {
ToByteArrayMethod() {
getDeclaringType().hasQualifiedName("java.io", "ByteArrayOutputStream") and
hasStringSignature("toByteArray()")
}
}
class OutputStreamWriteMethod extends Method {
OutputStreamWriteMethod() {
getDeclaringType().getASourceSupertype*().hasQualifiedName("java.io", "OutputStream") and
hasStringSignature("write(byte[])")
}
}
from MethodAccess toByteArrayCall, MethodAccess writeCall
where
toByteArrayCall.getMethod() instanceof ToByteArrayMethod and
writeCall.getMethod() instanceof OutputStreamWriteMethod and
// TODO: Using dataflow causes some false positives when array is additionally used in other ways
DataFlow::localExprFlow(toByteArrayCall, writeCall.getArgument(0))
select toByteArrayCall,
"Could use `ByteArrayOutputStream.writeTo` instead of manually writing to `OutputStream` $@",
writeCall, "here"