Skip to content

Commit

Permalink
handle overflow/underflow in index offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jan 1, 2018
1 parent 85919a0 commit 5cf5516
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,12 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
// find something. When we find something the `finger` will be set
// to a UTF8 boundary.
self.finger += index + 1;
let found_char = self.finger - self.utf8_size;
if let Some(slice) = self.haystack.as_bytes().get(found_char..self.finger) {
if slice == &self.utf8_encoded[0..self.utf8_size] {
return Some((found_char, self.finger));
if self.finger >= self.utf8_size {
let found_char = self.finger - self.utf8_size;
if let Some(slice) = self.haystack.as_bytes().get(found_char..self.finger) {
if slice == &self.utf8_encoded[0..self.utf8_size] {
return Some((found_char, self.finger));
}
}
}
} else {
Expand Down Expand Up @@ -386,12 +388,15 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
// char in the paradigm of reverse iteration). For
// multibyte chars we need to skip down by the number of more
// bytes they have than ASCII
let found_char = index - (self.utf8_size - 1);
if let Some(slice) = haystack.get(found_char..(found_char + self.utf8_size)) {
if slice == &self.utf8_encoded[0..self.utf8_size] {
// move finger to before the character found (i.e. at its start index)
self.finger_back = found_char;
return Some((self.finger_back, self.finger_back + self.utf8_size));
let shift = self.utf8_size - 1;
if index >= shift {
let found_char = index - shift;
if let Some(slice) = haystack.get(found_char..(found_char + self.utf8_size)) {
if slice == &self.utf8_encoded[0..self.utf8_size] {
// move finger to before the character found (i.e. at its start index)
self.finger_back = found_char;
return Some((self.finger_back, self.finger_back + self.utf8_size));
}
}
}
// We can't use finger_back = index - size + 1 here. If we found the last char
Expand Down

0 comments on commit 5cf5516

Please sign in to comment.