[08/20] malloc: Use uintptr_t for pointer alignment

Message ID afb365933de06282be6eb3049b0271b24536eeff.1666877952.git.szabolcs.nagy@arm.com
State Committed
Commit d08dec4ea7c9f8b1d734cc1ba80c7f70df2f2c25
Headers
Series patches from the morello port |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Szabolcs Nagy Oct. 27, 2022, 3:32 p.m. UTC
  From: Carlos Eduardo Seo <carlos.seo@arm.com>

Avoid integer casts that assume unsigned long can represent pointers.
---
 malloc/arena.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Adhemerval Zanella Netto Oct. 31, 2022, 4:09 p.m. UTC | #1
On 27/10/22 12:32, Szabolcs Nagy via Libc-alpha wrote:
> From: Carlos Eduardo Seo <carlos.seo@arm.com>
> 
> Avoid integer casts that assume unsigned long can represent pointers.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>


> ---
>  malloc/arena.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/malloc/arena.c b/malloc/arena.c
> index 074ecbc09f..f381f18371 100644
> --- a/malloc/arena.c
> +++ b/malloc/arena.c
> @@ -439,7 +439,7 @@ dump_heap (heap_info *heap)
>    fprintf (stderr, "Heap %p, size %10lx:\n", heap, (long) heap->size);
>    ptr = (heap->ar_ptr != (mstate) (heap + 1)) ?
>          (char *) (heap + 1) : (char *) (heap + 1) + sizeof (struct malloc_state);
> -  p = (mchunkptr) (((unsigned long) ptr + MALLOC_ALIGN_MASK) &
> +  p = (mchunkptr) (((uintptr_t) ptr + MALLOC_ALIGN_MASK) &
>                     ~MALLOC_ALIGN_MASK);
>    for (;; )
>      {
> @@ -513,7 +513,7 @@ alloc_new_heap  (size_t size, size_t top_pad, size_t pagesize,
>        p1 = (char *) MMAP (0, max_size << 1, PROT_NONE, mmap_flags);
>        if (p1 != MAP_FAILED)
>          {
> -          p2 = (char *) (((unsigned long) p1 + (max_size - 1))
> +          p2 = (char *) (((uintptr_t) p1 + (max_size - 1))
>                           & ~(max_size - 1));
>            ul = p2 - p1;
>            if (ul)
> @@ -752,7 +752,7 @@ _int_new_arena (size_t size)
>  
>    /* Set up the top chunk, with proper alignment. */
>    ptr = (char *) (a + 1);
> -  misalign = (unsigned long) chunk2mem (ptr) & MALLOC_ALIGN_MASK;
> +  misalign = (uintptr_t) chunk2mem (ptr) & MALLOC_ALIGN_MASK;
>    if (misalign > 0)
>      ptr += MALLOC_ALIGNMENT - misalign;
>    top (a) = (mchunkptr) ptr;
  

Patch

diff --git a/malloc/arena.c b/malloc/arena.c
index 074ecbc09f..f381f18371 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -439,7 +439,7 @@  dump_heap (heap_info *heap)
   fprintf (stderr, "Heap %p, size %10lx:\n", heap, (long) heap->size);
   ptr = (heap->ar_ptr != (mstate) (heap + 1)) ?
         (char *) (heap + 1) : (char *) (heap + 1) + sizeof (struct malloc_state);
-  p = (mchunkptr) (((unsigned long) ptr + MALLOC_ALIGN_MASK) &
+  p = (mchunkptr) (((uintptr_t) ptr + MALLOC_ALIGN_MASK) &
                    ~MALLOC_ALIGN_MASK);
   for (;; )
     {
@@ -513,7 +513,7 @@  alloc_new_heap  (size_t size, size_t top_pad, size_t pagesize,
       p1 = (char *) MMAP (0, max_size << 1, PROT_NONE, mmap_flags);
       if (p1 != MAP_FAILED)
         {
-          p2 = (char *) (((unsigned long) p1 + (max_size - 1))
+          p2 = (char *) (((uintptr_t) p1 + (max_size - 1))
                          & ~(max_size - 1));
           ul = p2 - p1;
           if (ul)
@@ -752,7 +752,7 @@  _int_new_arena (size_t size)
 
   /* Set up the top chunk, with proper alignment. */
   ptr = (char *) (a + 1);
-  misalign = (unsigned long) chunk2mem (ptr) & MALLOC_ALIGN_MASK;
+  misalign = (uintptr_t) chunk2mem (ptr) & MALLOC_ALIGN_MASK;
   if (misalign > 0)
     ptr += MALLOC_ALIGNMENT - misalign;
   top (a) = (mchunkptr) ptr;