This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 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 256b4ff5765 mm/mm_gran: Check the alignment given to 
gran_alloc_align().
256b4ff5765 is described below

commit 256b4ff5765b86ebc5858be6499382ca489e4bf8
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Mon Nov 16 17:05:50 2026 -0300

    mm/mm_gran: Check the alignment given to gran_alloc_align().
    
    gran_alloc_align() rounds the start granule up with a mask, which
    only works when the alignment is a power of two:
    
    * An alignment of zero gave a mask of zero, so the allocation was
      placed at granule 0 even when granule 0 was in use, and the same
      memory was returned twice.  A request of one granule returned NULL.
    * An alignment that is not a power of two, for example three
      granules, returned memory that was not aligned.
    
    Check the alignment as mm_memalign() does: zero means no alignment,
    and an alignment that is not a power of two returns NULL.
    
    Assisted-by: Claude Code:claude-opus-5-5
---
 include/nuttx/mm/gran.h   |  2 +-
 include/nuttx/pgalloc.h   |  3 ++-
 mm/mm_gran/mm_granalloc.c | 10 +++++++++-
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/include/nuttx/mm/gran.h b/include/nuttx/mm/gran.h
index 1cc8a894770..aeeb7512127 100644
--- a/include/nuttx/mm/gran.h
+++ b/include/nuttx/mm/gran.h
@@ -210,7 +210,7 @@ FAR void *gran_alloc(GRAN_HANDLE handle, size_t size);
  * Input Parameters:
  *   handle - The handle previously returned by gran_initialize
  *   size   - The size of the memory region to allocate.
- *   align  - The size of the memory region to align.
+ *   align  - The alignment in bytes: a power of two, or zero for none.
  *
  * Returned Value:
  *   On success, a non-NULL pointer to the allocated memory is returned;
diff --git a/include/nuttx/pgalloc.h b/include/nuttx/pgalloc.h
index f8ebced4e0c..e3363a117e8 100644
--- a/include/nuttx/pgalloc.h
+++ b/include/nuttx/pgalloc.h
@@ -177,7 +177,8 @@ uintptr_t mm_pgalloc(unsigned int npages);
  *
  * Input Parameters:
  *   npages - The number of pages to allocate, each of size CONFIG_MM_PGSIZE.
- *   align  - The number of pages to align, each of size CONFIG_MM_PGSIZE.
+ *   align  - The alignment in pages, each of size CONFIG_MM_PGSIZE: a
+ *            power of two, or zero for none.
  *
  * Returned Value:
  *   On success, a non-zero, physical address of the allocated page memory
diff --git a/mm/mm_gran/mm_granalloc.c b/mm/mm_gran/mm_granalloc.c
index 1236851d987..0e356f67244 100644
--- a/mm/mm_gran/mm_granalloc.c
+++ b/mm/mm_gran/mm_granalloc.c
@@ -92,7 +92,15 @@ FAR void *gran_alloc_align(GRAN_HANDLE handle, size_t size, 
size_t align)
   int ret;
 
   DEBUGASSERT(gran);
-  nalign = NGRANULE(gran, align);
+
+  /* As in mm_memalign(): a power of two, or zero for no alignment */
+
+  if ((align & (align - 1)) != 0)
+    {
+      return NULL;
+    }
+
+  nalign = align ? NGRANULE(gran, align) : 1;
   ngran = NGRANULE(gran, size);
 
   naligned_up = ngran + (nalign - 1);

Reply via email to