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

make take_while function unsafe #12

Closed
wants to merge 1 commit into from

Conversation

kitcatier
Copy link

@kitcatier kitcatier commented Mar 17, 2023

Hello, I found a soundness issue in this crate.

pub fn take_while<F>(input: &[u8], mut condition: F) -> Res<&str>
where
F: FnMut(u8) -> bool,
{
for i in 0..input.len() {
unsafe {
if !condition(*input.get_unchecked(i)) {
return Ok((
input.get_unchecked(i..),
std::str::from_utf8_unchecked(input.get_unchecked(..i)),
));
}
}
}
Ok((&[], unsafe { std::str::from_utf8_unchecked(input) }))
}

The unsafe function called needs to ensure that the parameter must be:

  • The bytes passed in must be valid UTF-8.
    https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html
    When the parameter condition panics, the input will be saved as non-utf8, which may cause undefined behavior in multi-threaded programs. The developer who calls the take_while function may not notice this safety requirement.
    Marking them unsafe also means that callers must make sure they know what they're doing.

@Mubelotix
Copy link
Owner

Mubelotix commented Apr 29, 2023

Hi, thank you for your input. It is known there are several soundness issues like that one in this crate. I'm also suspecting some safety issues. This is due to me being a beginner when I started it and wanting to prioritize performance over everything else. I have been working on a v2 on another branch that will fix everything.

@Mubelotix
Copy link
Owner

Closing in favor of #13

@Mubelotix Mubelotix closed this Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants