ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.12k stars 500 forks source link

Crash when accessing pairs on usertype from C++ #1453

Open sagamusix opened 1 year ago

sagamusix commented 1 year ago

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