Skip to content

Commit

Permalink
fix(content): Decode IPLS/TIPL/TMCL with odd value count gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus authored and polyfloyd committed Feb 8, 2025
1 parent adda14e commit d1c18df
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/stream/frame/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,21 @@ impl<'a> Decoder<'a> {
Some(result)
}
(Some(_), None) => {
// This can only happen if there is an uneven number of elements.
*last_string = None;
Some(Err(Error::new(
ErrorKind::Parsing,
"uneven number of IPLS strings",
)))
// This can only happen if there is an uneven number of elements. For
// compatibility, we assume that the missing value is an empty string instead of
// erroring out and failing to parse the entire tag.
//
// This is in line with what the Python mutagen library does. See this issue for
// details:
// - <https://github.com/polyfloyd/rust-id3/issues/147>
let first = last_string.take().expect("option must be some");
let result = first.map(|involvement| {
Some(InvolvedPeopleListItem {
involvement,
involvee: String::new(),
})
});
Some(result)
}
(None, None) => None,
})
Expand Down Expand Up @@ -1711,7 +1720,28 @@ mod tests {
data.extend(bytes_for_encoding("other involvement", *encoding).into_iter());
data.extend(delim_for_encoding(*encoding).into_iter());
// involveee missing here
assert!(decode(frame_id, version, &data[..]).is_err());

let content = frame::InvolvedPeopleList {
items: vec![
InvolvedPeopleListItem {
involvement: "involvement".to_string(),
involvee: "involvee".to_string(),
},
InvolvedPeopleListItem {
involvement: "other involvement".to_string(),
// Assume empty string if value is missing
involvee: "".to_string(),
},
],
};
assert_eq!(
*decode(frame_id, version, &data[..])
.unwrap()
.0
.involved_people_list()
.unwrap(),
content
);
}
}

Expand Down

0 comments on commit d1c18df

Please sign in to comment.