swf: Remove panic when data left over in tag

This commit is contained in:
Mike Welsh 2019-10-10 15:20:54 -07:00
parent 800147043a
commit 614903bdec
2 changed files with 21 additions and 3 deletions

View File

@ -657,10 +657,20 @@ impl<R: Read> Reader<R> {
}
};
if cfg!(debug_assertions) && tag_reader.read_u8().is_ok() {
if tag_reader.read_u8().is_ok() {
// There should be no data remaining in the tag if we read it correctly.
// If there is data remaining, we probably screwed up, so panic in debug builds.
panic!("Error reading tag {:?}", tag_code);
// If there is data remaining, the most likely scenario is we screwed up parsing.
// But sometimes tools will export SWF tags that are larger than they should be.
// TODO: It might be worthwhile to have a "strict mode" to determine
// whether this should error or not.
log::warn!(
"Data remaining in buffer when parsing {} ({})",
TagCode::name(tag_code),
tag_code
);
// Discard the rest of the data.
let _ = std::io::copy(&mut tag_reader.get_mut(), &mut io::sink());
}
Ok(tag)

View File

@ -95,4 +95,12 @@ impl TagCode {
pub fn from_u16(n: u16) -> Option<Self> {
num_traits::FromPrimitive::from_u16(n)
}
pub fn name(n: u16) -> String {
if let Some(n) = TagCode::from_u16(n) {
format!("{:?}", n)
} else {
format!("Unknown({})", n)
}
}
}