Skip to content

Commit

Permalink
fix: eventcallback from std::forward to args (opentibiabr#2962)
Browse files Browse the repository at this point in the history
Using std::forward inside a loop can cause rvalue arguments to be moved
and invalidated after the first iteration. This happens because
std::forward preserves the value category, allowing rvalues to be moved.
To ensure arguments remain valid across all iterations when invoking
callbacks, avoid using std::forward inside the loop and pass arguments
directly as args....

Resolves opentibiabr#2959
  • Loading branch information
dudantas authored Oct 11, 2024
1 parent e71a259 commit 35550d1
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/lua/callbacks/events_callbacks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class EventsCallbacks {
void executeCallback(EventCallback_t eventType, CallbackFunc callbackFunc, Args &&... args) {
for (const auto &[name, callback] : getCallbacksByType(eventType)) {
if (callback && callback->isLoadedCallback()) {
std::invoke(callbackFunc, *callback, std::forward<Args>(args)...);
// g_logger().trace("Executed callback: {}", name);
std::invoke(callbackFunc, *callback, args...);
}
}
}
Expand All @@ -107,7 +106,7 @@ class EventsCallbacks {
ReturnValue checkCallbackWithReturnValue(EventCallback_t eventType, CallbackFunc callbackFunc, Args &&... args) {
for (const auto &[name, callback] : getCallbacksByType(eventType)) {
if (callback && callback->isLoadedCallback()) {
ReturnValue callbackResult = std::invoke(callbackFunc, *callback, std::forward<Args>(args)...);
ReturnValue callbackResult = std::invoke(callbackFunc, *callback, args...);
if (callbackResult != RETURNVALUE_NOERROR) {
return callbackResult;
}
Expand All @@ -128,7 +127,7 @@ class EventsCallbacks {
bool allCallbacksSucceeded = true;
for (const auto &[name, callback] : getCallbacksByType(eventType)) {
if (callback && callback->isLoadedCallback()) {
bool callbackResult = std::invoke(callbackFunc, *callback, std::forward<Args>(args)...);
bool callbackResult = std::invoke(callbackFunc, *callback, args...);
allCallbacksSucceeded &= callbackResult;
}
}
Expand Down

0 comments on commit 35550d1

Please sign in to comment.