Skip to content

Commit

Permalink
safety: compute pointers correctly
Browse files Browse the repository at this point in the history
Previously, we were using 'end_ptr' by computing a zero-length pointer
by offseting from the end of the haystack. But for pointer provenance
reasons, it is more correct to computer the end pointer by adding to the
start pointer.

I don't believe this is necessary for the forward case, but do it there
too "just in case" and also to make the code more similar to the reverse
case.

Closes #121
  • Loading branch information
BurntSushi committed Sep 2, 2022
1 parent 4a21319 commit 14af7d3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/byteset/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ pub fn inv_memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
let loop_size = cmp::min(LOOP_SIZE, haystack.len());
let align = USIZE_BYTES - 1;
let start_ptr = haystack.as_ptr();
let end_ptr = haystack[haystack.len()..].as_ptr();
let mut ptr = start_ptr;

unsafe {
let end_ptr = haystack.as_ptr().add(haystack.len());
let mut ptr = start_ptr;

if haystack.len() < USIZE_BYTES {
return forward_search(start_ptr, end_ptr, ptr, confirm);
}
Expand Down Expand Up @@ -67,10 +68,11 @@ pub fn inv_memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
let loop_size = cmp::min(LOOP_SIZE, haystack.len());
let align = USIZE_BYTES - 1;
let start_ptr = haystack.as_ptr();
let end_ptr = haystack[haystack.len()..].as_ptr();
let mut ptr = end_ptr;

unsafe {
let end_ptr = haystack.as_ptr().add(haystack.len());
let mut ptr = end_ptr;

if haystack.len() < USIZE_BYTES {
return reverse_search(start_ptr, end_ptr, ptr, confirm);
}
Expand Down

0 comments on commit 14af7d3

Please sign in to comment.