Skip to content

Commit 0f18c02

Browse files
committed
Kotlin: Support apply
1 parent 3c3b530 commit 0f18c02

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added support for the Kotlin method `apply`.

java/ql/lib/ext/kotlin.model.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ extensions:
33
pack: codeql/java-all
44
extensible: summaryModel
55
data:
6+
- ["kotlin", "StandardKt", False, "apply", "", "", "Argument[0]", "Argument[1].Parameter[0]", "value", "manual"]
7+
- ["kotlin", "StandardKt", False, "apply", "", "", "Argument[0]", "ReturnValue", "value", "manual"]
68
- ["kotlin", "StandardKt", False, "with", "", "", "Argument[0]", "Argument[1].Parameter[0]", "value", "manual"]
79
- ["kotlin", "StandardKt", False, "with", "", "", "Argument[1].ReturnValue", "ReturnValue", "value", "manual"]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** Provides classes and predicates related to `kotlin`. */
2+
3+
import java
4+
5+
/** A call to Kotlin's `apply` method. */
6+
class KotlinApply extends MethodAccess {
7+
ExtensionMethod m;
8+
9+
KotlinApply() {
10+
this.getMethod() = m and
11+
m.hasQualifiedName("kotlin", "StandardKt", "apply")
12+
}
13+
14+
/** Gets the function block argument of this call. */
15+
LambdaExpr getLambdaArg() {
16+
result = this.getArgument(m.getExtensionReceiverParameterIndex() + 1)
17+
}
18+
19+
/** Gets the receiver argument of this call. */
20+
Argument getReceiver() { result = this.getArgument(m.getExtensionReceiverParameterIndex()) }
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| apply.kt:6:9:6:41 | apply(...) |
2+
| apply.kt:7:14:7:40 | apply(...) |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ApplyFlowTest {
2+
fun <T> taint(t: T) = t
3+
fun sink(s: String) { }
4+
5+
fun test(input: String) {
6+
taint(input).apply { sink(this) } // $ hasValueFlow
7+
sink(taint(input).apply { this }) // $ hasValueFlow
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
import semmle.code.java.frameworks.kotlin.Kotlin
3+
4+
from KotlinApply a
5+
select a

0 commit comments

Comments
 (0)