sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Crash when accessing pairs on usertype from C++

Open sagamusix opened this issue 2 years ago • 0 comments

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");).

sagamusix avatar Feb 08 '23 21:02 sagamusix