Skip to content

Commit

Permalink
fix: disallow tileset width/height from being 0
Browse files Browse the repository at this point in the history
Fixes a divide by zero panic
  • Loading branch information
smackysnacks committed May 30, 2024
1 parent 4ff6eea commit 984067d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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 @@ -78,6 +100,8 @@ pub enum Error {
/// Stores the wrongly parsed String.
read_string: 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 @@ -126,6 +150,7 @@ impl fmt::Display for Error {
Error::TemplateHasNoObject => write!(fmt, "A template was found with no object element"),
Error::InvalidWangIdEncoding{read_string} =>
write!(fmt, "\"{}\" is not a valid WangId format", read_string),
Error::InvalidTileset(e) => write!(fmt, "{}", e),
}
}
}
Expand Down
14 changes: 13 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 @@ -153,6 +153,12 @@ impl Tileset {
((spacing, margin, columns, name, user_type, user_class), (tilecount, first_gid, tile_width, tile_height))
);

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

let root_path = path.parent().ok_or(Error::PathIsNotFile)?.to_owned();

Self::finish_parsing_xml(
Expand Down Expand Up @@ -223,6 +229,12 @@ impl Tileset {
((spacing, margin, columns, name, user_type, user_class), (tilecount, tile_width, tile_height))
);

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

let root_path = path.parent().ok_or(Error::PathIsNotFile)?.to_owned();

Self::finish_parsing_xml(
Expand Down

0 comments on commit 984067d

Please sign in to comment.