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(stream)!: Prevent slicing on non-UTF8 boundaries #471

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
I: FindSlice<(u8, u8)>,
I: FindSlice<(char, char)>,
<I as Stream>::Token: AsChar + Clone,
{
trace("till_line_ending", move |input: &mut I| {
Expand All @@ -152,10 +152,10 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
I: FindSlice<(u8, u8)>,
I: FindSlice<(char, char)>,
<I as Stream>::Token: AsChar + Clone,
{
let res = match take_until::<_, _, ()>(0.., (b'\r', b'\n')).parse_next(input) {
let res = match take_until::<_, _, ()>(0.., ('\r', '\n')).parse_next(input) {
Ok(slice) => slice,
Err(ErrMode::Incomplete(err)) => {
return Err(ErrMode::Incomplete(err));
Expand Down
122 changes: 47 additions & 75 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,20 +2194,6 @@
}
}

impl<'a> Compare<u8> for &'a str {
#[inline(always)]
fn compare(&self, t: u8) -> CompareResult {
self.as_bytes().compare(t)
}
}

impl<'a> Compare<AsciiCaseless<u8>> for &'a str {
#[inline(always)]
fn compare(&self, t: AsciiCaseless<u8>) -> CompareResult {
self.as_bytes().compare(t)
}
}

impl<'a> Compare<char> for &'a str {
#[inline(always)]
fn compare(&self, t: char) -> CompareResult {
Expand All @@ -2222,20 +2208,6 @@
}
}

impl<'a, 'b> Compare<&'b [u8]> for &'a str {
#[inline(always)]
fn compare(&self, t: &'b [u8]) -> CompareResult {
self.as_bytes().compare(t)
}
}

impl<'a, 'b> Compare<AsciiCaseless<&'b [u8]>> for &'a str {
#[inline(always)]
fn compare(&self, t: AsciiCaseless<&'b [u8]>) -> CompareResult {
self.as_bytes().compare(t)
}
}

impl<'a, T> Compare<T> for &'a Bytes
where
&'a [u8]: Compare<T>,
Expand Down Expand Up @@ -2340,6 +2312,48 @@
}
}

impl<'i> FindSlice<char> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: char) -> Option<crate::lib::std::ops::Range<usize>> {
let mut b = [0; 4];
let substr = substr.encode_utf8(&mut b);
self.find_slice(&*substr)

Check warning on line 2320 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2317-L2320

Added lines #L2317 - L2320 were not covered by tests
}
}

impl<'i> FindSlice<(char,)> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: (char,)) -> Option<crate::lib::std::ops::Range<usize>> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
self.find_slice((&*substr0,))

Check warning on line 2329 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2326-L2329

Added lines #L2326 - L2329 were not covered by tests
}
}

impl<'i> FindSlice<(char, char)> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: (char, char)) -> Option<crate::lib::std::ops::Range<usize>> {

Check warning on line 2335 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2335

Added line #L2335 was not covered by tests
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1))
}
}

impl<'i> FindSlice<(char, char, char)> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: (char, char, char)) -> Option<crate::lib::std::ops::Range<usize>> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
let mut b = [0; 4];
let substr2 = substr.2.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1, &*substr2))

Check warning on line 2353 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2346-L2353

Added lines #L2346 - L2353 were not covered by tests
}
}

impl<'i> FindSlice<u8> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: u8) -> Option<crate::lib::std::ops::Range<usize>> {
Expand Down Expand Up @@ -2409,7 +2423,7 @@
impl<'i, 's> FindSlice<&'s str> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: &'s str) -> Option<crate::lib::std::ops::Range<usize>> {
self.as_bytes().find_slice(substr.as_bytes())
self.as_bytes().find_slice(substr)
}
}

Expand Down Expand Up @@ -2440,70 +2454,28 @@
impl<'i> FindSlice<char> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: char) -> Option<crate::lib::std::ops::Range<usize>> {
let mut b = [0; 4];
let substr = substr.encode_utf8(&mut b);
self.find_slice(&*substr)
self.as_bytes().find_slice(substr)

Check warning on line 2457 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2457

Added line #L2457 was not covered by tests
}
}

impl<'i> FindSlice<(char,)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (char,)) -> Option<crate::lib::std::ops::Range<usize>> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
self.find_slice((&*substr0,))
self.as_bytes().find_slice(substr)

Check warning on line 2464 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2464

Added line #L2464 was not covered by tests
}
}

impl<'i> FindSlice<(char, char)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (char, char)) -> Option<crate::lib::std::ops::Range<usize>> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1))
self.as_bytes().find_slice(substr)
}
}

impl<'i> FindSlice<(char, char, char)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (char, char, char)) -> Option<crate::lib::std::ops::Range<usize>> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
let mut b = [0; 4];
let substr2 = substr.2.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1, &*substr2))
}
}

impl<'i> FindSlice<u8> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: u8) -> Option<crate::lib::std::ops::Range<usize>> {
self.find_slice(substr.as_char())
}
}

impl<'i> FindSlice<(u8,)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (u8,)) -> Option<crate::lib::std::ops::Range<usize>> {
self.find_slice((substr.0.as_char(),))
}
}

impl<'i> FindSlice<(u8, u8)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (u8, u8)) -> Option<crate::lib::std::ops::Range<usize>> {
self.find_slice((substr.0.as_char(), substr.1.as_char()))
}
}

impl<'i> FindSlice<(u8, u8, u8)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (u8, u8, u8)) -> Option<crate::lib::std::ops::Range<usize>> {
self.find_slice((substr.0.as_char(), substr.1.as_char(), substr.2.as_char()))
self.as_bytes().find_slice(substr)

Check warning on line 2478 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2478

Added line #L2478 was not covered by tests
}
}

Expand Down
Loading