sol2
sol2 copied to clipboard
sol happily bypasses const correctness
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"]).