cursey / safetyhook

C++23 procedure hooking library.

Home Page:https://cursey.dev/safetyhook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling original of functions with references in the parameters causes crashing

Nukoooo opened this issue · comments

example code:

safetyhook::InlineHook hk {};

void some_func(int& ref)
{
    ref++;
}

// If I change reference to pointer then it will not crash
void hk_some_func(int& ref)
{
    hk.call(ref);
    // or call this, this wont crash
    // hk.call<void, int&>(ref);
}
commented

I'm not sure there is a good solution here. The problem is the .call helper methods take in variadic template arguments by value. References naturally decompose to value which is why it crashes if you don't explicitly state that the parameter is a reference type.

Perfect forwarding has the opposite problem though, where if you pass in a local variable as an argument it will be treated as a reference.

I think the best I can do in this instance is provide documentation in the comments of each of the .call helper methods explaining the issues with passing references unless someone else has a better idea.

commented

I talked with @cursey about this, there's a few ways to work around this.

Call with reference:

void hk_some_func(int& ref)
{
    hk.call(&ref);
}

Make the reference argument(s) a pointer:

void hk_some_func(int* ref)
{
    hk.call(ref);
}

Unless someone else is a metaprogramming genius and can figure it out with an elegant and easy-to-read solution then I think we've both agreed on just providing documentation.

:) Thank you both! I think it is safe to close this issue now