sol2
sol2 copied to clipboard
Crash when accessing pairs on usertype from C++
The following code crashes somewhere deep in sol while trying to increment a pairs_iterator.
#include <sol/sol.hpp>
int main()
{
struct Foo
{
int getBar() { return 1; }
void setBar(int) {};
};
sol::state lua;
sol::table g = lua.globals();
g.new_usertype<Foo>("Foo", "bar", sol::property(&Foo::getBar, &Foo::setBar));
g["baz"] = sol::as_function([]{return Foo{};});
lua.do_string("b = baz()");
sol::table obj = g["b"];
for (const auto& pair : obj.pairs())
{
}
return 0;
}
Apparently the sol code violates a Lua API invariant in Lua's index2stack function (api_check(L, o < L->top, "invalid index");).