Skip to content

Commit

Permalink
add assertions after many unchecked dynamic casts
Browse files Browse the repository at this point in the history
These were all reported by coverity, and indeed seem to have been
assumed to work without failing at runtime.
  • Loading branch information
cbeck88 committed Nov 19, 2014
1 parent d27aab7 commit f5e2204
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/game_initialization/create_engine.cpp
Expand Up @@ -811,6 +811,8 @@ void create_engine::set_current_level(const size_t index)
random_map* current_random_map =
dynamic_cast<random_map*>(&current_level());

assert(current_random_map); // if dynamic cast has failed then we somehow have gotten all the pointers mixed together.

generator_.reset(current_random_map->create_map_generator());
} else {
generator_.reset(NULL);
Expand Down
4 changes: 4 additions & 0 deletions src/game_initialization/multiplayer_create.cpp
Expand Up @@ -368,6 +368,8 @@ void create::process_event()
ng::scenario* current_scenario =
dynamic_cast<ng::scenario*>(&engine_.current_level());

assert(current_scenario);

players << current_scenario->num_players();
map_size << _("Size: ") << current_scenario->map_size();

Expand All @@ -378,6 +380,8 @@ void create::process_event()
ng::campaign* current_campaign =
dynamic_cast<ng::campaign*>(&engine_.current_level());

assert(current_campaign);

players << current_campaign->min_players();
if (current_campaign->max_players() !=
current_campaign->min_players()) {
Expand Down
6 changes: 3 additions & 3 deletions src/gui/widgets/widget.cpp
Expand Up @@ -96,12 +96,12 @@ twidget::~twidget()

void twidget::set_id(const std::string& id)
{
tcontrol* this_ctrl = dynamic_cast<tcontrol*>(this);

DBG_GUI_LF
<< "set id of " << static_cast<void*>(this) << " to '" << id << "' "
<< "(was '" << id_ << "'). Widget type: "
<< (dynamic_cast<tcontrol*>(this)
? dynamic_cast<tcontrol*>(this)->get_control_type()
: typeid(twidget).name()) << "\n";
<< (this_ctrl ? this_ctrl->get_control_type() : typeid(twidget).name()) << "\n";

id_ = id;
}
Expand Down
3 changes: 3 additions & 0 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -2781,6 +2781,9 @@ static int intf_debug_ai(lua_State *L)
//so set up a dummy engine

ai::ai_composite * ai_ptr = dynamic_cast<ai::ai_composite *>(c);

assert(ai_ptr);

ai::ai_context& ai_context = ai_ptr->get_ai_context();
config cfg = ai::configuration::get_default_ai_parameters();

Expand Down

0 comments on commit f5e2204

Please sign in to comment.