Skip to content

ID3v2: Ignore empty timestamp frames #416

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

Merged
merged 1 commit into from
Jul 4, 2024
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
7 changes: 6 additions & 1 deletion lofty/src/id3/v2/items/timestamp_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ impl<'a> TimestampFrame<'a> {

let reader = &mut value.as_bytes();

frame.timestamp = Timestamp::parse(reader, parse_mode)?;
let Some(timestamp) = Timestamp::parse(reader, parse_mode)? else {
// Timestamp is empty
return Ok(None);
};

frame.timestamp = timestamp;
Ok(Some(frame))
}

Expand Down
33 changes: 26 additions & 7 deletions lofty/src/tag/items/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl FromStr for Timestamp {
type Err = LoftyError;

fn from_str(s: &str) -> Result<Self> {
Timestamp::parse(&mut s.as_bytes(), ParsingMode::BestAttempt)
Timestamp::parse(&mut s.as_bytes(), ParsingMode::BestAttempt)?
.ok_or_else(|| LoftyError::new(ErrorKind::BadTimestamp("Timestamp frame is empty")))
}
}

Expand All @@ -86,7 +87,7 @@ impl Timestamp {
///
/// * Failure to read from `reader`
/// * The timestamp is invalid
pub fn parse<R>(reader: &mut R, parse_mode: ParsingMode) -> Result<Self>
pub fn parse<R>(reader: &mut R, parse_mode: ParsingMode) -> Result<Option<Self>>
where
R: Read,
{
Expand All @@ -109,6 +110,14 @@ impl Timestamp {
.take(Self::MAX_LENGTH as u64)
.read_to_end(&mut content)?;

if content.is_empty() {
if parse_mode == ParsingMode::Strict {
err!(BadTimestamp("Timestamp frame is empty"))
}

return Ok(None);
}

let reader = &mut &content[..];

// We need to very that the year is exactly 4 bytes long. This doesn't matter for other segments.
Expand All @@ -131,7 +140,7 @@ impl Timestamp {
break;
}

Ok(timestamp)
Ok(Some(timestamp))
}

fn segment<const SIZE: usize>(
Expand Down Expand Up @@ -237,7 +246,7 @@ mod tests {
let parsed_timestamp =
Timestamp::parse(&mut content.as_bytes(), ParsingMode::Strict).unwrap();

assert_eq!(parsed_timestamp, expected());
assert_eq!(parsed_timestamp, Some(expected()));
}

#[test]
Expand All @@ -248,7 +257,7 @@ mod tests {
let parsed_timestamp =
Timestamp::parse(&mut content.as_bytes(), ParsingMode::BestAttempt).unwrap();

assert_eq!(parsed_timestamp, expected());
assert_eq!(parsed_timestamp, Some(expected()));
}

#[test]
Expand All @@ -259,7 +268,7 @@ mod tests {
let parsed_timestamp =
Timestamp::parse(&mut content.as_bytes(), ParsingMode::BestAttempt).unwrap();

assert_eq!(parsed_timestamp, expected());
assert_eq!(parsed_timestamp, Some(expected()));
}

#[test]
Expand Down Expand Up @@ -348,7 +357,17 @@ mod tests {

for (data, expected) in partial_timestamps {
let parsed_timestamp = Timestamp::parse(&mut &data[..], ParsingMode::Strict).unwrap();
assert_eq!(parsed_timestamp, expected);
assert_eq!(parsed_timestamp, Some(expected));
}
}

#[test]
fn empty_timestamp() {
let empty_timestamp =
Timestamp::parse(&mut "".as_bytes(), ParsingMode::BestAttempt).unwrap();
assert!(empty_timestamp.is_none());

let empty_timestamp_strict = Timestamp::parse(&mut "".as_bytes(), ParsingMode::Strict);
assert!(empty_timestamp_strict.is_err());
}
}