mirror of
https://github.com/github/codeql.git
synced 2025-12-18 01:33:15 +01:00
Implement Any?.String using java.lang.String.valueOf
This is how kotlinc does it, and doesn't involve an unchecked null deref like the existing use of Object.toString.
This commit is contained in:
committed by
Ian Lynagh
parent
616f20fa52
commit
7368b49b16
@@ -1102,6 +1102,25 @@ open class KotlinFileExtractor(
|
||||
return result
|
||||
}
|
||||
|
||||
val javaLangString by lazy {
|
||||
val result = pluginContext.referenceClass(FqName("java.lang.String"))?.owner
|
||||
result?.let { extractExternalClassLater(it) }
|
||||
result
|
||||
}
|
||||
|
||||
val stringValueOfObjectMethod by lazy {
|
||||
val result = javaLangString?.declarations?.find {
|
||||
it is IrFunction &&
|
||||
it.name.asString() == "valueOf" &&
|
||||
it.valueParameters.size == 1 &&
|
||||
it.valueParameters[0].type == pluginContext.irBuiltIns.anyNType
|
||||
} as IrFunction?
|
||||
if (result == null) {
|
||||
logger.error("Couldn't find declaration java.lang.String.valueOf(Object)")
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fun extractCall(c: IrCall, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>) {
|
||||
with("call", c) {
|
||||
fun isFunction(pkgName: String, className: String, fName: String, hasQuestionMark: Boolean? = false): Boolean {
|
||||
@@ -1394,13 +1413,9 @@ open class KotlinFileExtractor(
|
||||
logger.errorElement("Unhandled builtin", c)
|
||||
}
|
||||
isFunction("kotlin", "Any", "toString", true) -> {
|
||||
// TODO: this is not correct. `a.toString()` becomes `(a?:"\"null\"").toString()`
|
||||
val func = pluginContext.irBuiltIns.anyType.classOrNull?.owner?.declarations?.find { it is IrFunction && it.name.asString() == "toString" }
|
||||
if (func == null) {
|
||||
logger.errorElement("Couldn't find toString function", c)
|
||||
return
|
||||
stringValueOfObjectMethod?.let {
|
||||
extractRawMethodAccess(it, c, callable, parent, idx, enclosingStmt, listOf(c.extensionReceiver), null, null)
|
||||
}
|
||||
extractMethodAccess(func as IrFunction)
|
||||
}
|
||||
isBuiltinCallKotlin(c, "enumValues") -> {
|
||||
extractSpecialEnumFunction("values")
|
||||
|
||||
@@ -976,7 +976,8 @@ exprs.kt:
|
||||
# 200| 5: [BlockStmt] { ... }
|
||||
# 202| 0: [LocalVariableDeclStmt] var ...;
|
||||
# 202| 1: [LocalVariableDeclExpr] a
|
||||
# 202| 0: [MethodAccess] toString(...)
|
||||
# 202| 0: [MethodAccess] valueOf(...)
|
||||
# 202| -1: [TypeAccess] String
|
||||
# 202| 0: [VarAccess] aa
|
||||
# 203| 1: [LocalVariableDeclStmt] var ...;
|
||||
# 203| 1: [LocalVariableDeclExpr] b0
|
||||
|
||||
@@ -579,7 +579,8 @@
|
||||
| exprs.kt:199:1:212:1 | <obinit>(...) | exprs.kt:199:1:212:1 | Class2 | MethodAccess |
|
||||
| exprs.kt:202:9:202:29 | a | exprs.kt:200:5:211:5 | x | LocalVariableDeclExpr |
|
||||
| exprs.kt:202:17:202:18 | aa | exprs.kt:200:5:211:5 | x | VarAccess |
|
||||
| exprs.kt:202:20:202:29 | toString(...) | exprs.kt:200:5:211:5 | x | MethodAccess |
|
||||
| exprs.kt:202:20:202:29 | String | exprs.kt:200:5:211:5 | x | TypeAccess |
|
||||
| exprs.kt:202:20:202:29 | valueOf(...) | exprs.kt:200:5:211:5 | x | MethodAccess |
|
||||
| exprs.kt:203:9:203:27 | b0 | exprs.kt:200:5:211:5 | x | LocalVariableDeclExpr |
|
||||
| exprs.kt:203:19:203:19 | s | exprs.kt:200:5:211:5 | x | VarAccess |
|
||||
| exprs.kt:203:21:203:27 | Intrinsics | exprs.kt:200:5:211:5 | x | TypeAccess |
|
||||
|
||||
Reference in New Issue
Block a user