Skip to content

Commit 9403f17

Browse files
authored
Adds safety checks to scripting orders with waypoints (#5173)
* Adds safety checks to scripting orders with waypoints Turns out setting a waypoint order then trying to get to target of that order in the same frame triggers a crash in the scripting API for ship orders (`order.cpp` line 295). This was due to invalid lookups which lacked safety checks. This PR adds those safety checks. Now, if this situation is encountered, it leaves `objnum` at -1 and thus returns an object for which `isValid` is false. Fix is tested and works as expected. Also fixes #5172 * appease clang * cleanup thanks to Goober * clang again * forgot to re-add the nullptr * remove redundant checks
1 parent 9a7b3fe commit 9403f17

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

code/scripting/api/objs/order.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,15 @@ ADE_VIRTVAR(Target, l_Order, "object", "Target of the order. Value may also be a
289289
break;
290290
case AI_GOAL_WAYPOINTS:
291291
case AI_GOAL_WAYPOINTS_ONCE:
292-
wpl = find_matching_waypoint_list(ohp->aigp->target_name);
293-
if(ohp->odx == 0) {
292+
// check if waypoint order is the current goal (ohp->odx == 0) and if it is valid
293+
if ( (ohp->odx == 0) && (aip->wp_index != INVALID_WAYPOINT_POSITION) &&
294+
(aip->wp_index < aip->wp_list->get_waypoints().size()) ) {
294295
objnum = aip->wp_list->get_waypoints()[aip->wp_index].get_objnum();
295296
} else {
296-
objnum = wpl->get_waypoints().front().get_objnum();
297+
wpl = find_matching_waypoint_list(ohp->aigp->target_name);
298+
if (wpl != nullptr) {
299+
objnum = wpl->get_waypoints().front().get_objnum();
300+
}
297301
}
298302
break;
299303
case AI_GOAL_STAY_STILL:

0 commit comments

Comments
 (0)