Error: Add ErrorKind::SizeMismatch

This commit is contained in:
Serial 2022-10-13 19:49:32 -04:00
parent 2ff86e6e00
commit fcd9cee50f
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
8 changed files with 21 additions and 12 deletions

View file

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
and can be overridden when using `Probe`.
- **FileProperties**: `FileProperties::new`
- Debug logging via the [log](https://crates.io/crates/log) crate for exposing recoverable errors.
- **Error**: `ErrorKind::SizeMismatch`
### Changed
- **ID3v2**: Frame/tag flags with optional additional data are now `Option<T>` instead of `(bool, T)`

View file

@ -24,7 +24,7 @@ where
let value_size = data.read_u32::<LittleEndian>()?;
if value_size > remaining_size {
err!(TooMuchData);
err!(SizeMismatch);
}
remaining_size -= 4;

View file

@ -24,6 +24,11 @@ pub enum ErrorKind {
// File data related errors
/// Attempting to read/write an abnormally large amount of data
TooMuchData,
/// Expected the data to be a different size than provided
///
/// This occurs when the size of an item is written as one value, but that size is either too
/// big or small to be valid within the bounds of that item.
SizeMismatch,
/// Errors that occur while decoding a file
FileDecoding(FileDecodingError),
/// Errors that occur while encoding a file
@ -359,10 +364,6 @@ impl Display for LoftyError {
ErrorKind::UnknownFormat => {
write!(f, "No format could be determined from the provided file")
},
ErrorKind::TooMuchData => write!(
f,
"An abnormally large amount of data was provided, and an overflow occurred"
),
ErrorKind::NotAPicture => write!(f, "Picture: Encountered invalid data"),
ErrorKind::UnsupportedPicture => {
write!(f, "Picture: attempted to write an unsupported picture")
@ -377,6 +378,14 @@ impl Display for LoftyError {
ErrorKind::BadAtom(message) => write!(f, "MP4 Atom: {message}"),
// Files
ErrorKind::TooMuchData => write!(
f,
"An abnormally large amount of data was provided, and an overflow occurred"
),
ErrorKind::SizeMismatch => write!(
f,
"Encountered an invalid item size, either too big or too small to be valid"
),
ErrorKind::FileDecoding(ref file_decode_err) => write!(f, "{file_decode_err}"),
ErrorKind::FileEncoding(ref file_encode_err) => write!(f, "{file_encode_err}"),
}

View file

@ -82,7 +82,7 @@ impl<B: ByteOrder> Chunks<B> {
R: Read,
{
if size > self.remaining_size {
err!(TooMuchData);
err!(SizeMismatch);
}
let mut content = try_vec![0; size as usize];

View file

@ -75,7 +75,7 @@ impl AtomInfo {
// `len` includes itself
if (len - 4) > reader_size {
data.seek(SeekFrom::Current(-4))?;
err!(TooMuchData);
err!(SizeMismatch);
}
let mut atom_ident = AtomIdent::Fourcc(ident);

View file

@ -62,7 +62,7 @@ where
pub(super) fn next(&mut self) -> Result<AtomInfo> {
if self.remaining_size < 8 {
err!(TooMuchData);
err!(SizeMismatch);
}
AtomInfo::read(self, self.remaining_size)

View file

@ -27,7 +27,7 @@ where
let vendor_len = data.read_u32::<LittleEndian>()?;
if u64::from(vendor_len) > len {
err!(TooMuchData);
err!(SizeMismatch);
}
let mut vendor = try_vec![0; vendor_len as usize];
@ -61,8 +61,7 @@ where
for _ in 0..comments_total_len {
let comment_len = data.read_u32::<LittleEndian>()?;
if u64::from(comment_len) > len {
// TODO: Maybe add ErrorKind::SizeMismatch?
err!(TooMuchData);
err!(SizeMismatch);
}
let mut comment_bytes = try_vec![0; comment_len as usize];

View file

@ -779,7 +779,7 @@ impl Picture {
size -= 4;
if mime_len > size {
err!(TooMuchData);
err!(SizeMismatch);
}
let mime_type_str = std::str::from_utf8(&content[8..8 + mime_len])?;