From 229103270e9ac024d7d127df1bd57cd914c039ba Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 26 May 2023 23:17:30 +0300 Subject: [PATCH 5/5] Fix ap_diplomat_battle() clang analyzer warnings - Add nonnull attribute for tgt_tile parameter - Make sure that no caller pass NULL tgt_tile - Refactor calling side to avoid duplicate code See osdn #48069 Signed-off-by: Marko Lindqvist --- common/actions.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/common/actions.c b/common/actions.c index 22d995a1bb..e97f121b2a 100644 --- a/common/actions.c +++ b/common/actions.c @@ -119,6 +119,11 @@ action_prob_not_relevant(const struct act_prob probability); static inline bool action_prob_not_impl(const struct act_prob probability); +static struct act_prob ap_diplomat_battle(const struct unit *pattacker, + const struct unit *pvictim, + const struct tile *tgt_tile) + fc__attribute((nonnull(3))); + /* Make sure that an action distance can target the whole map. */ FC_STATIC_ASSERT(MAP_DISTANCE_MAX <= ACTION_DISTANCE_LAST_NON_SIGNAL, action_range_can_not_cover_the_whole_map); @@ -3475,8 +3480,6 @@ static struct act_prob ap_diplomat_battle(const struct unit *pattacker, const struct unit *pvictim, const struct tile *tgt_tile) { - fc_assert_ret_val(tgt_tile, ACTPROB_NOT_KNOWN); - if (!can_player_see_hypotetic_units_at(unit_owner(pattacker), tgt_tile)) { /* Don't leak information about unseen defenders. */ @@ -3605,11 +3608,6 @@ action_prob(const action_id wanted_action, target_specialist)); switch (wanted_action) { - case ACTION_SPY_POISON: - case ACTION_SPY_POISON_ESC: - /* All uncertainty comes from potential diplomatic battles. */ - chance = ap_diplomat_battle(actor_unit, NULL, target_tile); - break; case ACTION_SPY_STEAL_GOLD: /* TODO */ break; @@ -3622,14 +3620,18 @@ action_prob(const action_id wanted_action, case ACTION_STEAL_MAPS_ESC: /* TODO */ break; + case ACTION_SPY_POISON: + case ACTION_SPY_POISON_ESC: case ACTION_SPY_SABOTAGE_UNIT: case ACTION_SPY_SABOTAGE_UNIT_ESC: - /* All uncertainty comes from potential diplomatic battles. */ - chance = ap_diplomat_battle(actor_unit, target_unit, target_tile); - break; case ACTION_SPY_BRIBE_UNIT: /* All uncertainty comes from potential diplomatic battles. */ - chance = ap_diplomat_battle(actor_unit, target_unit, target_tile); + if (target_tile != NULL) { + chance = ap_diplomat_battle(actor_unit, target_unit, target_tile); + } else { + /* Never should enter to this "else" branch in the first place. */ + fc_assert(action_id_get_target_kind(wanted_action) != ATK_SELF); + } break; case ACTION_SPY_SABOTAGE_CITY: /* TODO */ -- 2.39.2