Skip to content

Commit f95ae0b

Browse files
authored
Prevent Potential Memory Problems with squadmsg hook function (#6801)
* Prevent Potential Memory Problems with squadmsg hook function Within `hud_squadmsg_run_order_issued_hook` it is possible for the calling code to pass `nullptr` as the target parameter, which would cause memory problems with the several instances of `Objects[target->objnum]`. This PR mitigates that potential while testing of `hud_squadmsg_run_order_issued_hook` still works as expected. * add more safety
1 parent 94dac6b commit f95ae0b

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

code/hud/hudsquadmsg.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ scripting::api::lua_enum hud_squadmsg_get_order_scripting_enum(int command)
983983

984984
// Run the order issued hook. When an order is issued we first check if we should override. If so, then we run the hook and return true
985985
// which will skip the rest of the order code.
986-
bool hud_squadmsg_run_order_issued_hook(int command, ship* sendingShip, ship* recipientShip, wing* recipientWing, ship* target, ship_subsys* subsys)
986+
bool hud_squadmsg_run_order_issued_hook(int command, ship* sendingShip, ship* recipientShip, wing* recipientWing, ship* targetShip, ship_subsys* subsys)
987987
{
988988
bool isOverride = false;
989989

@@ -996,21 +996,24 @@ bool hud_squadmsg_run_order_issued_hook(int command, ship* sendingShip, ship* re
996996
recipient = recipientWing;
997997
}
998998

999+
auto sendingObject = (sendingShip == nullptr) ? nullptr : &Objects[sendingShip->objnum];
1000+
auto targetObject = (targetShip == nullptr) ? nullptr : &Objects[targetShip->objnum];
1001+
9991002
auto paramList = scripting::hook_param_list(
1000-
scripting::hook_param("Sender", 'o', &Objects[sendingShip->objnum]),
1003+
scripting::hook_param("Sender", 'o', sendingObject),
10011004
scripting::hook_param("Recipient", 'o', scripting::api::l_OSWPT.Set(recipient)),
1002-
scripting::hook_param("Target", 'o', &Objects[target->objnum]),
1003-
scripting::hook_param("Subsystem", 'o', scripting::api::l_Subsystem.Set(scripting::api::ship_subsys_h(&Objects[target->objnum], subsys))),
1005+
scripting::hook_param("Target", 'o', targetObject),
1006+
scripting::hook_param("Subsystem", 'o', scripting::api::l_Subsystem.Set(scripting::api::ship_subsys_h(targetObject, subsys))),
10041007
scripting::hook_param("Order", 'o', scripting::api::l_Enum.Set(scripting::api::enum_h(hud_squadmsg_get_order_scripting_enum(command)))),
10051008
scripting::hook_param("Name", 's', hud_squadmsg_get_order_name(command).c_str())
10061009
);
10071010
if (scripting::hooks::OnHudCommOrderIssued->isOverride(
1008-
scripting::hooks::CommOrderConditions{sendingShip, &Objects[target->objnum], &recipient},
1011+
scripting::hooks::CommOrderConditions{sendingShip, targetObject, &recipient},
10091012
paramList)) {
10101013
isOverride = true;
10111014
}
10121015
scripting::hooks::OnHudCommOrderIssued->run(
1013-
scripting::hooks::CommOrderConditions{sendingShip, &Objects[target->objnum], &recipient},
1016+
scripting::hooks::CommOrderConditions{sendingShip, targetObject, &recipient},
10141017
paramList);
10151018
}
10161019

0 commit comments

Comments
 (0)