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

Protect Image.file_name #243

Merged
merged 1 commit into from
Jan 11, 2022
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
30 changes: 28 additions & 2 deletions src/glyph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::error::ConvertContourError;
#[cfg(feature = "druid")]
use druid::{Data, Lens};

use crate::error::{ErrorKind, GlifLoadError, GlifWriteError};
use crate::error::{ErrorKind, GlifLoadError, GlifWriteError, StoreError};
use crate::name::Name;
use crate::names::NameList;
use crate::shared_types::PUBLIC_OBJECT_LIBS_KEY;
Expand Down Expand Up @@ -735,13 +735,39 @@ impl std::default::Default for AffineTransform {
#[derive(Debug, Clone, PartialEq)]
pub struct Image {
/// The name of the image file. Must be a base file name, no subdirectories involved.
pub file_name: PathBuf,
file_name: PathBuf,
/// Optional image color.
pub color: Option<Color>,
/// Affine transformation.
pub transform: AffineTransform,
}

impl Image {
/// Create a new image.
pub fn new(
file_name: PathBuf,
color: Option<Color>,
transform: AffineTransform,
) -> Result<Self, StoreError> {
// Note: Mostly mirrors [`Self::validate_entry`].
if file_name.as_os_str().is_empty() {
return Err(StoreError::EmptyPath);
}
if file_name.is_absolute() {
return Err(StoreError::PathIsAbsolute);
}
if file_name.parent().map_or(false, |p| !p.as_os_str().is_empty()) {
return Err(StoreError::Subdir);
}
Ok(Self { file_name, color, transform })
}

/// Returns the file name of the image.
pub fn file_name(&self) -> &Path {
self.file_name.as_path()
}
}

#[cfg(feature = "kurbo")]
impl From<AffineTransform> for kurbo::Affine {
fn from(src: AffineTransform) -> kurbo::Affine {
Expand Down
5 changes: 4 additions & 1 deletion src/glyph/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,10 @@ impl<'names> GlifParser<'names> {

match filename {
Some(file_name) => {
self.glyph.image = Some(Image { file_name, color, transform });
self.glyph.image = Some(
Image::new(file_name, color, transform)
.map_err(|_| GlifLoadError::Parse(ErrorKind::BadImage))?,
);
Ok(())
}
None => Err(ErrorKind::BadImage.into()),
Expand Down
2 changes: 1 addition & 1 deletion src/glyph/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl Color {
impl Image {
fn to_event(&self) -> Event {
let mut start = BytesStart::borrowed_name(b"image");
start.push_attribute(("fileName", self.file_name.to_str().unwrap_or("missing path")));
start.push_attribute(("fileName", self.file_name.to_str().expect("missing path")));

write_transform_attributes(&mut start, &self.transform);

Expand Down