Skip to content

Commit 5cf5516

Browse files
committed
handle overflow/underflow in index offsets
1 parent 85919a0 commit 5cf5516

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Diff for: src/libcore/str/pattern.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,12 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
330330
// find something. When we find something the `finger` will be set
331331
// to a UTF8 boundary.
332332
self.finger += index + 1;
333-
let found_char = self.finger - self.utf8_size;
334-
if let Some(slice) = self.haystack.as_bytes().get(found_char..self.finger) {
335-
if slice == &self.utf8_encoded[0..self.utf8_size] {
336-
return Some((found_char, self.finger));
333+
if self.finger >= self.utf8_size {
334+
let found_char = self.finger - self.utf8_size;
335+
if let Some(slice) = self.haystack.as_bytes().get(found_char..self.finger) {
336+
if slice == &self.utf8_encoded[0..self.utf8_size] {
337+
return Some((found_char, self.finger));
338+
}
337339
}
338340
}
339341
} else {
@@ -386,12 +388,15 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
386388
// char in the paradigm of reverse iteration). For
387389
// multibyte chars we need to skip down by the number of more
388390
// bytes they have than ASCII
389-
let found_char = index - (self.utf8_size - 1);
390-
if let Some(slice) = haystack.get(found_char..(found_char + self.utf8_size)) {
391-
if slice == &self.utf8_encoded[0..self.utf8_size] {
392-
// move finger to before the character found (i.e. at its start index)
393-
self.finger_back = found_char;
394-
return Some((self.finger_back, self.finger_back + self.utf8_size));
391+
let shift = self.utf8_size - 1;
392+
if index >= shift {
393+
let found_char = index - shift;
394+
if let Some(slice) = haystack.get(found_char..(found_char + self.utf8_size)) {
395+
if slice == &self.utf8_encoded[0..self.utf8_size] {
396+
// move finger to before the character found (i.e. at its start index)
397+
self.finger_back = found_char;
398+
return Some((self.finger_back, self.finger_back + self.utf8_size));
399+
}
395400
}
396401
}
397402
// We can't use finger_back = index - size + 1 here. If we found the last char

0 commit comments

Comments
 (0)