Skip to content

Commit

Permalink
myers: avoid a Miri-flagged pointer invalidation (#10)
Browse files Browse the repository at this point in the history
In Myers::new, the kforward and kbackward pointers (derived from kvec)
are invalidated by a subsequent kvec.into() (into a pointer). This
can be seen by copying the relevant code into the Rust playground and
running it under Miri [1]. To fix this, make kvec a pointer from the
start.

[1] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=554abe93817c3d95a3412006e8efb022
---
This was my attempt at fixing this - feel free to rewrite to fit your
project's coding style if you like.
  • Loading branch information
jonathantanmy authored Jul 26, 2024
1 parent 50f105c commit d09118e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/myers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ impl Drop for Myers {
impl Myers {
fn new(len1: usize, len2: usize) -> Self {
let ndiags = len1 + len2 + 3;
let kvec = Box::leak(vec![0; 2 * ndiags + 2].into_boxed_slice());
let kforward = NonNull::from(&mut kvec[len2 + 1]);
let kbackward = NonNull::from(&mut kvec[ndiags + len2 + 1]);
let kvec: *mut [i32] = Box::into_raw(vec![0; 2 * ndiags + 2].into_boxed_slice()).into();
let (kforward, kbackward) = unsafe {
(
NonNull::new_unchecked((kvec as *mut i32).offset((len2 + 1) as isize)),
NonNull::new_unchecked((kvec as *mut i32).offset((ndiags + len2 + 1) as isize)),
)
};
Self {
kvec: kvec.into(),
kvec: unsafe { NonNull::new_unchecked(kvec) },
kforward,
kbackward,
max_cost: sqrt(ndiags).max(MAX_COST_MIN),
Expand Down

0 comments on commit d09118e

Please sign in to comment.