Skip to content

Commit

Permalink
Avoid division by zero in dai_military_bodyguard
Browse files Browse the repository at this point in the history
Coverity CID 1435482, 1435595.
See #2386.
  • Loading branch information
lmoureaux committed Oct 26, 2024
1 parent aa98e98 commit 6e5abce
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ai/default/aiunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,14 @@ static void dai_military_bodyguard(struct ai_type *ait,
int me2them = real_map_distance(unit_tile(punit), unit_tile(aunit));
int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
int unit_mv_rate = unit_move_rate(punit);
fc_assert_ret_msg(unit_mv_rate, "div by zero");
int punit_mv_rate = unit_move_rate(punit);
int aunit_mv_rate = unit_move_rate(aunit);
fc_assert_ret_msg(punit_mv_rate, "div by zero");
fc_assert_ret_msg(aunit_mv_rate, "div by zero");
if (me2goal < me2them
|| (me2goal / unit_mv_rate < them2goal / unit_move_rate(aunit)
&& me2goal / unit_mv_rate < me2them / unit_mv_rate
&& unit_mv_rate > unit_move_rate(aunit))) {
|| (me2goal / punit_mv_rate < them2goal / aunit_mv_rate
&& me2goal / punit_mv_rate < me2them / punit_mv_rate
&& punit_mv_rate > aunit_mv_rate)) {
ptile = aunit->goto_tile;
} else {
ptile = unit_tile(aunit);
Expand Down

0 comments on commit 6e5abce

Please sign in to comment.