Skip to content

Commit 1479586

Browse files
authored
Merge pull request github#1303 from jbj/hasQualifiedName
C++: Fix `getQualifiedName` performance issues
2 parents 6a198ff + d820fc9 commit 1479586

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+743
-298
lines changed

change-notes/1.21/analysis-cpp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
| `()`-declared function called with too many arguments (`cpp/futile-params`) | Improved coverage | Query has been generalized to find all cases where the number of arguments exceedes the number of parameters of the function, provided the function is also properly declared/defined elsewhere. |
3131

3232
## Changes to QL libraries
33+
- The predicate `Declaration.hasGlobalName` now only holds for declarations that are not nested in a class. For example, it no longer holds for a member function `MyClass::myFunction` or a constructor `MyClass::MyClass`, whereas previously it would classify those two declarations as global names.
34+
- In class `Declaration`, predicates `getQualifiedName/0` and `hasQualifiedName/1` are no longer recommended for finding functions by name. Instead, use `hasGlobalName/1` and the new `hasQualifiedName/2` and `hasQualifiedName/3` predicates. This improves performance and makes it more reliable to identify names involving templates.
3335
- Additional support for definition by reference has been added to the `semmle.code.cpp.dataflow.TaintTracking` library.
3436
- The taint tracking library now includes taint-specific edges for functions modeled in `semmle.code.cpp.models.interfaces.DataFlow`.
3537
- The taint tracking library adds flow through library functions that are modeled in `semmle.code.cpp.models.interfaces.Taint`. Queries can add subclasses of `TaintFunction` to specify additional flow.

cpp/ql/src/Critical/DescriptorMayNotBeClosed.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import semmle.code.cpp.pointsto.PointsTo
1313
import Negativity
1414

