This is an automated email from the ASF dual-hosted git repository.
casaroli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 8773241554a mm/heap: Handle zero memalign alignment
8773241554a is described below
commit 8773241554a3d699eb67c1d54565b9d621d90c2c
Author: chao an <[email protected]>
AuthorDate: Wed Oct 28 14:58:27 2026 +0800
mm/heap: Handle zero memalign alignment
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]>
---
mm/mm_heap/mm_memalign.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/mm/mm_heap/mm_memalign.c b/mm/mm_heap/mm_memalign.c
index fecb9b742ed..99fb74f70ea 100644
--- a/mm/mm_heap/mm_memalign.c
+++ b/mm/mm_heap/mm_memalign.c
@@ -62,6 +62,15 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t
alignment,
size_t allocsize;
size_t newsize;
+ /* Treat zero as a request for the natural malloc alignment. Some
+ * internal callers pass zero when no extra alignment is required.
+ */
+
+ if (alignment == 0)
+ {
+ alignment = MM_ALIGN;
+ }
+
/* Make sure that alignment is less than half max size_t */
if (alignment >= (SIZE_MAX / 2))
@@ -94,6 +103,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t
alignment,
if (alignment <= MM_ALIGN)
{
FAR void *ptr = mm_malloc(heap, size);
+
DEBUGASSERT(ptr == NULL || ((uintptr_t)ptr) % alignment == 0);
return ptr;
}