How to right way to process C null pointer in Kotl...
# kotlin-native
r
How to right way to process C null pointer in Kotlin function?
o
Andrey, please use Slack threads for long messages with stacktraces
r
Sorry, I'm not familiar with slack. Hello! I compile simple library with one function
Copy code
const val HELLO_EN = "Hello"
fun hello(name: String, lang: String?) = "${lang ?: HELLO_EN} $name!!!\n"
And call it from C-code
Copy code
example_kt_symbols()->kotlin.root.hello(name, lang)
name
- is initialized
char *
, and
lang
is unitialized
char *
aka null pointer Execution failed on this step
Copy code
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff4a39f81 in __strlen_sse2_pminub () from /usr/lib64/libc.so.6
(gdb) bt
#0  0x00007ffff4a39f81 in __strlen_sse2_pminub () from /usr/lib64/libc.so.6
#1  0x00007fffe720fadb in CreateStringFromCString () from ./modules/example.so
#2  0x00007fffe71f77fb in _konan_function_7_impl(char const*, char const*) () from ./modules/example.so
#3  0x00007fffe71f7016 in zif_hello (execute_data=<optimized out>, return_value=0x7ffff3a12080) at /root/simpleExtension/example.c:57
#4  0x0000555555802cf6 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER ()
#5  0x00005555557f2cab in execute_ex ()
#6  0x0000555555846964 in zend_execute ()
#7  0x000055555579c168 in zend_eval_stringl ()
#8  0x000055555579c319 in zend_eval_stringl_ex ()
#9  0x00005555558488e3 in do_cli ()
#10 0x0000555555624bbf in main ()
What I'm doing wrong?
o
it’s likely K/N bug, in
CreateCStringFromString
, combined with different behavior of
strlen
with null arg. Pass “” for now, we will fix it.
smth like
Copy code
diff --git a/runtime/src/main/cpp/KString.cpp b/runtime/src/main/cpp/KString.cpp
index 890885b2f..00d08ff40 100644
--- a/runtime/src/main/cpp/KString.cpp
+++ b/runtime/src/main/cpp/KString.cpp
@@ -713,7 +713,7 @@ int iswlower_Konan(KChar ch) {
 extern "C" {
 
 OBJ_GETTER(CreateStringFromCString, const char* cstring) {
-  RETURN_RESULT_OF(utf8ToUtf16, cstring, strlen(cstring));
+  RETURN_RESULT_OF(utf8ToUtf16, cstring, cstring ? strlen(cstring) : 0);
 }
 
 OBJ_GETTER(CreateStringFromUtf8, const char* utf8, uint32_t lengthBytes) {
may help
r
Thank!
o
fixed in master