Fix panic reading MP3 files with no MPEG frames

This commit is contained in:
Serial 2022-04-24 16:56:13 -04:00
parent 87315321ef
commit 89ebe2ed17
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
2 changed files with 24 additions and 7 deletions

View file

@ -96,13 +96,6 @@ where
// We have found the first frame
break;
}
// The search for sync bits was unsuccessful
return Err(FileDecodingError::new(
FileType::MP3,
"File contains an invalid frame",
)
.into());
},
}
}
@ -140,6 +133,13 @@ where
file.last_frame_offset = reader.stream_position()?;
file.properties = if read_properties {
if first_frame_header.is_none() {
// The search for sync bits was unsuccessful
return Err(
FileDecodingError::new(FileType::MP3, "File contains an invalid frame").into(),
);
}
// Safe to unwrap, since we return early if no frame is found
let first_frame_header = first_frame_header.unwrap();
@ -172,3 +172,20 @@ where
Ok(file)
}
#[cfg(test)]
mod tests {
use crate::file::AudioFile;
use crate::mp3::Mp3File;
use std::fs::File;
#[test]
fn issue_39() {
// MP3 file that only consists of an ID3v2 tag
assert!(Mp3File::read_from(
&mut File::open("tests/files/assets/issue_39.mp3").unwrap(),
true,
)
.is_err());
}
}

Binary file not shown.