Repository navigation
Description
First of all, thank you so much for maintaining fixedbitset; while applying our testing tool across multiple crates during its evaluation, we encountered an issue that I would like to bring to your attention.
FixedBitSet::grow_inner moves the bit set's buffer into a temporary Vec and then calls resize on it. If resize panics, unwinding drops the temporary Vec and frees the buffer, but self.data still points to it. The next access is a use-after-free, and dropping the bit set frees the buffer a second time.
I notice the following code may have an unsound problem:
Lines 161 to 174 in 111fb25
| unsafe fn grow_inner(&mut self, bits: usize, fill: MaybeUninit<SimdBlock>) { | |
| // SAFETY: The data pointer and capacity were created from a Vec initially. The block | |
| // len is identical to that of the original. | |
| let mut data = unsafe { | |
| Vec::from_raw_parts(self.data.as_ptr(), self.simd_block_len(), self.capacity) | |
| }; | |
| let (mut blocks, rem) = div_rem(bits, SimdBlock::BITS); | |
| blocks += (rem > 0) as usize; | |
| data.resize(blocks, fill); | |
| let (data, capacity, _) = vec_into_parts(data); | |
| self.data = data; | |
| self.capacity = capacity; | |
| self.length = bits; | |
| } |
Describe the bug
unsafe fn grow_inner(&mut self, bits: usize, fill: MaybeUninit<SimdBlock>) {
let mut data = unsafe {
Vec::from_raw_parts(self.data.as_ptr(), self.simd_block_len(), self.capacity)
};
let (mut blocks, rem) = div_rem(bits, SimdBlock::BITS);
blocks += (rem > 0) as usize;
data.resize(blocks, fill); // may panic: `data` is dropped, `self.data` dangles
let (data, capacity, _) = vec_into_parts(data);
self.data = data;
self.capacity = capacity;
self.length = bits;
}resize panics when the allocation fails and the allocation error handler panics. The standard library allows this: the docs of std::alloc::set_alloc_error_hook say "The hook function may choose to panic or abort", and their example hook panics. They also say the default "may be changed to panicking in future versions of Rust". grow, grow_and_insert and clone_from all go through grow_inner.
Tested on the latest release, fixedbitset 0.5.7. master 111fb25 has the same grow_inner.
To Reproduce
- Install an allocation error hook that panics, as in the
set_alloc_error_hookdocs. - Call
grow(usize::MAX)insidecatch_unwind. The allocation fails and the panic unwinds out ofgrow. - Use the bit set again.
#![feature(alloc_error_hook)]
use fixedbitset::FixedBitSet;
use std::alloc::{set_alloc_error_hook, Layout};
use std::panic::{catch_unwind, AssertUnwindSafe};
fn main() {
// documented as allowed: "The hook function may choose to panic or abort"
set_alloc_error_hook(|layout: Layout| panic!("allocation of {} bytes failed", layout.size()));
let mut bs = FixedBitSet::with_capacity(1);
// the allocation fails and the panic unwinds out of `grow`
let r = catch_unwind(AssertUnwindSafe(|| bs.grow(usize::MAX)));
println!("grow panicked: {}", r.is_err());
bs.insert(0); // writes to the buffer freed during unwinding
}After a panic in grow, the bit set should still be valid, and later calls should not touch freed memory.
output (RUSTFLAGS="-Zsanitizer=address" cargo +nightly-2026-04-03 run --release --target x86_64-unknown-linux-gnu, with ASAN_OPTIONS=allocator_may_return_null=1 so the oversized request fails instead of aborting inside ASan):
grow panicked: true
==ERROR: AddressSanitizer: heap-use-after-free on address 0x10a5378e0010
READ of size 8 at 0x10a5378e0010 thread T0
#0 fixedbitset_grow_panic_dangling_buffer::main
freed by thread T0 here:
#0 free
#1 fixedbitset::FixedBitSet::grow::do_grow
Suggested fix
Leave self empty while the temporary Vec owns the buffer (src/lib.rs):
unsafe fn grow_inner(&mut self, bits: usize, fill: MaybeUninit<SimdBlock>) {
let mut data = unsafe {
Vec::from_raw_parts(self.data.as_ptr(), self.simd_block_len(), self.capacity)
};
// `data` owns the buffer now: leave `self` empty in case `resize` panics
self.data = NonNull::dangling();
self.capacity = 0;
self.length = 0;
let (mut blocks, rem) = div_rem(bits, SimdBlock::BITS);
...If resize panics, the bit set is now empty but valid, and Drop rebuilds a zero-capacity Vec, which frees nothing.
I applied this to a copy of 0.5.7. The PoC now panics cleanly at insert (insert at index 0 exceeds fixedbitset size 0) with no AddressSanitizer report.
Thank you for your time.
Activity