1515
predicate closeCall(FunctionCall fc, Variable v) {
16-
fc.getTarget().hasQualifiedName("close") and v.getAnAccess() = fc.getArgument(0)
16+
fc.getTarget().hasGlobalName("close") and v.getAnAccess() = fc.getArgument(0)
1717
or
1818
exists(FunctionCall midcall, Function mid, int arg |
1919
fc.getArgument(arg) = v.getAnAccess() and

cpp/ql/src/Critical/DescriptorNeverClosed.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import semmle.code.cpp.pointsto.PointsTo
1313

1414
predicate closed(Expr e) {
1515
exists(FunctionCall fc |
16-
fc.getTarget().hasQualifiedName("close") and
16+
fc.getTarget().hasGlobalName("close") and
1717
fc.getArgument(0) = e
1818
)
1919
}

cpp/ql/src/Critical/GlobalUseBeforeInit.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ predicate useFunc(GlobalVariable v, Function f) {
3030
}
3131

3232
predicate uninitialisedBefore(GlobalVariable v, Function f) {
33-
f.hasQualifiedName("main")
33+
f.hasGlobalName("main")
3434
or
3535
exists(Call call, Function g |
3636
uninitialisedBefore(v, g) and

cpp/ql/src/Critical/InitialisationNotRun.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ predicate global(GlobalVariable v) {
2020
}
2121

2222
predicate mainCalled(Function f) {
23-
f.getQualifiedName() = "main"
23+
f.hasGlobalName("main")
2424
or
2525
exists(Function caller | mainCalled(caller) and allCalls(caller, f))
2626
}

cpp/ql/src/Critical/MemoryMayNotBeFreed.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ predicate allocCallOrIndirect(Expr e) {
5555
* can cause memory leaks.
5656
*/
5757
predicate verifiedRealloc(FunctionCall reallocCall, Variable v, ControlFlowNode verified) {
58-
reallocCall.getTarget().hasQualifiedName("realloc") and
58+
reallocCall.getTarget().hasGlobalName("realloc") and
5959
reallocCall.getArgument(0) = v.getAnAccess() and
6060
(
6161
exists(Variable newV, ControlFlowNode node |
@@ -82,7 +82,7 @@ predicate verifiedRealloc(FunctionCall reallocCall, Variable v, ControlFlowNode
8282
predicate freeCallOrIndirect(ControlFlowNode n, Variable v) {
8383
// direct free call
8484
freeCall(n, v.getAnAccess()) and
85-
not n.(FunctionCall).getTarget().hasQualifiedName("realloc")
85+
not n.(FunctionCall).getTarget().hasGlobalName("realloc")
8686
or
8787
// verified realloc call
8888
verifiedRealloc(_, v, n)

cpp/ql/src/Critical/OverflowCalculated.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import cpp
1414

1515
class MallocCall extends FunctionCall {
1616
MallocCall() {
17-
this.getTarget().hasQualifiedName("malloc") or
18-
this.getTarget().hasQualifiedName("std::malloc")
17+
this.getTarget().hasGlobalName("malloc") or
18+
this.getTarget().hasQualifiedName("std", "malloc")
1919
}
2020

2121
Expr getAllocatedSize() {
@@ -36,12 +36,12 @@ predicate spaceProblem(FunctionCall append, string msg) {
3636
malloc.getAllocatedSize() = add and
3737
buffer.getAnAccess() = strlen.getStringExpr() and
3838
(
39-
insert.getTarget().hasQualifiedName("strcpy") or
40-
insert.getTarget().hasQualifiedName("strncpy")
39+
insert.getTarget().hasGlobalName("strcpy") or
40+
insert.getTarget().hasGlobalName("strncpy")
4141
) and
4242
(
43-
append.getTarget().hasQualifiedName("strcat") or
44-
append.getTarget().hasQualifiedName("strncat")
43+
append.getTarget().hasGlobalName("strcat") or
44+
append.getTarget().hasGlobalName("strncat")
4545
) and
4646
malloc.getASuccessor+() = insert and
4747
insert.getArgument(1) = buffer.getAnAccess() and

cpp/ql/src/Critical/OverflowDestination.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import semmle.code.cpp.security.TaintTracking
2525
predicate sourceSized(FunctionCall fc, Expr src) {
2626
exists(string name |
2727
(name = "strncpy" or name = "strncat" or name = "memcpy" or name = "memmove") and
28-
fc.getTarget().hasQualifiedName(name)
28+
fc.getTarget().hasGlobalName(name)
2929
) and
3030
exists(Expr dest, Expr size, Variable v |
3131
fc.getArgument(0) = dest and

cpp/ql/src/Critical/OverflowStatic.ql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ predicate overflowOffsetInLoop(BufferAccess bufaccess, string msg) {
5959
}
6060

6161
predicate bufferAndSizeFunction(Function f, int buf, int size) {
62-
f.hasQualifiedName("read") and buf = 1 and size = 2
62+
f.hasGlobalName("read") and buf = 1 and size = 2
6363
or
64-
f.hasQualifiedName("fgets") and buf = 0 and size = 1
64+
f.hasGlobalName("fgets") and buf = 0 and size = 1
6565
or
66-
f.hasQualifiedName("strncpy") and buf = 0 and size = 2
66+
f.hasGlobalName("strncpy") and buf = 0 and size = 2
6767
or
68-
f.hasQualifiedName("strncat") and buf = 0 and size = 2
68+
f.hasGlobalName("strncat") and buf = 0 and size = 2
6969
or
70-
f.hasQualifiedName("memcpy") and buf = 0 and size = 2
70+
f.hasGlobalName("memcpy") and buf = 0 and size = 2
7171
or
72-
f.hasQualifiedName("memmove") and buf = 0 and size = 2
72+
f.hasGlobalName("memmove") and buf = 0 and size = 2
7373
or
74-
f.hasQualifiedName("snprintf") and buf = 0 and size = 1
74+
f.hasGlobalName("snprintf") and buf = 0 and size = 1
7575
or
76-
f.hasQualifiedName("vsnprintf") and buf = 0 and size = 1
76+
f.hasGlobalName("vsnprintf") and buf = 0 and size = 1
7777
}
7878

7979
class CallWithBufferSize extends FunctionCall {

cpp/ql/src/Critical/SizeCheck.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import cpp
1717
class Allocation extends FunctionCall {
1818
Allocation() {
1919
exists(string name |
20-
this.getTarget().hasQualifiedName(name) and
20+
this.getTarget().hasGlobalName(name) and
2121
(name = "malloc" or name = "calloc" or name = "realloc")
2222
)
2323
}
2424

25-
string getName() { result = this.getTarget().getQualifiedName() }
25+
private string getName() { this.getTarget().hasGlobalName(result) }
2626

2727
int getSize() {
2828
this.getName() = "malloc" and

0 commit comments

Comments
 (0)