Skip to content

Commit cf9c3af

Browse files
committed
Swift: Add and use AbstractFunctionDecl.hasGlobalName predicate.
1 parent caf9ac5 commit cf9c3af

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

swift/ql/lib/codeql/swift/elements/decl/AbstractFunctionDecl.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private import codeql.swift.generated.decl.AbstractFunctionDecl
2+
private import codeql.swift.elements.decl.MethodDecl
23

34
class AbstractFunctionDecl extends Generated::AbstractFunctionDecl {
45
override string toString() { result = this.getName() }
@@ -7,4 +8,12 @@ class AbstractFunctionDecl extends Generated::AbstractFunctionDecl {
78
* Holds if this function is called `funcName`.
89
*/
910
predicate hasName(string funcName) { this.getName() = funcName }
11+
12+
/**
13+
* Holds if this is a global (non-member) function called `funcName`.
14+
*/
15+
predicate hasGlobalName(string funcName) {
16+
this.hasName(funcName) and
17+
not this instanceof MethodDecl
18+
}
1019
}

swift/ql/src/queries/Security/CWE-089/SqlInjection.ql

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ abstract class SqlSink extends DataFlow::Node { }
2727
class CApiSqlSink extends SqlSink {
2828
CApiSqlSink() {
2929
// `sqlite3_exec` and variants of `sqlite3_prepare`.
30-
exists(AbstractFunctionDecl f, CallExpr call |
31-
f.getName() =
32-
[
33-
"sqlite3_exec(_:_:_:_:_:)", "sqlite3_prepare(_:_:_:_:_:)",
34-
"sqlite3_prepare_v2(_:_:_:_:_:)", "sqlite3_prepare_v3(_:_:_:_:_:_:)",
35-
"sqlite3_prepare16(_:_:_:_:_:)", "sqlite3_prepare16_v2(_:_:_:_:_:)",
36-
"sqlite3_prepare16_v3(_:_:_:_:_:_:)"
37-
] and
38-
call.getStaticTarget() = f and
30+
exists(CallExpr call |
31+
call.getStaticTarget()
32+
.hasGlobalName([
33+
"sqlite3_exec(_:_:_:_:_:)", "sqlite3_prepare(_:_:_:_:_:)",
34+
"sqlite3_prepare_v2(_:_:_:_:_:)", "sqlite3_prepare_v3(_:_:_:_:_:_:)",
35+
"sqlite3_prepare16(_:_:_:_:_:)", "sqlite3_prepare16_v2(_:_:_:_:_:)",
36+
"sqlite3_prepare16_v3(_:_:_:_:_:_:)"
37+
]) and
3938
call.getArgument(1).getExpr() = this.asExpr()
4039
)
4140
}

0 commit comments

Comments
 (0)