Repository navigation
mm/heap: Handle zero memalign alignment - #21067
Merged
Merged
Conversation
mm_memalign() accepted a zero alignment because the power-of-two check also passes for zero. That could trip the debug alignment assertion with a modulo by zero. Treat zero as a request for the natural malloc alignment so internal callers that do not require extra alignment keep the existing allocation semantics. Signed-off-by: chao an <[email protected]>
xiaoxiang781216
approved these changes
Oct 28, 2026
|
casaroli
requested changes
Oct 28, 2026
casaroli
left a comment
Contributor
There was a problem hiding this comment.
mm_memalign()in mm/tlsf/mm_tlsf.c has the same defect: with CONFIG_MM_HEAP_MEMPOOL, alignment 0 goes tomempool_multiple_memalign(), whereALIGN_UP(blk, 0)divides by zero (include/nuttx/nuttx.h:51). The ELF loader can pass 0 (libs/libc/elf/elf_load.c:105,sh_addralign).
Fix: add the same if (alignment == 0) alignment = MM_ALIGN; check at the top of mm_memalign() in mm/tlsf/mm_tlsf.c.
- The Testing section has only "sim/nsh" with no log.
Fix: add the build and runtime logs.
jerpelea
approved these changes
Oct 28, 2026
acassis
approved these changes
Oct 30, 2026
casaroli
approved these changes
Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Handle zero alignment values in
mm_memalign()by treating them as a requestfor the natural malloc alignment.
Previously,
mm_memalign()acceptedalignment == 0because the power-of-twocheck also passed for zero. That could later hit the natural-alignment fast path
and trigger the debug alignment assertion with a modulo-by-zero operation.
This change normalizes zero alignment to
MM_ALIGNbefore the validation andallocation logic. Internal callers that pass zero when no extra alignment is
required keep the same allocation semantics as a regular naturally aligned
malloc allocation.
Impact
This fixes a zero-alignment edge case in heap allocation.
Expected impact:
extra alignment
Testing
sim/nsh