]> Savannah Git Hosting - gnulib.git/commitdiff
malloca: pacify -Wcheri-provenance
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 7 Nov 2023 18:54:58 +0000 (10:54 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 7 Nov 2023 18:54:58 +0000 (10:54 -0800)
This shouldn’t affect generated code when optimizing.
* lib/malloca.c (mmalloca): Pacify -Wcheri-provenance on CHERI-64 cc.
(freea): Assign to temporaries to simplify debugging and avoid casts.

ChangeLog
lib/malloca.c

index c206ebccc9fbfb77311c089bbed1b46423738d8e..b93b28f3266b7d599164041484d69fc10d834376 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-11-07  Paul Eggert  <eggert@cs.ucla.edu>
+
+       malloca: pacify -Wcheri-provenance
+       This shouldn’t affect generated code when optimizing.
+       * lib/malloca.c (mmalloca): Pacify -Wcheri-provenance on CHERI-64 cc.
+       (freea): Assign to temporaries to simplify debugging and avoid casts.
+
 2023-11-07  Bruno Haible  <bruno@clisp.org>
 
        rawmemchr: Port to CHERI.
index f055b1e5ca873bb7938e315870aa9987d640413c..690ce2324b2119905ea82f574267b0d185bc3925 100644 (file)
@@ -60,7 +60,7 @@ mmalloca (size_t n)
           /* The ckd_add avoids signed integer overflow on
              theoretical platforms where UINTPTR_MAX <= INT_MAX.  */
           ckd_add (&umemplus, umem, sizeof (small_t) + sa_alignment_max - 1);
-          idx_t offset = ((umemplus & ~alignment2_mask)
+          idx_t offset = (umemplus - umemplus % (2 * sa_alignment_max)
                           + sa_alignment_max - umem);
           void *vp = mem + offset;
           small_t *p = vp;
@@ -90,15 +90,18 @@ void
 freea (void *p)
 {
   /* Check argument.  */
-  if ((uintptr_t) p & (sa_alignment_max - 1))
+  uintptr_t u = (uintptr_t) p;
+  if (u & (sa_alignment_max - 1))
     {
       /* p was not the result of a malloca() call.  Invalid argument.  */
       abort ();
     }
   /* Determine whether p was a non-NULL pointer returned by mmalloca().  */
-  if ((uintptr_t) p & sa_alignment_max)
+  if (u & sa_alignment_max)
     {
-      void *mem = (char *) p - ((small_t *) p)[-1];
+      char *cp = p;
+      small_t *sp = p;
+      void *mem = cp - sp[-1];
       free (mem);
     }
 }