Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nullable strings cause segfault when called from c #2956

Closed
josephdwyer opened this issue May 9, 2019 · 1 comment

Comments

@josephdwyer
Copy link

commented May 9, 2019

Description

Kotlin functions that return nullable strings error when accessing via C interop.

Repro

Compile the example:

kotlinc-native knlibrary.kt -produce dynamic -o knlibrary.dll

Using instructions from here to consume the library.
Note: We have to rename the dll because consumer.exe looks for the name with _symbols for whatever reason.

lib.exe /def:knlibrary_symbols.def /out:knlibrary_symbols.lib
cl.exe consumer.c knlibrary_symbols.lib
MOVE knlibrary.dll knlibrary_symbols.dll
consumer.exe

Program output:

>consumer.exe
From C: Hello
From C: (null)
From KN: World

>echo Exit Code is %errorlevel%
Exit Code is -1073741819

Expected: null value does not crash program

Example Code

knlibrary.kt

package knlibrary

fun getStringFromKN(param: Int) : String? {
    if (param == 0) {
        return "World"
    }
    return null
}

consumer.c

#include <stdio.h>
#include "knlibrary_api.h"

const char* getStringFromC(int param)
{
   if (param == 0)
   {
      return "Hello";
   }
   return NULL;
}

int main()
{
   // non-null value works
   char* cResult = getStringFromC(0);
   printf("From C: %s\n", cResult);

   // null value works
   cResult = getStringFromC(1);
   printf("From C: %s\n", cResult);

   knlibrary_ExportedSymbols* lib = knlibrary_symbols();

   // non-null value works
   const char* knResult = lib->kotlin.root.knlibrary.getStringFromKN(0);
   printf("From KN: %s\n", knResult);

   // null-value errors
   knResult = lib->kotlin.root.knlibrary.getStringFromKN(1);
   printf("From KN: %s\n", knResult);

   printf("Should get here, but does not :(\n");

   return 0;
}
@olonho

This comment has been minimized.

Copy link
Contributor

commented May 13, 2019

#2967 shall fix it.

@olonho olonho closed this May 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.