Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup two divide-by-zero issues from Coverity #2386

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions ai/default/aiunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,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 Expand Up @@ -794,7 +796,7 @@ int look_for_charge(struct ai_type *ait, struct player *pplayer,
continue;
}
// Reduce want based on move cost.
def >>= move_cost / (2 * unit_move_rate(punit));
def >>= move_cost / (2 * std::min(1, unit_move_rate(punit)));
if (def > best_def && ai_fuzzy(pplayer, true)) {
*acity = pcity;
*aunit = nullptr;
Expand Down
Loading