Skip to content

Commit

Permalink
catch bad lexical casts (from parsing map locations from configs)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed May 18, 2014
1 parent 7247cb4 commit a7b81ea
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
29 changes: 22 additions & 7 deletions src/actions/undo.cpp
Expand Up @@ -236,12 +236,13 @@ undo_list::undo_action::create(const config & cfg, const std::string & tag)
// constructors will parse the "unit" child config, while this function
// parses everything else.

if ( str == "move" )
if ( str == "move" ) {
res = new move_action(cfg.child("unit", tag), cfg,
cfg["starting_moves"],
cfg["time_bonus"],
cfg["village_owner"],
map_location::parse_direction(cfg["starting_direction"]));
}

if ( str == "recruit" ) {
// Validate the unit type.
Expand Down Expand Up @@ -532,16 +533,30 @@ void undo_list::read(const config & cfg)

// Build the undo stack.
BOOST_FOREACH( const config & child, cfg.child_range("undo") ) {
undo_action * action = undo_action::create(child, "[undo]");
if ( action )
undos_.push_back(action);
try {
undo_action * action = undo_action::create(child, "[undo]");
if ( action ) {
undos_.push_back(action);
}
} catch (bad_lexical_cast &) {
ERR_NG << "Error when parsing undo list from config: bad lexical cast." << std::endl;
ERR_NG << "config was: " << child.debug() << std::endl;
ERR_NG << "Skipping this undo action..." << std::endl;
}
}

// Build the redo stack.
BOOST_FOREACH( const config & child, cfg.child_range("redo") ) {
undo_action * action = undo_action::create(child, "[redo]");
if ( action )
redos_.push_back(action);
try {
undo_action * action = undo_action::create(child, "[redo]");
if ( action ) {
redos_.push_back(action);
}
} catch (bad_lexical_cast &) {
ERR_NG << "Error when parsing redo list from config: bad lexical cast." << std::endl;
ERR_NG << "config was: " << child.debug() << std::endl;
ERR_NG << "Skipping this redo action..." << std::endl;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/actions/undo.hpp
Expand Up @@ -75,6 +75,7 @@ class undo_list : boost::noncopyable {


/// Creates an undo_action based on a config.
/// Throws bad_lexical_cast if it cannot parse the config properly.
static undo_action * create(const config & cfg, const std::string & tag);
/// Writes this into the provided config.
virtual void write(config & cfg) const = 0;
Expand Down
11 changes: 9 additions & 2 deletions src/game_events/action_wml.cpp
Expand Up @@ -2247,8 +2247,15 @@ WML_HANDLER_FUNCTION(set_variables, /*event_info*/, cfg)

WML_HANDLER_FUNCTION(sound_source, /*event_info*/, cfg)
{
soundsource::sourcespec spec(cfg.get_parsed_config());
resources::soundsources->add(spec);
config parsed = cfg.get_parsed_config();
try {
soundsource::sourcespec spec(parsed);
resources::soundsources->add(spec);
} catch (bad_lexical_cast &) {
ERR_NG << "Error when parsing sound_source config: bad lexical cast." << std::endl;
ERR_NG << "sound_source config was: " << parsed.debug() << std::endl;
ERR_NG << "Skipping this sound source..." << std::endl;
}
}

/// Store time of day config in a WML variable. This is useful for those who
Expand Down
6 changes: 5 additions & 1 deletion src/map_location.hpp
Expand Up @@ -126,7 +126,11 @@ std::vector<map_location> parse_location_range(const std::string& xvals,
*/
void write_location_range(const std::set<map_location>& locs, config& cfg);

/** Parse x,y keys of a config into a vector of locations */
/**
* Parse x,y keys of a config into a vector of locations
*
* Throws bad_lexical_cast if it fails to parse.
*/
void read_locations(const config& cfg, std::vector<map_location>& locs);

/** Write a vector of locations into a config
Expand Down
10 changes: 8 additions & 2 deletions src/playsingle_controller.cpp
Expand Up @@ -355,8 +355,14 @@ LEVEL_RESULT playsingle_controller::play_scenario(
// Read sound sources
assert(soundsources_manager_ != NULL);
BOOST_FOREACH(const config &s, level_.child_range("sound_source")) {
soundsource::sourcespec spec(s);
soundsources_manager_->add(spec);
try {
soundsource::sourcespec spec(s);
soundsources_manager_->add(spec);
} catch (bad_lexical_cast &) {
ERR_NG << "Error when parsing sound_source config: bad lexical cast." << std::endl;
ERR_NG << "sound_source config was: " << s.debug() << std::endl;
ERR_NG << "Skipping this sound source..." << std::endl;
}
}

set_victory_when_enemies_defeated(level_["victory_when_enemies_defeated"].to_bool(true));
Expand Down

0 comments on commit a7b81ea

Please sign in to comment.