Fix unit_defense report generator doing unchecked map access (bug #17721)

This commit fixes report_unit_defense() to return an empty report
object if it detects that the game_display::displayed_unit_hex()
return value is not a valid map location. This problem is not really
recent, but fendrin's odd usage of the engine to display unit type
info on his recruit dialog unveiled this flaw in the worst manner
possible.
This commit is contained in:
Ignacio R. Morelle 2011-02-19 08:57:37 +00:00
parent 785b8d203d
commit 93ecd8498b

View file

@ -348,7 +348,9 @@ REPORT_GENERATOR(unit_defense)
if (!u) return report();
std::ostringstream str, tooltip;
const gamemap &map = *resources::game_map;
const t_translation::t_terrain &terrain = map[resources::screen->displayed_unit_hex()];
const map_location& displayed_unit_hex = resources::screen->displayed_unit_hex();
if (!resources::game_map->on_board(displayed_unit_hex)) return report();
const t_translation::t_terrain &terrain = map[displayed_unit_hex];
int def = 100 - u->defense_modifier(terrain);
SDL_Color color = int_to_color(game_config::red_to_green(def));
str << span_color(color) << def << "%</span>";