Skip to content

Commit 317fbb6

Browse files
committed
OGG: Stop silently discarding fields that are too large
Previously when writing `VorbisComments`, any field larger than `u32::MAX` would just be ignored. Now `TooMuchData` is thrown.
1 parent 5ec7063 commit 317fbb6

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/ogg/write.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ pub(crate) fn create_comments(
7070
}
7171

7272
let comment = format!("{k}={v}");
73+
let comment_bytes = comment.as_bytes();
7374

74-
let comment_b = comment.as_bytes();
75-
let bytes_len = comment_b.len();
75+
let Ok(bytes_len) = u32::try_from(comment_bytes.len()) else {
76+
err!(TooMuchData);
77+
};
7678

77-
if u32::try_from(bytes_len as u64).is_ok() {
78-
*count += 1;
79+
*count += 1;
7980

80-
packet.write_all(&(bytes_len as u32).to_le_bytes())?;
81-
packet.write_all(comment_b)?;
82-
}
81+
packet.write_u32::<LittleEndian>(bytes_len)?;
82+
packet.write_all(comment_bytes)?;
8383
}
8484

8585
Ok(())
@@ -88,22 +88,22 @@ pub(crate) fn create_comments(
8888
fn create_pictures(
8989
packet: &mut impl Write,
9090
count: &mut u32,
91-
pictures: &mut dyn Iterator<Item = (Picture, PictureInformation)>,
91+
pictures: &mut dyn Iterator<Item = (&Picture, PictureInformation)>,
9292
) -> Result<()> {
9393
const PICTURE_KEY: &str = "METADATA_BLOCK_PICTURE=";
9494

9595
for (pic, info) in pictures {
9696
let picture = pic.as_flac_bytes(info, true);
9797

98-
let bytes_len = picture.len() + PICTURE_KEY.len();
98+
let Ok(bytes_len) = u32::try_from(picture.len() + PICTURE_KEY.len()) else {
99+
err!(TooMuchData);
100+
};
99101

100-
if u32::try_from(bytes_len as u64).is_ok() {
101-
*count += 1;
102+
*count += 1;
102103

103-
packet.write_u32::<LittleEndian>(bytes_len as u32)?;
104-
packet.write_all(PICTURE_KEY.as_bytes())?;
105-
packet.write_all(&picture)?;
106-
}
104+
packet.write_u32::<LittleEndian>(bytes_len)?;
105+
packet.write_all(PICTURE_KEY.as_bytes())?;
106+
packet.write_all(&picture)?;
107107
}
108108

109109
Ok(())
@@ -189,8 +189,6 @@ where
189189
II: Iterator<Item = (&'a str, &'a str)>,
190190
IP: Iterator<Item = (&'a Picture, PictureInformation)>,
191191
{
192-
const PICTURE_KEY: &str = "METADATA_BLOCK_PICTURE=";
193-
194192
let mut new_comment_packet = Cursor::new(Vec::new());
195193

196194
new_comment_packet.write_all(comment_signature)?;

0 commit comments

Comments
 (0)