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

sol happily bypasses const correctness #1333

Open
lmoureaux opened this issue Mar 27, 2022 · 0 comments
Open

sol happily bypasses const correctness #1333

lmoureaux opened this issue Mar 27, 2022 · 0 comments

Comments

@lmoureaux
Copy link

Hello and thank you for the awesome piece of code that sol is!

I was evaluating what kind of safety net needs to be put around sol in the context of my application, and I found that the following example of const-related undefined behavior isn't caught by sol:

#include <sol/sol.hpp>

void f(int *x)
{
    *x = 5;
}

int main()
{
    sol::state lua;
    lua["f"] = f;

    const int x = 0;

    auto result = lua["f"](&x); // f(&x) is incorrect and casting the const away is undefined behavior
    return result.valid();
}

I tested this with GCC 11.2.0 and sol 4.0.0-alpha, compiling with:

g++ -I sol2/ -o test test.cpp -llua

With this compiler on my system I get result.valid() == true. In the debugger I can see that f is executed. Of course this is undefined behavior so YMMV.

Can sol prevent this? (I understand that Lua itself has no cv-qualifiers so this might not be so trivial.) I found no configuration option by the name CONST; -DSOL_ALL_SAFETIES_ON=1 did not help either. If nothing can be done in sol, I suppose I'll want to override sol_lua_push to prevent accidentally pushing any const pointer or reference, but even that wouldn't solve every possible case (I can get a std::function<void(const int*)> out of lua["f"]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant