wren icon indicating copy to clipboard operation
wren copied to clipboard

Segmentation fault when trying to create foreign class

Open iacore opened this issue 3 years ago • 4 comments

Currently, when the bind foreign class callback returns { NULL, NULL } like the following:

WrenForeignClassMethods APITest_bindForeignClass(
    WrenVM* vm, const char* module, const char* className)
{
  WrenForeignClassMethods methods = { NULL, NULL };
  return methods;
}

Any attempt to create a class in Wren will result in segmentation fault.

In https://wren.io/embedding/storing-c-data.html, the code example given does just that.

I think Wren should throw an error about foreign class not defined when allocateFn is NULL.

I also suspect that Wren might crash when a foreign class's finalizeFn is NULL, but I haven't tried that.

iacore avatar Sep 04 '21 11:09 iacore

To reproduce it, simply replace the above function in test/api/api_tests.c and run:

> bin/wren_test test/api/foreign_class.wren
fish: Job 1, 'bin/wren_test test/api/foreign_…' terminated by signal SIGSEGV (Address boundary error)

iacore avatar Sep 04 '21 11:09 iacore

Should trigger an error/assert in debug mode.

mhermier avatar Sep 04 '21 14:09 mhermier

Should trigger an error/assert in debug mode.

I simply ran make to build the static library.

Anyway, I think { NULL, _ } should mean no foreign class is found. The current behavior is inconsistent with bindForeignMethodFn, where returning NULL means no method is found and raise an error.

iacore avatar Sep 04 '21 14:09 iacore

Possible duplicate of #811

Should trigger an error/assert in debug mode.

foreign class A {
    construct new() {}
}

var a = A.new()

image

CrazyInfin8 avatar Sep 04 '21 19:09 CrazyInfin8