diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2f5c2..0be11f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fix panic in `tlsf::Heap::used`. +- Fix panic in `tlsf::Heap::free` in case the value returned from `insert_free_block_ptr` + does not conver the full memory range passed in. ## [v0.7.0] - 2026-01-03 diff --git a/src/tlsf.rs b/src/tlsf.rs index 7155aaa..57f91a0 100644 --- a/src/tlsf.rs +++ b/src/tlsf.rs @@ -70,18 +70,26 @@ impl Heap { /// This function will panic if either of the following are true: /// /// - this function is called more than ONCE. - /// - `size == 0`. + /// - `size`, after aligning start and end to `rlsf::GRANULARITY`, is smaller than `rlsf::GRANULARITY * 2`. pub unsafe fn init(&self, start_addr: usize, size: usize) { assert!(size > 0); critical_section::with(|cs| { let mut heap = self.heap.borrow_ref_mut(cs); assert!(!heap.initialized); - heap.initialized = true; let block: NonNull<[u8]> = NonNull::slice_from_raw_parts(NonNull::new_unchecked(start_addr as *mut u8), size); - heap.tlsf.insert_free_block_ptr(block); - heap.raw_block = Some(block); - heap.raw_block_size = size; + if let Some(actual_size) = heap.tlsf.insert_free_block_ptr(block) { + let block: NonNull<[u8]> = NonNull::slice_from_raw_parts( + NonNull::new_unchecked(start_addr as *mut u8), + actual_size.get(), + ); + heap.initialized = true; + heap.raw_block = Some(block); + heap.raw_block_size = size; + } + if !heap.initialized { + panic!("Allocation too small for heap"); + } }); }