Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow invalid utf8 when decoding modules #483

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/text_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub fn convert_to_utf8<'a>(
charset: &'_ str,
) -> Result<Cow<'a, str>, std::io::Error> {
match encoding_rs::Encoding::for_label(charset.as_bytes()) {
Some(encoding) => encoding
.decode_without_bom_handling_and_without_replacement(bytes)
.ok_or_else(|| std::io::ErrorKind::InvalidData.into()),
Some(encoding) => Ok(encoding.decode_without_bom_handling(bytes).0),
None => Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("Unsupported charset: {charset}"),
Expand Down Expand Up @@ -109,8 +107,6 @@ mod test {
fn test_decoding_invalid_utf8() {
let test_data = b"\xFE\xFE\xFF\xFF".to_vec();
let result = convert_to_utf8(&test_data, "utf-8");
assert!(result.is_err());
let err = result.expect_err("Err expected");
assert!(err.kind() == ErrorKind::InvalidData);
assert!(result.is_ok());
}
}