Skip to content

Commit 479f6f5

Browse files
intrigus-lgtmripatel-fd
authored andcommitted
codeql: query for useless memcpy + cleanup
Add query for useless memcpy and use the same memcpy definition across all queries.
1 parent b2bf2f2 commit 479f6f5

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

contrib/codeql/nightly/TrivialMemcpy.ql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@
1414

1515
import cpp
1616
import filter
17-
18-
class MemcpyFunction extends Function {
19-
MemcpyFunction() {
20-
this.hasGlobalOrStdName("memcpy")
21-
or
22-
this.hasGlobalName(["fd_memcpy", "__builtin_memcpy"])
23-
}
24-
}
17+
import fd_memcpy
2518

2619
predicate ignoredLocation(Location l) {
2720
// we don't want to change vendored code if not really necessary

contrib/codeql/nightly/TrivialMemcpyWrong.ql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@
1313

1414
import cpp
1515
import filter
16-
17-
class MemcpyFunction extends Function {
18-
MemcpyFunction() {
19-
this.hasGlobalOrStdName("memcpy")
20-
or
21-
this.hasGlobalName(["fd_memcpy", "__builtin_memcpy"])
22-
}
23-
}
16+
import fd_memcpy
2417

2518
class NotVoidChar extends Type {
2619
NotVoidChar() {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @name Suspicious/useless `memcpy(foo, foo, ...)` call.
3+
* @description `memcpy` is called with the same source and destination pointer.
4+
* This is likely a bug or at best useless code.
5+
* @kind problem
6+
* @id asymmetric-research/useless-memcpy
7+
* @problem.severity warning
8+
* @precision high
9+
* @tags correctness
10+
*/
11+
12+
import cpp
13+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
14+
import fd_memcpy
15+
16+
predicate isSamePointer(Expr e1, Expr e2) { globalValueNumber(e1) = globalValueNumber(e2) }
17+
18+
from MemcpyFunction memcpy, FunctionCall call
19+
where
20+
call.getTarget() = memcpy and
21+
isSamePointer(call.getArgument(0), call.getArgument(1))
22+
select call,
23+
"Call to " + memcpy.getName() +
24+
" has the same source and destination. This is likely a bug or useless code."
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import cpp
2+
3+
/**
4+
* A memcpy function:
5+
* - `memcpy` from `<string.h>`
6+
* - `fd_memcpy` from `fd_util_base.h`
7+
* - `__builtin_memcpy`
8+
*/
9+
class MemcpyFunction extends Function {
10+
MemcpyFunction() {
11+
this.hasGlobalOrStdName("memcpy")
12+
or
13+
this.hasGlobalName(["fd_memcpy", "__builtin_memcpy"])
14+
}
15+
}

0 commit comments

Comments
 (0)