Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,23 @@ open class KotlinFileExtractor(
extractExprContext(it, locId, callable, enclosingStmt)
}

private fun escapeCharForQuotedLiteral(c: Char) =
when (c) {
'\r' -> "\\r"
'\n' -> "\\n"
'\t' -> "\\t"
'\\' -> "\\\\"
'"' -> "\\\""
else -> c.toString()
}

// Render a string literal as it might occur in Kotlin source. Note this is a reasonable guess; the real source
// could use other escape sequences to describe the same String. Importantly, this is the same guess the Java
// extractor makes regarding string literals occurring within annotations, which we need to coincide with to ensure
// database consistency.
private fun toQuotedLiteral(s: String) =
s.toCharArray().joinToString(separator = "", prefix = "\"", postfix = "\"") { c -> escapeCharForQuotedLiteral(c) }

private fun extractExpression(e: IrExpression, callable: Label<out DbCallable>, parent: StmtExprParent) {
with("expression", e) {
when(e) {
Expand Down Expand Up @@ -3602,7 +3619,7 @@ open class KotlinFileExtractor(
tw.writeExprs_stringliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
extractExprContext(id, locId, callable, exprParent.enclosingStmt)
tw.writeNamestrings(v.toString(), v.toString(), id)
tw.writeNamestrings(toQuotedLiteral(v.toString()), v.toString(), id)
}
v == null -> {
extractNull(e.type, tw.getLocation(e), exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ app/src/main/kotlin/testProject/App.kt:
# 0| 5: [BlockStmt] { ... }
# 0| 0: [ReturnStmt] return ...
# 0| 0: [StringTemplateExpr] "..."
# 0| 0: [StringLiteral] Project(
# 0| 1: [StringLiteral] name=
# 0| 0: [StringLiteral] "Project("
# 0| 1: [StringLiteral] "name="
# 0| 2: [VarAccess] this.name
# 0| -1: [ThisAccess] this
# 0| 3: [StringLiteral] ,
# 0| 4: [StringLiteral] language=
# 0| 3: [StringLiteral] ", "
# 0| 4: [StringLiteral] "language="
# 0| 5: [VarAccess] this.language
# 0| -1: [ThisAccess] this
# 0| 6: [StringLiteral] )
# 0| 6: [StringLiteral] ")"
# 0| 9: [Method] write$Self
# 0| 3: [TypeAccess] Unit
#-----| 4: (Parameters)
Expand Down Expand Up @@ -405,19 +405,19 @@ app/src/main/kotlin/testProject/App.kt:
# 7| 1: [LocalVariableDeclExpr] tmp0_serialDesc
# 7| 0: [ClassInstanceExpr] new PluginGeneratedSerialDescriptor(...)
# 7| -3: [TypeAccess] PluginGeneratedSerialDescriptor
# 7| 0: [StringLiteral] testProject.Project
# 7| 0: [StringLiteral] "testProject.Project"
# 7| 1: [ThisAccess] $serializer.this
# 7| 0: [TypeAccess] $serializer
# 7| 2: [IntegerLiteral] 2
# 7| 1: [ExprStmt] <Expr>;
# 7| 0: [MethodAccess] addElement(...)
# 7| -1: [VarAccess] tmp0_serialDesc
# 7| 0: [StringLiteral] name
# 7| 0: [StringLiteral] "name"
# 7| 1: [BooleanLiteral] false
# 7| 2: [ExprStmt] <Expr>;
# 7| 0: [MethodAccess] addElement(...)
# 7| -1: [VarAccess] tmp0_serialDesc
# 7| 0: [StringLiteral] language
# 7| 0: [StringLiteral] "language"
# 7| 1: [BooleanLiteral] false
# 7| 3: [ExprStmt] <Expr>;
# 7| 0: [AssignExpr] ...=...
Expand Down Expand Up @@ -520,7 +520,7 @@ app/src/main/kotlin/testProject/App.kt:
# 14| 0: [VarAccess] X.this.id
# 14| -1: [ThisAccess] X.this
# 14| 0: [TypeAccess] X
# 16| 1: [StringLiteral] X
# 16| 1: [StringLiteral] "X"
# 14| 1: [WhenBranch] ... -> ...
# 14| 0: [BooleanLiteral] true
# 14| 1: [ExprStmt] <Expr>;
Expand Down Expand Up @@ -556,7 +556,7 @@ app/src/main/kotlin/testProject/App.kt:
# 14| 0: [ValueNEExpr] ... (value not-equals) ...
# 14| 0: [MethodAccess] getId(...)
# 14| -1: [VarAccess] self
# 16| 1: [StringLiteral] X
# 16| 1: [StringLiteral] "X"
# 14| 1: [ExprStmt] <Expr>;
# 14| 0: [MethodAccess] encodeStringElement(...)
# 14| -1: [VarAccess] output
Expand Down Expand Up @@ -720,14 +720,14 @@ app/src/main/kotlin/testProject/App.kt:
# 14| 1: [LocalVariableDeclExpr] tmp0_serialDesc
# 14| 0: [ClassInstanceExpr] new PluginGeneratedSerialDescriptor(...)
# 14| -3: [TypeAccess] PluginGeneratedSerialDescriptor
# 14| 0: [StringLiteral] testProject.X
# 14| 0: [StringLiteral] "testProject.X"
# 14| 1: [ThisAccess] $serializer.this
# 14| 0: [TypeAccess] $serializer
# 14| 2: [IntegerLiteral] 1
# 14| 1: [ExprStmt] <Expr>;
# 14| 0: [MethodAccess] addElement(...)
# 14| -1: [VarAccess] tmp0_serialDesc
# 14| 0: [StringLiteral] id
# 14| 0: [StringLiteral] "id"
# 14| 1: [BooleanLiteral] true
# 14| 2: [ExprStmt] <Expr>;
# 14| 0: [AssignExpr] ...=...
Expand Down Expand Up @@ -764,7 +764,7 @@ app/src/main/kotlin/testProject/App.kt:
# 16| 0: [VarAccess] id
# 16| 6: [FieldDeclaration] String id;
# 16| -1: [TypeAccess] String
# 16| 0: [StringLiteral] X
# 16| 0: [StringLiteral] "X"
# 16| 7: [Method] getId
# 16| 3: [TypeAccess] String
# 16| 5: [BlockStmt] { ... }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
edges
| hasFields.kt:5:5:5:34 | constField : String | ReadsFields.java:5:10:5:29 | HasFields.constField |
| hasFields.kt:5:28:5:34 | taint : String | hasFields.kt:5:5:5:34 | constField : String |
| hasFields.kt:5:28:5:34 | "taint" : String | hasFields.kt:5:5:5:34 | constField : String |
| hasFields.kt:7:5:7:38 | lateinitField : String | ReadsFields.java:6:10:6:32 | HasFields.lateinitField |
| hasFields.kt:7:14:7:38 | <set-?> : String | hasFields.kt:7:5:7:38 | lateinitField : String |
| hasFields.kt:7:14:7:38 | <set-?> : String | hasFields.kt:7:14:7:38 | <set-?> : String |
| hasFields.kt:9:5:9:50 | jvmFieldAnnotatedField : String | ReadsFields.java:7:10:7:41 | HasFields.jvmFieldAnnotatedField |
| hasFields.kt:9:44:9:50 | taint : String | hasFields.kt:9:5:9:50 | jvmFieldAnnotatedField : String |
| hasFields.kt:14:22:14:26 | taint : String | hasFields.kt:7:14:7:38 | <set-?> : String |
| hasFields.kt:9:44:9:50 | "taint" : String | hasFields.kt:9:5:9:50 | jvmFieldAnnotatedField : String |
| hasFields.kt:14:22:14:26 | "taint" : String | hasFields.kt:7:14:7:38 | <set-?> : String |
nodes
| ReadsFields.java:5:10:5:29 | HasFields.constField | semmle.label | HasFields.constField |
| ReadsFields.java:6:10:6:32 | HasFields.lateinitField | semmle.label | HasFields.lateinitField |
| ReadsFields.java:7:10:7:41 | HasFields.jvmFieldAnnotatedField | semmle.label | HasFields.jvmFieldAnnotatedField |
| hasFields.kt:5:5:5:34 | constField : String | semmle.label | constField : String |
| hasFields.kt:5:28:5:34 | taint : String | semmle.label | taint : String |
| hasFields.kt:5:28:5:34 | "taint" : String | semmle.label | "taint" : String |
| hasFields.kt:7:5:7:38 | lateinitField : String | semmle.label | lateinitField : String |
| hasFields.kt:7:14:7:38 | <set-?> : String | semmle.label | <set-?> : String |
| hasFields.kt:7:14:7:38 | <set-?> : String | semmle.label | <set-?> : String |
| hasFields.kt:9:5:9:50 | jvmFieldAnnotatedField : String | semmle.label | jvmFieldAnnotatedField : String |
| hasFields.kt:9:44:9:50 | taint : String | semmle.label | taint : String |
| hasFields.kt:14:22:14:26 | taint : String | semmle.label | taint : String |
| hasFields.kt:9:44:9:50 | "taint" : String | semmle.label | "taint" : String |
| hasFields.kt:14:22:14:26 | "taint" : String | semmle.label | "taint" : String |
subpaths
#select
| hasFields.kt:5:28:5:34 | taint : String | hasFields.kt:5:28:5:34 | taint : String | ReadsFields.java:5:10:5:29 | HasFields.constField | flow path |
| hasFields.kt:9:44:9:50 | taint : String | hasFields.kt:9:44:9:50 | taint : String | ReadsFields.java:7:10:7:41 | HasFields.jvmFieldAnnotatedField | flow path |
| hasFields.kt:14:22:14:26 | taint : String | hasFields.kt:14:22:14:26 | taint : String | ReadsFields.java:6:10:6:32 | HasFields.lateinitField | flow path |
| hasFields.kt:5:28:5:34 | "taint" : String | hasFields.kt:5:28:5:34 | "taint" : String | ReadsFields.java:5:10:5:29 | HasFields.constField | flow path |
| hasFields.kt:9:44:9:50 | "taint" : String | hasFields.kt:9:44:9:50 | "taint" : String | ReadsFields.java:7:10:7:41 | HasFields.jvmFieldAnnotatedField | flow path |
| hasFields.kt:14:22:14:26 | "taint" : String | hasFields.kt:14:22:14:26 | "taint" : String | ReadsFields.java:6:10:6:32 | HasFields.lateinitField | flow path |
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ callArgs
| KotlinUser.kt:10:34:10:65 | new InnerGeneric<String>(...) | KotlinUser.kt:10:14:10:32 | new OuterGeneric<Integer>(...) | -2 |
| KotlinUser.kt:10:34:10:65 | new InnerGeneric<String>(...) | KotlinUser.kt:10:34:10:65 | InnerGeneric<String> | -3 |
| KotlinUser.kt:10:34:10:65 | new InnerGeneric<String>(...) | KotlinUser.kt:10:47:10:49 | a | 0 |
| KotlinUser.kt:10:34:10:65 | new InnerGeneric<String>(...) | KotlinUser.kt:10:53:10:63 | hello world | 1 |
| KotlinUser.kt:10:34:10:65 | new InnerGeneric<String>(...) | KotlinUser.kt:10:53:10:63 | "hello world" | 1 |
| KotlinUser.kt:11:13:11:31 | new OuterGeneric<Integer>(...) | KotlinUser.kt:11:13:11:31 | OuterGeneric<Integer> | -3 |
| KotlinUser.kt:11:33:11:49 | new InnerNotGeneric<>(...) | KotlinUser.kt:11:13:11:31 | new OuterGeneric<Integer>(...) | -2 |
| KotlinUser.kt:11:33:11:49 | new InnerNotGeneric<>(...) | KotlinUser.kt:11:33:11:49 | InnerNotGeneric<> | -3 |
Expand All @@ -88,10 +88,10 @@ callArgs
| KotlinUser.kt:13:31:13:52 | new InnerGeneric<String>(...) | KotlinUser.kt:13:31:13:52 | InnerGeneric<String> | -3 |
| KotlinUser.kt:14:26:14:63 | new InnerStaticGeneric<String>(...) | KotlinUser.kt:14:26:14:63 | InnerStaticGeneric<String> | -3 |
| KotlinUser.kt:14:26:14:63 | new InnerStaticGeneric<String>(...) | KotlinUser.kt:14:45:14:47 | a | 0 |
| KotlinUser.kt:14:26:14:63 | new InnerStaticGeneric<String>(...) | KotlinUser.kt:14:51:14:61 | hello world | 1 |
| KotlinUser.kt:14:26:14:63 | new InnerStaticGeneric<String>(...) | KotlinUser.kt:14:51:14:61 | "hello world" | 1 |
| KotlinUser.kt:15:13:15:39 | new OuterManyParams<Integer,String>(...) | KotlinUser.kt:15:13:15:39 | OuterManyParams<Integer,String> | -3 |
| KotlinUser.kt:15:13:15:39 | new OuterManyParams<Integer,String>(...) | KotlinUser.kt:15:29:15:29 | 1 | 0 |
| KotlinUser.kt:15:13:15:39 | new OuterManyParams<Integer,String>(...) | KotlinUser.kt:15:33:15:37 | hello | 1 |
| KotlinUser.kt:15:13:15:39 | new OuterManyParams<Integer,String>(...) | KotlinUser.kt:15:33:15:37 | "hello" | 1 |
| KotlinUser.kt:15:41:15:67 | new MiddleManyParams<Float,Double>(...) | KotlinUser.kt:15:13:15:39 | new OuterManyParams<Integer,String>(...) | -2 |
| KotlinUser.kt:15:41:15:67 | new MiddleManyParams<Float,Double>(...) | KotlinUser.kt:15:41:15:67 | MiddleManyParams<Float,Double> | -3 |
| KotlinUser.kt:15:41:15:67 | new MiddleManyParams<Float,Double>(...) | KotlinUser.kt:15:58:15:61 | 1.0 | 0 |
Expand All @@ -103,23 +103,23 @@ callArgs
| KotlinUser.kt:15:89:15:99 | shortValue(...) | KotlinUser.kt:15:89:15:89 | 1 | -1 |
| KotlinUser.kt:17:19:17:44 | returnsecond(...) | KotlinUser.kt:17:19:17:19 | a | -1 |
| KotlinUser.kt:17:19:17:44 | returnsecond(...) | KotlinUser.kt:17:34:17:34 | 0 | 0 |
| KotlinUser.kt:17:19:17:44 | returnsecond(...) | KotlinUser.kt:17:38:17:42 | hello | 1 |
| KotlinUser.kt:17:19:17:44 | returnsecond(...) | KotlinUser.kt:17:38:17:42 | "hello" | 1 |
| KotlinUser.kt:18:20:18:50 | returnsecond(...) | KotlinUser.kt:18:20:18:20 | a | -1 |
| KotlinUser.kt:18:20:18:50 | returnsecond(...) | KotlinUser.kt:18:20:18:50 | Character | -2 |
| KotlinUser.kt:18:20:18:50 | returnsecond(...) | KotlinUser.kt:18:35:18:35 | 0 | 0 |
| KotlinUser.kt:18:20:18:50 | returnsecond(...) | KotlinUser.kt:18:39:18:43 | hello | 1 |
| KotlinUser.kt:18:20:18:50 | returnsecond(...) | KotlinUser.kt:18:39:18:43 | "hello" | 1 |
| KotlinUser.kt:18:20:18:50 | returnsecond(...) | KotlinUser.kt:18:47:18:49 | a | 2 |
| KotlinUser.kt:19:19:19:31 | identity(...) | KotlinUser.kt:19:19:19:19 | b | -1 |
| KotlinUser.kt:19:19:19:31 | identity(...) | KotlinUser.kt:19:30:19:30 | 5 | 0 |
| KotlinUser.kt:20:20:20:39 | identity(...) | KotlinUser.kt:20:20:20:21 | b2 | -1 |
| KotlinUser.kt:20:20:20:39 | identity(...) | KotlinUser.kt:20:33:20:37 | hello | 0 |
| KotlinUser.kt:20:20:20:39 | identity(...) | KotlinUser.kt:20:33:20:37 | "hello" | 0 |
| KotlinUser.kt:21:19:21:37 | identity(...) | KotlinUser.kt:21:19:21:19 | c | -1 |
| KotlinUser.kt:21:19:21:37 | identity(...) | KotlinUser.kt:21:31:21:35 | world | 0 |
| KotlinUser.kt:21:19:21:37 | identity(...) | KotlinUser.kt:21:31:21:35 | "world" | 0 |
| KotlinUser.kt:22:19:22:39 | identity(...) | KotlinUser.kt:22:19:22:19 | d | -1 |
| KotlinUser.kt:22:19:22:39 | identity(...) | KotlinUser.kt:22:31:22:37 | goodbye | 0 |
| KotlinUser.kt:22:19:22:39 | identity(...) | KotlinUser.kt:22:31:22:37 | "goodbye" | 0 |
| KotlinUser.kt:23:19:23:71 | returnSixth(...) | KotlinUser.kt:23:19:23:19 | e | -1 |
| KotlinUser.kt:23:19:23:71 | returnSixth(...) | KotlinUser.kt:23:33:23:33 | 1 | 0 |
| KotlinUser.kt:23:19:23:71 | returnSixth(...) | KotlinUser.kt:23:37:23:41 | hello | 1 |
| KotlinUser.kt:23:19:23:71 | returnSixth(...) | KotlinUser.kt:23:37:23:41 | "hello" | 1 |
| KotlinUser.kt:23:19:23:71 | returnSixth(...) | KotlinUser.kt:23:45:23:48 | 1.0 | 2 |
| KotlinUser.kt:23:19:23:71 | returnSixth(...) | KotlinUser.kt:23:51:23:53 | 1.0 | 3 |
| KotlinUser.kt:23:19:23:71 | returnSixth(...) | KotlinUser.kt:23:56:23:57 | 1 | 4 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ d.kt:
# 1| 1: [Class] D
# 0| 2: [FieldDeclaration] String bar;
# 0| -1: [TypeAccess] String
# 0| 0: [StringLiteral] Foobar
# 0| 0: [StringLiteral] "Foobar"
# 1| 3: [Constructor] D
# 1| 5: [BlockStmt] { ... }
# 1| 0: [SuperConstructorInvocationStmt] super(...)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| d.kt:0:0:0:0 | bar | d.kt:0:0:0:0 | Foobar |
| d.kt:0:0:0:0 | bar | d.kt:0:0:0:0 | "Foobar" |
6 changes: 3 additions & 3 deletions java/ql/test/kotlin/library-tests/classes/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ classes.kt:
# 39| 0: [ExprStmt] <Expr>;
# 39| 0: [MethodAccess] f(...)
# 39| -1: [TypeAccess] ClassesKt
# 39| 0: [StringLiteral] init1
# 39| 0: [StringLiteral] "init1"
# 42| 1: [ExprStmt] <Expr>;
# 42| 0: [KtInitializerAssignExpr] ...=...
# 42| 0: [VarAccess] x
# 45| 2: [ExprStmt] <Expr>;
# 45| 0: [MethodAccess] f(...)
# 45| -1: [TypeAccess] ClassesKt
# 45| 0: [StringLiteral] init2
# 45| 0: [StringLiteral] "init2"
# 36| 2: [ExprStmt] <Expr>;
# 36| 0: [MethodAccess] f(...)
# 36| -1: [TypeAccess] ClassesKt
Expand Down Expand Up @@ -1119,7 +1119,7 @@ local_anonymous.kt:
# 40| 1: [BlockStmt] { ... }
# 42| 0: [LocalVariableDeclStmt] var ...;
# 42| 1: [LocalVariableDeclExpr] answer
# 42| 0: [StringLiteral] 42
# 42| 0: [StringLiteral] "42"
# 40| 1: [ExprStmt] <Expr>;
# 40| 0: [ClassInstanceExpr] new (...)
# 40| -3: [TypeAccess] Interface2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ test.kt:
# 1| 0: [VarAccess] p0
# 0| 1: [ArrayCreationExpr] new String[]
# 0| -2: [ArrayInit] {...}
# 0| 0: [StringLiteral] hello
# 0| 1: [StringLiteral] world
# 0| 0: [StringLiteral] "hello"
# 0| 1: [StringLiteral] "world"
# 0| -1: [TypeAccess] String
# 0| 0: [IntegerLiteral] 2
# 1| 1: [IfStmt] if (...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@
| Test.kt:105:5:109:5 | <Expr>; | 5 | Test.kt:105:9:105:17 | ... (value not-equals) ... |
| Test.kt:105:20:107:5 | { ... } | 0 | Test.kt:105:20:107:5 | { ... } |
| Test.kt:105:20:107:5 | { ... } | 1 | Test.kt:106:9:106:29 | <Expr>; |
| Test.kt:105:20:107:5 | { ... } | 2 | Test.kt:106:18:106:27 | x not null |
| Test.kt:105:20:107:5 | { ... } | 2 | Test.kt:106:18:106:27 | "x not null" |
| Test.kt:105:20:107:5 | { ... } | 3 | Test.kt:106:9:106:29 | println(...) |
| Test.kt:107:16:109:5 | ... -> ... | 0 | Test.kt:107:16:109:5 | ... -> ... |
| Test.kt:107:16:109:5 | ... -> ... | 1 | Test.kt:107:16:107:16 | y |
| Test.kt:107:16:109:5 | ... -> ... | 2 | Test.kt:107:21:107:24 | null |
| Test.kt:107:16:109:5 | ... -> ... | 3 | Test.kt:107:16:107:24 | ... (value not-equals) ... |
| Test.kt:107:27:109:5 | { ... } | 0 | Test.kt:107:27:109:5 | { ... } |
| Test.kt:107:27:109:5 | { ... } | 1 | Test.kt:108:9:108:29 | <Expr>; |
| Test.kt:107:27:109:5 | { ... } | 2 | Test.kt:108:18:108:27 | y not null |
| Test.kt:107:27:109:5 | { ... } | 2 | Test.kt:108:18:108:27 | "y not null" |
| Test.kt:107:27:109:5 | { ... } | 3 | Test.kt:108:9:108:29 | println(...) |
| Test.kt:112:1:116:1 | fn | 0 | Test.kt:112:1:116:1 | fn |
| Test.kt:112:32:116:1 | { ... } | 0 | Test.kt:112:32:116:1 | { ... } |
Expand Down
Loading