diff --git a/src/map.cpp b/src/map.cpp index 392ab8ab6c..a8023c9226 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -32,6 +32,25 @@ uint _map_tile_mask; ///< _map_size - 1 (to mask the mapsize) Tile *_m = NULL; ///< Tiles of the map TileExtended *_me = NULL; ///< Extended Tiles of the map +/** + * Validates whether a map with the given dimension is valid + * @param size_x the width of the map along the NE/SW edge + * @param size_y the 'height' of the map along the SE/NW edge + * @return true if valid, or false if not valid + */ +bool ValidateMapSize(uint size_x, uint size_y) +{ + /* Make sure that the map size is within the limits and that + * size of both axes is a power of 2. */ + if (size_x * size_y > MAX_MAP_TILES || + size_x < MIN_MAP_SIZE || + size_y < MIN_MAP_SIZE || + (size_x & (size_x - 1)) != 0 || + (size_y & (size_y - 1)) != 0) { + return false; + } + return true; +} /** * (Re)allocates a map with the given dimension @@ -43,13 +62,7 @@ void AllocateMap(uint size_x, uint size_y) DEBUG(map, 2, "Min/max map size %d/%d, max map tiles %d", MIN_MAP_SIZE, MAX_MAP_SIZE, MAX_MAP_TILES); DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y); - /* Make sure that the map size is within the limits and that - * size of both axes is a power of 2. */ - if (size_x * size_y > MAX_MAP_TILES || - size_x < MIN_MAP_SIZE || - size_y < MIN_MAP_SIZE || - (size_x & (size_x - 1)) != 0 || - (size_y & (size_y - 1)) != 0) { + if (!ValidateMapSize(size_x, size_y)) { error("Invalid map size"); } diff --git a/src/map_func.h b/src/map_func.h index 9198c2cd1f..b390f0eaef 100644 --- a/src/map_func.h +++ b/src/map_func.h @@ -43,6 +43,7 @@ extern Tile *_m; */ extern TileExtended *_me; +bool ValidateMapSize(uint size_x, uint size_y); void AllocateMap(uint size_x, uint size_y); /** diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp index 86a185ca42..ee157ec17c 100644 --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -37,6 +37,9 @@ static void Save_MAPS() static void Load_MAPS() { SlGlobList(_map_dimensions); + if (!ValidateMapSize(_map_dim_x, _map_dim_y)) { + SlErrorCorruptFmt("Invalid map size: %u x %u", _map_dim_x, _map_dim_y); + } AllocateMap(_map_dim_x, _map_dim_y); }