Skip to content

Commit

Permalink
fix: prevent segfault when loading invalid zone type (cataclysmbnteam…
Browse files Browse the repository at this point in the history
…#4398)

use `std::remove_if` to remove unsafe pointer arithmatic

see: https://en.cppreference.com/w/cpp/algorithm/remove
  • Loading branch information
scarf005 authored Mar 27, 2024
1 parent 0e06d77 commit a8986b4
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,15 +1113,16 @@ void zone_manager::serialize( JsonOut &json ) const
void zone_manager::deserialize( JsonIn &jsin )
{
jsin.read( zones );
for( auto it = zones.begin(); it != zones.end(); ++it ) {
const zone_type_id zone_type = it->get_type();
if( !has_type( zone_type ) ) {
it = zones.erase( it );
debugmsg( "Invalid zone type: %s", zone_type.c_str() );
} else {
it++;
}
}
zones.erase( std::remove_if( zones.begin(), zones.end(),
[this]( const zone_data & it ) -> bool {
const zone_type_id zone_type = it.get_type();
const bool is_valid = has_type( zone_type );

if( !is_valid ) debugmsg( "Invalid zone type: %s", zone_type.c_str() );

return is_valid;
} ),
zones.end() );
}

void zone_data::serialize( JsonOut &json ) const
Expand Down

0 comments on commit a8986b4

Please sign in to comment.