The result of a call that can return null, often a Java method, is used without a check
What it finds
System.getProperty("app.home").trim(), reader.readLine().length: a Java method returns null when it finds nothing, and Kotlin, which sees its result as a platform type, lets the caller use it without a check, which throws NullPointerException.
Calls go to Java methods of the JDK and of the servlet API that return null, in the table models/null.txt, and to functions of the same file, whose results count as null only where they write null or return such a call's result. Also reported: passing null, or such a result, to a parameter that the callee dereferences on every path before any test. Safe calls, ?:, !!, is checks, when, requireNotNull, isNullOrEmpty and locals declared with a type that is not nullable (which Kotlin checks where they are assigned) are understood.
Functions of the file with a declared return type are left to the compiler, which makes callers test a T? result, and so are their parameters. System.getProperty with a property that the JVM always defines, such as os.name, is not null.
From the rule's source, null_dereference_across_calls.rs.
package app import java.io.BufferedReaderimport java.io.File fun home(): String { return System.getProperty("app.home").trim()tx0 reports: System.getProperty(...) can return null} fun homeSafe(): String { return System.getProperty("app.home")?.trim() ?: "/"} fun homeChecked(): String { val home = System.getProperty("app.home") if (home == null) { return "/" } return home.trim()} fun homeDeclared(): String { val home: String = System.getProperty("app.home") return home.trim()} fun homeAsserted(): Int { return System.getenv("HOME")!!.length} fun firstLine(reader: BufferedReader): Int { val line = reader.readLine() return line.lengthtx0 reports: readLine(...) can return null} fun firstLineWhen(reader: BufferedReader): Int { val line = reader.readLine() return when { line.isNullOrEmpty() -> 0 else -> line.length }} fun files(dir: File): Int { return dir.listFiles().sizetx0 reports: listFiles(...) can return null} fun env() = System.getenv("APP_ENV") fun envUpper(): String { return env().uppercase()tx0 reports: env(...) can return null} Turn it on, off or suppress it
The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:null-dereference-across-calls" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:null-dereference-across-calls] reason="..."tx0 explain tx0/kotlin:null-dereference-across-calls shows the same in the terminal. Found a false positive? Report it.