Skip to content

Commit 5ec7063

Browse files
committed
OGG: Fix reading error handling
1 parent 273451e commit 5ec7063

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/ogg/read.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777
len -= u64::from(comment_len);
7878

7979
// KEY=VALUE
80-
let mut comment_split = comment_bytes.splitn(2, b'=');
80+
let mut comment_split = comment_bytes.splitn(2, |b| *b == b'=');
8181

8282
let key = match comment_split.next() {
8383
Some(k) => k,
@@ -92,32 +92,34 @@ where
9292

9393
match key {
9494
k if k.eq_ignore_ascii_case(b"METADATA_BLOCK_PICTURE") => {
95-
let Ok(picture) = Picture::from_flac_bytes(value, true) else {
96-
if parse_mode == ParsingMode::Strict {
97-
return Err(e);
98-
}
99-
100-
log::warn!("Failed to decode FLAC picture, discarding field");
101-
continue;
102-
};
103-
104-
tag.pictures.push(picture)
95+
match Picture::from_flac_bytes(value, true) {
96+
Ok(picture) => tag.pictures.push(picture),
97+
Err(e) => {
98+
if parse_mode == ParsingMode::Strict {
99+
return Err(e);
100+
}
101+
102+
log::warn!("Failed to decode FLAC picture, discarding field");
103+
continue;
104+
},
105+
}
105106
},
106107
// The valid range is 0x20..=0x7D not including 0x3D
107-
k if k.iter().all(|c| (b' '..=b'}').contains(c) && c != b'=') => {
108+
k if k.iter().all(|c| (b' '..=b'}').contains(c) && *c != b'=') => {
108109
// SAFETY: We just verified that all of the bytes fall within the subset of ASCII
109110
let key = unsafe { String::from_utf8_unchecked(k.to_vec()) };
110111

111-
let Ok(value) = String::from_utf8(value.to_vec()) else {
112-
if parse_mode == ParsingMode::Strict {
113-
decode_err!(@BAIL "OGG: Vorbis comments contain a non UTF-8 field value");
114-
}
115-
116-
log::warn!("Non UTF-8 value found, discarding field");
117-
continue;
118-
};
119-
120-
tag.items.push((key, value))
112+
match String::from_utf8(value.to_vec()) {
113+
Ok(value) => tag.items.push((key, value)),
114+
Err(e) => {
115+
if parse_mode == ParsingMode::Strict {
116+
return Err(LoftyError::new(ErrorKind::StringFromUtf8(e)));
117+
}
118+
119+
log::warn!("Non UTF-8 value found, discarding field");
120+
continue;
121+
},
122+
}
121123
},
122124
_ => {
123125
parse_mode_choice!(

0 commit comments

Comments
 (0)