Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix divide by zero when giving a tileset width of zero #292

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Panic in `<Color as FromStr>::from_str` when parsing non-ascii input. (#290)
- Index out of bounds in `InfiniteTileLayerData` when parsing a chunk. (#289)
- Panic on unexpected XML in `ObjectData` content. (#291)
- Divide by zero when parsing a tileset with height/width dimension of 0. (#292)

## [Unreleased]
## Changed
Expand Down
25 changes: 25 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::InvalidTilesetError::InvalidTileDimensions;
use std::num::ParseIntError;
use std::{fmt, path::PathBuf};

Expand All @@ -19,6 +20,27 @@ impl fmt::Display for CsvDecodingError {

impl std::error::Error for CsvDecodingError {}

/// Errors that can occur parsing a Tileset.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum InvalidTilesetError {
/// An invalid width or height (0) dimension was found in the input.
InvalidTileDimensions,
}

impl fmt::Display for InvalidTilesetError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InvalidTileDimensions => write!(
f,
"An invalid width or height (0) dimension was found in the input."
),
}
}
}

impl std::error::Error for InvalidTilesetError {}

/// Errors which occurred when parsing the file
#[derive(Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -83,6 +105,8 @@ pub enum Error {
/// A description of the error that occurred.
description: String,
},
/// There was an invalid tileset in the map parsed.
InvalidTileset(InvalidTilesetError),
}

/// A result with an error variant of [`crate::Error`].
Expand Down Expand Up @@ -133,6 +157,7 @@ impl fmt::Display for Error {
write!(fmt, "\"{}\" is not a valid WangId format", read_string),
Error::InvalidObjectData{description} =>
write!(fmt, "Invalid object data: {}", description),
Error::InvalidTileset(e) => write!(fmt, "{}", e),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/tileset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::error::{Error, Result};
use crate::image::Image;
use crate::properties::{parse_properties, Properties};
use crate::tile::TileData;
use crate::{util::*, Gid, ResourceCache, ResourceReader, Tile, TileId};
use crate::{util::*, Gid, InvalidTilesetError, ResourceCache, ResourceReader, Tile, TileId};

mod wangset;
pub use wangset::*;
Expand Down Expand Up @@ -284,6 +284,12 @@ impl Tileset {
let is_image_collection_tileset = image.is_none();

if !is_image_collection_tileset {
if prop.tile_width == 0 || prop.tile_height == 0 {
return Err(Error::InvalidTileset(
InvalidTilesetError::InvalidTileDimensions,
));
}

for tile_id in 0..prop.tilecount {
tiles.entry(tile_id).or_default();
}
Expand Down
Loading