Consider this example:
fn main() { let mut arr = [[Box::new(42)]]; let alias = &mut arr[0][{ let [row] = arr; drop(row); 0 }]; println!("{}", **alias); // use-after-free of arr's dead stack slot } To conveniently apply the NLL algorithm manually to check whether the algorithm can analyze the problem in this code, I will desugar the above code to the following:
let mut arr = [[Box::new(42)]]; let index= { let [row] = arr; // #0 drop(row); 0 }; let alias = arr.index_mut(0).index_mut(index); // #1 println!("{}", **alias); According to NLL
An assignment statement LV = RV is a shallow write to LV;
and, within the rvalue RV:
- Each lvalue operand is either a deep read or a deep write action, depending on whether or not the type of the lvalue implements Copy.
In this case, #0 is counted as a deep write to arr. At #1, the calling method on arr creates a mutable borrowing, that is, a loan ('a,mut, arr).
When we compute the relevant borrows for the action at #1, there are no relevant borrows because the move of arr at #0 doesn't produce loans.
It appears to me that the algorithm cannot catch the error. Is my understanding right?
2 posts - 2 participants
