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

How to secure such a pointer? #1615

Open
tangjiands opened this issue Jul 9, 2024 · 1 comment
Open

How to secure such a pointer? #1615

tangjiands opened this issue Jul 9, 2024 · 1 comment

Comments

@tangjiands
Copy link

tangjiands commented Jul 9, 2024

class Base
{
public:
virtual void print()
{
std::cout << "base print:" << x << endl;
}
private:
int x;
};

class A: public Base
{
public:
void print() override
{
std::cout << "A print:" << y << endl;
}
private:
int y;
};

class C
{
public:
void print()
{
std::cout << "B print:" << z << endl;
}
private:
int z;
};
void funcBase(Base* base)
{
return base->print();
}

A a;
C c;
Base base;
sol::state lua;
luaL_openlibs(lua); 

lua.new_usertype<Base>(
    "Base");
lua.new_usertype<A>(
    "A", sol::base_classes, sol::bases<Base>());
lua["base"] = &base;
lua["a"] = &a;
lua["c"] = &c;
lua.safe_script("function startup() return funcBase(b) end");
// set funcBase
lua.set_function("funcBase", funcBase);
**//An error parameter &c was passed**
lua["funcBase"](&c);
@deadlocklogic
Copy link
Contributor

Please next time use a correctly formatted snippet in markdown, filtering all unused code so we can assist you better.

The call to lua["funcBase"](&c); returns a sol::protected_function_result.
You can then check if this result was valid yourself like:

auto res = lua["funcBase"](&c);
if (!res.valid()) {
    std::cout << "error occured!" << std::endl;
}

To make this call automatically print the error result you can call it from a protected function:

sol::protected_function protected_function(
    lua["funcBase"], [](std::string message) {
        std::cout << "error occurred : " << message << std::endl;
    });
protected_function(&c);

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

2 participants