Skip to content

Commit

Permalink
Fix unsoundness on invalid utf-8 inputs
Browse files Browse the repository at this point in the history
Previously, unchecked user input bytes were passed into
`str::from_utf8_unchecked`, which is unsound as it was within a safe
function. It's been revised to check the user input and fail if there's
invalid input.
  • Loading branch information
lf- authored and mgattozzi committed Jun 7, 2021
1 parent 0e1ecba commit bb661f2
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ where
let mut write_buffer = SmallVec::<[u8; BUFSIZE]>::new();

// Let textwrap work its magic
let wrapped = fill(unsafe { str::from_utf8_unchecked(input) }, max_width);
let wrapped = fill(
str::from_utf8(input).map_err(|_| std::io::ErrorKind::InvalidData)?,
max_width,
);

let lines: Vec<&str> = wrapped.lines().collect();

Expand Down

0 comments on commit bb661f2

Please sign in to comment.