Skip to content

Commit

Permalink
Merge pull request CleverRaven#49906 from kevingranade/mm-stop-UB
Browse files Browse the repository at this point in the history
Return an invalid mm_submap if the map_memory is not prepared
  • Loading branch information
ZhilkinSerg authored Jul 14, 2021
2 parents 7b88ade + 0eead1f commit 7d23809
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/map_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct reg_coord_pair {
};

mm_submap::mm_submap() = default;
mm_submap::mm_submap( bool make_valid ) : valid( make_valid ) {}

mm_region::mm_region() : submaps {{ nullptr }} {}

Expand Down Expand Up @@ -82,6 +83,9 @@ void map_memory::memorize_tile( const tripoint &pos, const std::string &ter,
{
coord_pair p( pos );
mm_submap &sm = get_submap( p.sm );
if( !sm.is_valid() ) {
return;
}
sm.set_tile( p.loc, memorized_terrain_tile{ ter, subtile, rotation } );
}

Expand All @@ -96,13 +100,19 @@ void map_memory::memorize_symbol( const tripoint &pos, const int symbol )
{
coord_pair p( pos );
mm_submap &sm = get_submap( p.sm );
if( !sm.is_valid() ) {
return;
}
sm.set_symbol( p.loc, symbol );
}

void map_memory::clear_memorized_tile( const tripoint &pos )
{
coord_pair p( pos );
mm_submap &sm = get_submap( p.sm );
if( !sm.is_valid() ) {
return;
}
sm.set_symbol( p.loc, mm_submap::default_symbol );
sm.set_tile( p.loc, mm_submap::default_tile );
}
Expand Down Expand Up @@ -235,10 +245,15 @@ shared_ptr_fast<mm_submap> map_memory::load_submap( const tripoint &sm_pos )
}

static mm_submap null_mz_submap;
static mm_submap invalid_mz_submap{ false };

const mm_submap &map_memory::get_submap( const tripoint &sm_pos ) const
{
point idx = ( sm_pos - cache_pos ).xy();
if( cache_pos == tripoint_min ) {
debugmsg( "Called map_memory with an " );
return invalid_mz_submap;
}
const point idx = ( sm_pos - cache_pos ).xy();
if( idx.x > 0 && idx.y > 0 && idx.x < cache_size.x && idx.y < cache_size.y ) {
return *cached[idx.y * cache_size.x + idx.x];
} else {
Expand All @@ -248,7 +263,10 @@ const mm_submap &map_memory::get_submap( const tripoint &sm_pos ) const

mm_submap &map_memory::get_submap( const tripoint &sm_pos )
{
point idx = ( sm_pos - cache_pos ).xy();
if( cache_pos == tripoint_min ) {
return invalid_mz_submap;
}
const point idx = ( sm_pos - cache_pos ).xy();
if( idx.x > 0 && idx.y > 0 && idx.x < cache_size.x && idx.y < cache_size.y ) {
return *cached[idx.y * cache_size.x + idx.x];
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/map_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ struct mm_submap {
static const int default_symbol;

mm_submap();
explicit mm_submap( bool make_valid );

/** Whether this mm_submap is empty. Empty submaps are skipped during saving. */
bool is_empty() const {
return tiles.empty() && symbols.empty();
}

// Whether this mm_submap is invalid, i.e. returned from an uninitialized region.
bool is_valid() const {
return valid;
}

inline const memorized_terrain_tile &tile( const point &p ) const {
if( tiles.empty() ) {
return default_tile;
Expand Down Expand Up @@ -80,6 +86,7 @@ struct mm_submap {
private:
std::vector<memorized_terrain_tile> tiles; // holds either 0 or SEEX*SEEY elements
std::vector<int> symbols; // holds either 0 or SEEX*SEEY elements
bool valid = true;
};

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/map_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ TEST_CASE( "map_memory_overwrites", "[map_memory]" )
CHECK( memory.get_symbol( p2 ) == 3 );
}

TEST_CASE( "map_memory_forgets", "[map_memory]" )
{
map_memory memory;
memory.memorize_symbol( tripoint_zero, 1 );
memory.memorize_symbol( p3, 1 );
}

// TODO: map memory save / load

#include <chrono>
Expand Down

0 comments on commit 7d23809

Please sign in to comment.