Skip to content

Commit 8a805bb

Browse files
committed
Swift: Replace getABaseOrAliasedType with slightly more sophisticated getABaseType.
1 parent 302013a commit 8a805bb

File tree

7 files changed

+23
-27
lines changed

7 files changed

+23
-27
lines changed

swift/ql/lib/codeql/swift/elements/type/NominalType.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ private import codeql.swift.elements.decl.NominalTypeDecl
33
private import codeql.swift.elements.type.Type
44

55
class NominalType extends Generated::NominalType {
6-
Type getABaseType() { result = this.getDeclaration().(NominalTypeDecl).getABaseType() }
6+
override Type getABaseType() { result = this.getDeclaration().(NominalTypeDecl).getABaseType() }
77

88
NominalType getADerivedType() { result.getABaseType() = this }
99

swift/ql/lib/codeql/swift/elements/type/Type.qll

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@ class Type extends Generated::Type {
1515
Type getUnderlyingType() { result = this }
1616

1717
/**
18-
* Gets any base type of this type, or the result of resolving a typedef. For
19-
* example in the following code, `C` has base type `B` which has underlying
20-
* type `A`. Thus, `getABaseOrAliasedType*` can be used to discover the
21-
* relationship between `C` and `A`.
18+
* Gets any base type of this type. For a `typealias`, this is a base type
19+
* of the aliased type. For example in the following code, both `B` and
20+
* `B_alias` have base type `A`.
2221
* ```
2322
* class A {}
2423
*
25-
* typealias B = A
24+
* class B : A {}
2625
*
27-
* class C : B {}
26+
* typealias B_alias = B
2827
* ```
2928
*/
30-
Type getABaseOrAliasedType() {
31-
result = this.(NominalType).getABaseType() or
32-
result = this.(TypeAliasType).getAliasedType()
33-
}
29+
Type getABaseType() { none() }
3430
}

swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ class TypeAliasType extends Generated::TypeAliasType {
1313
Type getAliasedType() { result = this.getDecl().getAliasedType() }
1414

1515
override Type getUnderlyingType() { result = this.getAliasedType().getUnderlyingType() }
16+
17+
override Type getABaseType() { result = getAliasedType().getABaseType() }
1618
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private class CoreDataStore extends CleartextStorageDatabaseSink {
4949
// with `coreDataObj.data` is a sink.
5050
// (ideally this would be only members with the `@NSManaged` attribute)
5151
exists(NominalType t, Expr e |
52-
t.getABaseOrAliasedType*().getName() = "NSManagedObject" and
52+
t.getABaseType*().getUnderlyingType().getName() = "NSManagedObject" and
5353
this.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() = e and
5454
e.getFullyConverted().getType() = t and
5555
not e.(DeclRefExpr).getDecl() instanceof SelfParamDecl
@@ -67,7 +67,7 @@ private class RealmStore extends CleartextStorageDatabaseSink instanceof DataFlo
6767
// example in `realmObj.data = sensitive` the post-update node corresponding
6868
// with `realmObj.data` is a sink.
6969
exists(NominalType t, Expr e |
70-
t.getABaseOrAliasedType*().getName() = "RealmSwiftObject" and
70+
t.getABaseType*().getUnderlyingType().getName() = "RealmSwiftObject" and
7171
this.getPreUpdateNode().asExpr() = e and
7272
e.getFullyConverted().getType() = t and
7373
not e.(DeclRefExpr).getDecl() instanceof SelfParamDecl

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CleartextStorageConfig extends TaintTracking::Configuration {
3838
// for example in `realmObj.data = sensitive`.
3939
isSink(node) and
4040
exists(NominalTypeDecl d, Decl cx |
41-
d.getType().getABaseOrAliasedType*().getName() = ["NSManagedObject", "RealmSwiftObject"] and
41+
d.getType().getABaseType*().getUnderlyingType().getName() = ["NSManagedObject", "RealmSwiftObject"] and
4242
cx.asNominalTypeDecl() = d and
4343
c.getAReadContent().(DataFlow::Content::FieldContent).getField() = cx.getAMember()
4444
)
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
| nominaltype.swift:35:6:35:6 | a | A | A | |
2-
| nominaltype.swift:36:6:36:6 | a_alias | A_alias | A | getABaseOrAliasedType:A, getAliasedType:A |
3-
| nominaltype.swift:37:6:37:6 | a_optional_alias | A_optional_alias | Optional<A> | getABaseOrAliasedType:Optional<A>, getAliasedType:Optional<A> |
4-
| nominaltype.swift:38:6:38:6 | b1 | B1 | B1 | getABaseOrAliasedType:A, getABaseType:A |
5-
| nominaltype.swift:39:6:39:6 | b2 | B2 | B2 | getABaseOrAliasedType:A_alias, getABaseType:A_alias |
6-
| nominaltype.swift:40:6:40:6 | b1_alias | B1_alias | B1 | getABaseOrAliasedType:B1, getAliasedType:B1 |
7-
| nominaltype.swift:41:6:41:6 | b2_alias | B2_alias | B2 | getABaseOrAliasedType:B2, getAliasedType:B2 |
2+
| nominaltype.swift:36:6:36:6 | a_alias | A_alias | A | getAliasedType:A |
3+
| nominaltype.swift:37:6:37:6 | a_optional_alias | A_optional_alias | Optional<A> | getAliasedType:Optional<A> |
4+
| nominaltype.swift:38:6:38:6 | b1 | B1 | B1 | getABaseType:A |
5+
| nominaltype.swift:39:6:39:6 | b2 | B2 | B2 | getABaseType:A_alias |
6+
| nominaltype.swift:40:6:40:6 | b1_alias | B1_alias | B1 | getABaseType:A, getAliasedType:B1 |
7+
| nominaltype.swift:41:6:41:6 | b2_alias | B2_alias | B2 | getABaseType:A_alias, getAliasedType:B2 |
88
| nominaltype.swift:42:6:42:6 | p | P | P | |
99
| nominaltype.swift:43:6:43:6 | p_alias | P_alias | P_alias | |
10-
| nominaltype.swift:44:6:44:6 | c1 | C1 | C1 | getABaseOrAliasedType:P, getABaseType:P |
11-
| nominaltype.swift:45:6:45:6 | c2 | C2 | C2 | getABaseOrAliasedType:P_alias, getABaseType:P_alias |
12-
| nominaltype.swift:46:6:46:6 | c1_alias | C1_alias | C1 | getABaseOrAliasedType:C1, getAliasedType:C1 |
13-
| nominaltype.swift:47:6:47:6 | c2_alias | C2_alias | C2 | getABaseOrAliasedType:C2, getAliasedType:C2 |
10+
| nominaltype.swift:44:6:44:6 | c1 | C1 | C1 | getABaseType:P |
11+
| nominaltype.swift:45:6:45:6 | c2 | C2 | C2 | getABaseType:P_alias |
12+
| nominaltype.swift:46:6:46:6 | c1_alias | C1_alias | C1 | getABaseType:P, getAliasedType:C1 |
13+
| nominaltype.swift:47:6:47:6 | c2_alias | C2_alias | C2 | getABaseType:P_alias, getAliasedType:C2 |

swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import swift
33
string describe(Type t) {
44
result = "getAliasedType:" + t.(TypeAliasType).getAliasedType()
55
or
6-
result = "getABaseType:" + t.(NominalType).getABaseType()
7-
or
8-
result = "getABaseOrAliasedType:" + t.getABaseOrAliasedType()
6+
result = "getABaseType:" + t.getABaseType()
97
}
108

119
from VarDecl v, Type t

0 commit comments

Comments
 (0)