Skip to content

Commit

Permalink
fix: add null check for moveFunction to prevent crashes (#3213)
Browse files Browse the repository at this point in the history
This adds a null check for the moveFunction in the MoveEvent::fireAddRemItem method. Previously, the code accessed moveFunction directly without verifying its validity, which could lead to crashes if it was nullptr. The fix ensures stability by:

Logging an error message when moveFunction is null.
Returning early to prevent further execution with an invalid function pointer.
This change improves the reliability of the function, particularly in scenarios where moveFunction might not be properly initialized.
  • Loading branch information
dudantas authored Jan 5, 2025
1 parent 4778078 commit 3471783
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/lua/creature/movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,13 @@ uint32_t MoveEvent::fireAddRemItem(const std::shared_ptr<Item> &item, const std:
if (isLoadedScriptId()) {
return executeAddRemItem(item, fromTile, pos);
} else {
if (!moveFunction) {
g_logger().error("[MoveEvent::fireAddRemItem - Item {} item on position: {}] "
"Move function is nullptr.",
item->getName(), pos.toString());
return 0;
}

return moveFunction(item, fromTile, pos);
}
}
Expand Down Expand Up @@ -840,6 +847,13 @@ uint32_t MoveEvent::fireAddRemItem(const std::shared_ptr<Item> &item, const Posi
if (isLoadedScriptId()) {
return executeAddRemItem(item, pos);
} else {
if (!moveFunction) {
g_logger().error("[MoveEvent::fireAddRemItem - Item {} item on position: {}] "
"Move function is nullptr.",
item->getName(), pos.toString());
return 0;
}

return moveFunction(item, nullptr, pos);
}
}
Expand All @@ -849,9 +863,9 @@ bool MoveEvent::executeAddRemItem(const std::shared_ptr<Item> &item, const Posit
// onRemoveItem(moveitem, pos)
if (!LuaScriptInterface::reserveScriptEnv()) {
g_logger().error("[MoveEvent::executeAddRemItem - "
"Item {} item on tile x: {} y: {} z: {}] "
"Item {} item on position: {}] "
"Call stack overflow. Too many lua script calls being nested.",
item->getName(), pos.getX(), pos.getY(), pos.getZ());
item->getName(), pos.toString());
return false;
}

Expand Down

0 comments on commit 3471783

Please sign in to comment.