Skip to content

mm/heap: Handle zero memalign alignment - #21067

Merged
casaroli merged 1 commit into
apache:masterfrom
anchao:26102808
Oct 30, 2026
Merged

casaroli merged 1 commit into
apache:masterfrom
anchao:26102808

Conversation

@anchao

@anchao anchao commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Handle zero alignment values in mm_memalign() by treating them as a request
for the natural malloc alignment.

Previously, mm_memalign() accepted alignment == 0 because the power-of-two
check 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_ALIGN before the validation and
allocation 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:

  • prevents modulo-by-zero in the debug alignment assertion
  • preserves natural malloc alignment behavior for callers that do not require
    extra alignment
  • keeps existing rejection behavior for invalid non-power-of-two alignments
  • no API, ABI, build-system, documentation, or security impact is expected

Testing

sim/nsh

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]>
@github-actions github-actions Bot added Area: Memory Management Memory Management issues Size: XS The size of the change in this PR is very small labels Oct 28, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

qemu-armv8a

  • Code: .text.mm_memalign +12 B (+0.0%, 352,772 B)

s698pm-dkit

  • Code: .text +32 B (+0.0%, 380,672 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. mm_memalign() in mm/tlsf/mm_tlsf.c has the same defect: with CONFIG_MM_HEAP_MEMPOOL, alignment 0 goes to mempool_multiple_memalign(), where ALIGN_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.

  1. The Testing section has only "sim/nsh" with no log.

Fix: add the build and runtime logs.

@casaroli
casaroli merged commit 8773241 into apache:master Oct 30, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Memory Management Memory Management issues Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants