From 5e8e90d28f5ac6715a2568f337234088fea1fdb3 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Wed, 28 Jan 2026 11:05:04 +0000 Subject: [PATCH] Fix iter_blocks on oddly sized or unaligned blocks If len < GRANULARITY * 2, insert_free_block_ptr doesn't insert another block: ``` let len = if let Some(x) = len .checked_sub((start as usize).wrapping_sub(unaligned_start as usize)) .filter(|&x| x >= GRANULARITY * 2) { // Round down x & !(GRANULARITY - 1) } else { // The block is too small return None; }; ``` This can happen if the block being added has an odd size (not a multiple of GRANULARITY * 2) or is unaligned. Therefore, iter_blocks must also skip remaining lengths smaller than this. I think this is the root cause for a panic we observed in https://github.com/rust-embedded/embedded-alloc/issues/121 --- crates/rlsf/src/tlsf.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rlsf/src/tlsf.rs b/crates/rlsf/src/tlsf.rs index e3afdad..f16a5b5 100644 --- a/crates/rlsf/src/tlsf.rs +++ b/crates/rlsf/src/tlsf.rs @@ -1545,7 +1545,7 @@ impl<'pool, FLBitmap: BinInteger, SLBitmap: BinInteger, const FLLEN: usize, cons core::iter::from_fn(move || { let _ = &start; - if len == 0 { + if len < GRANULARITY * 2 { None } else { let block_hdr = &*(start.0 as *const BlockHdr);