<@UB3CEA8QG> - A bug has been discovered with the ...
# kotlin-native
n
@Alexey Belkov [JB] - A bug has been discovered with the experimental memory model. Below is the Kotlin Native program:
Copy code
fun main() {
    runProgram("/bin/sh")
}

fun runProgram(cmd: String, vararg args: String): Int = memScoped {
    val programArgs = if (args.isNotEmpty()) arrayOf(cmd, *args) else arrayOf(cmd)
    execvp(cmd, programArgs.toCStringArray(this))
}
After running the Kotlin Native program the following error appears:
/bin/sh: 0: Can't open p
If the old memory model is used instead then the correct behaviour is exhibited by the Kotlin Native program, where the shell prompt appears (eg $).
a
Can you please report all bugs to https://youtrack.jetbrains.com/issues/KT. Thank you!
n
The issue has been resolved. The
toCStringArray
function has behaviour (is undefined) that differs between Kotlin Native's old and new memory models. In order to solve this an array has to be manually allocated in a memory scope, each Kotlin String is mapped to the array as a CPointer, and a
null
is appended at the end. Below is an example of the solution:
Copy code
// ...
val programArgs = if (args.isNotEmpty()) {
        allocArrayOf(arrayOf(cmd, *args).map { it.cstr.getPointer(this) } + null)
    } else {
        allocArrayOf(arrayOf(cmd).map { it.cstr.getPointer(this) } + null)
    }
execvp(cmd, programArgs)
// ...