Skip to content

Commit 5c6b8bd

Browse files
committed
Swift: Introduce EnumElmentDecl.hasQualifiedName and use it to clean up the code.
1 parent 5f8f1b6 commit 5c6b8bd

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
private import codeql.swift.generated.decl.EnumElementDecl
2+
private import codeql.swift.elements.decl.Decl
23

4+
/**
5+
* An enum element declaration, for example `enumElement` and `anotherEnumElement` in:
6+
* ```
7+
* enum MyEnum {
8+
* case enumElement
9+
* case anotherEnumElement(Int)
10+
* }
11+
* ```
12+
*/
313
class EnumElementDecl extends Generated::EnumElementDecl {
414
override string toString() { result = this.getName() }
15+
16+
/**
17+
* Holds if this function is called `funcName` and is a member of a
18+
* class, struct, extension, enum or protocol called `typeName`.
19+
*/
20+
cached
21+
predicate hasQualifiedName(string typeName, string enumElementName) {
22+
this.getName() = enumElementName and
23+
exists(Decl d |
24+
d.asNominalTypeDecl().getFullName() = typeName and
25+
d.getAMember() = this
26+
)
27+
}
28+
29+
/**
30+
* Holds if this function is called `funcName` and is a member of a
31+
* class, struct, extension, enum or protocol called `typeName` in a module
32+
* called `moduleName`.
33+
*/
34+
predicate hasQualifiedName(string moduleName, string typeName, string enumElementName) {
35+
this.hasQualifiedName(typeName, enumElementName) and
36+
this.getModule().getFullName() = moduleName
37+
}
538
}

swift/ql/lib/codeql/swift/elements/expr/EnumElementExpr.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ private import codeql.swift.elements.expr.MethodLookupExpr
1010
private import codeql.swift.elements.decl.EnumElementDecl
1111

1212
/**
13-
* An expression that constructs a case of an enum.
13+
* An expression that references a case of an enum. For example both `enumElement` in:
14+
* ```
15+
* let value = MyEnum.enumElement
16+
* ...
17+
* switch (anotherValue) {
18+
* case .enumElement:
19+
* ...
20+
* ```
1421
*/
1522
class EnumElementExpr extends Expr {
1623
EnumElementDecl decl;

swift/ql/lib/codeql/swift/security/PathInjectionExtensions.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ private class GlobalVariablePathInjectionSink extends PathInjectionSink {
5353
private class EnumConstructorPathInjectionSink extends PathInjectionSink {
5454
EnumConstructorPathInjectionSink() {
5555
// first argument to `Connection.Location.uri(_:parameters:)`
56-
exists(ApplyExpr ae, EnumElementDecl decl, NominalTypeDecl parent |
56+
exists(ApplyExpr ae, EnumElementDecl decl |
5757
ae.getFunction().(MethodLookupExpr).getMember() = decl and
58-
decl.getName() = "uri" and
59-
decl.getDeclaringDecl() = parent and
60-
parent.getName() = "Location" and
61-
parent.getDeclaringDecl().(NominalTypeDecl).(NominalTypeDecl).getName() = "Connection" and
58+
decl.hasQualifiedName("Connection.Location", "uri") and
6259
this.asExpr() = ae.getArgument(0).getExpr()
6360
)
6461
}

0 commit comments

Comments
 (0)