diff --git a/src/dense_byte_node.rs b/src/dense_byte_node.rs index 4c4246f4..b82ce57f 100644 --- a/src/dense_byte_node.rs +++ b/src/dense_byte_node.rs @@ -6,7 +6,7 @@ use std::hint::unreachable_unchecked; use crate::alloc::Allocator; use crate::ring::*; -use crate::utils::ByteMask; +use crate::utils::{AbortOnUnwind, ByteMask}; use crate::utils::BitMask; use crate::trie_node::*; @@ -2215,6 +2215,10 @@ impl, Other let mut r = 0; let mut c = 0; + // The loop moves entries before repairing vector lengths. Do not let a + // panic unwind into vectors that still claim ownership of moved entries. + let abort_on_unwind = AbortOnUnwind; + for i in 0..4 { let mut lm = jm.0[i]; while lm != 0 { @@ -2250,6 +2254,7 @@ impl, Other unsafe { self.values.set_len(0) } unsafe { other.values.set_len(0) } unsafe { v.v.set_len(c) } + core::mem::forget(abort_on_unwind); self.mask = jm; self.values = v.v; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 1991cdc5..d5d671f8 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -7,6 +7,17 @@ pub mod ints; pub mod debug; +/// Aborts if dropped, preventing unwinding through a temporarily invalid state. +/// Forget this guard once the protected invariants have been restored. +pub(crate) struct AbortOnUnwind; + +impl Drop for AbortOnUnwind { + #[cold] + fn drop(&mut self) { + std::process::abort(); + } +} + /// Use `fast_slice_utils` directly. We don't want to maintain this re-export from pathmap //GOAT, remove this re-export when nothing downstream is going to break #[deprecated] @@ -1233,4 +1244,4 @@ mod tests { core::assert_eq!(mask_byte,range_byte); } } -} \ No newline at end of file +}