]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Use uintptr_t instead of performing pointer subtraction with a null pointer
authorQihao Chencao <twose@qq.com>
Tue, 28 Jun 2022 08:57:55 +0000 (16:57 +0800)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 17 Feb 2023 20:07:44 +0000 (17:07 -0300)
Signed-off-by: Qihao Chencao <twose@qq.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
12 files changed:
crypt/md5-crypt.c
crypt/sha256-crypt.c
crypt/sha512-crypt.c
elf/dl-minimal-malloc.c
misc/regexp.c
nscd/nscd_getgr_r.c
nscd/nscd_gethst_r.c
nscd/nscd_getserv_r.c
nss/nss_files/files-alias.c
nss/nss_files/files-parse.c
stdlib/msort.c
sysdeps/unix/sysv/linux/dl-sysdep.c

index 791a59767dede418916b670aef53ab1abd7d51b9..0cc597a7f2948bb76937a90ac24d5106a2386eeb 100644 (file)
@@ -110,7 +110,7 @@ __md5_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
   salt_len = MIN (strcspn (salt, "$"), 8);
   key_len = strlen (key);
 
-  if ((key - (char *) 0) % __alignof__ (md5_uint32) != 0)
+  if (((uintptr_t) key) % __alignof__ (md5_uint32) != 0)
     {
       char *tmp;
 
@@ -125,19 +125,19 @@ __md5_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
 
       key = copied_key =
        memcpy (tmp + __alignof__ (md5_uint32)
-               - (tmp - (char *) 0) % __alignof__ (md5_uint32),
+               - ((uintptr_t) tmp) % __alignof__ (md5_uint32),
                key, key_len);
-      assert ((key - (char *) 0) % __alignof__ (md5_uint32) == 0);
+      assert (((uintptr_t) key) % __alignof__ (md5_uint32) == 0);
     }
 
-  if ((salt - (char *) 0) % __alignof__ (md5_uint32) != 0)
+  if (((uintptr_t) salt) % __alignof__ (md5_uint32) != 0)
     {
       char *tmp = (char *) alloca (salt_len + __alignof__ (md5_uint32));
       salt = copied_salt =
        memcpy (tmp + __alignof__ (md5_uint32)
-               - (tmp - (char *) 0) % __alignof__ (md5_uint32),
+               - ((uintptr_t) tmp) % __alignof__ (md5_uint32),
                salt, salt_len);
-      assert ((salt - (char *) 0) % __alignof__ (md5_uint32) == 0);
+      assert (((uintptr_t) salt) % __alignof__ (md5_uint32) == 0);
     }
 
 #ifdef USE_NSS
index 2a2b356d9a17c8c20384dfcec2b460cd8449ea61..1ef16af80f9a1d2469311f971a1323342b15e310 100644 (file)
@@ -142,7 +142,7 @@ __sha256_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
   salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
   key_len = strlen (key);
 
-  if ((key - (char *) 0) % __alignof__ (uint32_t) != 0)
+  if (((uintptr_t) key) % __alignof__ (uint32_t) != 0)
     {
       char *tmp;
 
@@ -157,20 +157,20 @@ __sha256_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
 
       key = copied_key =
        memcpy (tmp + __alignof__ (uint32_t)
-               - (tmp - (char *) 0) % __alignof__ (uint32_t),
+               - ((uintptr_t) tmp) % __alignof__ (uint32_t),
                key, key_len);
-      assert ((key - (char *) 0) % __alignof__ (uint32_t) == 0);
+      assert (((uintptr_t) key) % __alignof__ (uint32_t) == 0);
     }
 
-  if ((salt - (char *) 0) % __alignof__ (uint32_t) != 0)
+  if (((uintptr_t) salt) % __alignof__ (uint32_t) != 0)
     {
       char *tmp = (char *) alloca (salt_len + __alignof__ (uint32_t));
       alloca_used += salt_len + __alignof__ (uint32_t);
       salt = copied_salt =
        memcpy (tmp + __alignof__ (uint32_t)
-               - (tmp - (char *) 0) % __alignof__ (uint32_t),
+               - ((uintptr_t) tmp) % __alignof__ (uint32_t),
                salt, salt_len);
-      assert ((salt - (char *) 0) % __alignof__ (uint32_t) == 0);
+      assert (((uintptr_t) salt) % __alignof__ (uint32_t) == 0);
     }
 
 #ifdef USE_NSS
index 9deb31ce99fbdc58e31a47b0788e2d58a8c8966b..b592eb0976c40237b74f44053508e7ee2c444ce1 100644 (file)
@@ -142,7 +142,7 @@ __sha512_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
   salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
   key_len = strlen (key);
 
-  if ((key - (char *) 0) % __alignof__ (uint64_t) != 0)
+  if (((uintptr_t) key) % __alignof__ (uint64_t) != 0)
     {
       char *tmp;
 
@@ -157,19 +157,19 @@ __sha512_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
 
       key = copied_key =
        memcpy (tmp + __alignof__ (uint64_t)
-               - (tmp - (char *) 0) % __alignof__ (uint64_t),
+               - ((uintptr_t) tmp) % __alignof__ (uint64_t),
                key, key_len);
-      assert ((key - (char *) 0) % __alignof__ (uint64_t) == 0);
+      assert (((uintptr_t) key) % __alignof__ (uint64_t) == 0);
     }
 
-  if ((salt - (char *) 0) % __alignof__ (uint64_t) != 0)
+  if (((uintptr_t) salt) % __alignof__ (uint64_t) != 0)
     {
       char *tmp = (char *) alloca (salt_len + __alignof__ (uint64_t));
       salt = copied_salt =
        memcpy (tmp + __alignof__ (uint64_t)
-               - (tmp - (char *) 0) % __alignof__ (uint64_t),
+               - ((uintptr_t) tmp) % __alignof__ (uint64_t),
                salt, salt_len);
-      assert ((salt - (char *) 0) % __alignof__ (uint64_t) == 0);
+      assert (((uintptr_t) salt) % __alignof__ (uint64_t) == 0);
     }
 
 #ifdef USE_NSS
index 2ef8ad34b863003f5f52f3aeaab3d4e1f72ff8fc..27549645d0d8ae49426ac01a206d778bcea0e20e 100644 (file)
@@ -38,13 +38,13 @@ __minimal_malloc (size_t n)
       /* Consume any unused space in the last page of our data segment.  */
       extern int _end attribute_hidden;
       alloc_ptr = &_end;
-      alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
+      alloc_end = (void *) 0 + ((((uintptr_t) alloc_ptr)
                                 + GLRO(dl_pagesize) - 1)
                                & ~(GLRO(dl_pagesize) - 1));
     }
 
   /* Make sure the allocation pointer is ideally aligned.  */
-  alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + MALLOC_ALIGNMENT - 1)
+  alloc_ptr = (void *) 0 + ((((uintptr_t) alloc_ptr) + MALLOC_ALIGNMENT - 1)
                            & ~(MALLOC_ALIGNMENT - 1));
 
   if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
index 5ffdfb35e6f1c2a32ba44cd7f32f3a12d3bb6c55..85ff41e909bbdb8ae96d46a2dafff364d75d2605 100644 (file)
@@ -23,6 +23,7 @@
    argument to 'step' and 'advance' was defined only in regexp.h,
    as its definition depended on macros defined by the user.  */
 
+#include <stdint.h>
 #include <regex.h>
 #include <shlib-compat.h>
 
@@ -50,7 +51,7 @@ step (const char *string, const char *expbuf)
   regmatch_t match;    /* We only need info about the full match.  */
 
   expbuf += __alignof (regex_t *);
-  expbuf -= (expbuf - ((const char *) 0)) % __alignof__ (regex_t *);
+  expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
 
   if (__regexec ((const regex_t *) expbuf, string, 1, &match, REG_NOTEOL)
       == REG_NOMATCH)
@@ -73,7 +74,7 @@ advance (const char *string, const char *expbuf)
   regmatch_t match;    /* We only need info about the full match.  */
 
   expbuf += __alignof__ (regex_t *);
-  expbuf -= (expbuf - ((const char *) 0)) % __alignof__ (regex_t *);
+  expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
 
   if (__regexec ((const regex_t *) expbuf, string, 1, &match, REG_NOTEOL)
       == REG_NOMATCH
index dab852e8f29e24ae85027d5593c705a9db2cdb77..f30d99973d5048e83ffb8293982b958d46bd4ff8 100644 (file)
@@ -159,7 +159,7 @@ nscd_getgr_r (const char *key, size_t keylen, request_type type,
 
       /* Now allocate the buffer the array for the group members.  We must
         align the pointer.  */
-      align = ((__alignof__ (char *) - (p - ((char *) 0)))
+      align = ((__alignof__ (char *) - ((uintptr_t) p))
               & (__alignof__ (char *) - 1));
       total_len = (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
                   + gr_resp.gr_name_len + gr_resp.gr_passwd_len);
index 153194ad0455fcc825d58a4cd29582c70c6b6796..ab9c9d311f29a9116f47ee3222e9d90efe041e1a 100644 (file)
@@ -244,10 +244,9 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
       /* A first check whether the buffer is sufficiently large is possible.  */
       /* Now allocate the buffer the array for the group members.  We must
         align the pointer and the base of the h_addr_list pointers.  */
-      align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
+      align1 = ((__alignof__ (char *) - ((uintptr_t) cp))
                & (__alignof__ (char *) - 1));
-      align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
-                                        - ((char *) 0)))
+      align2 = ((__alignof__ (char *) - ((uintptr_t) (cp + align1 + hst_resp.h_name_len)))
                & (__alignof__ (char *) - 1));
       if (buflen < (align1 + hst_resp.h_name_len + align2
                    + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
index 0ee83ff88cfc373b836c2495fb393ffc05d9aeac..6969fcb739450ec3b805fe84139c0e9099214abe 100644 (file)
@@ -205,11 +205,10 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
       /* A first check whether the buffer is sufficiently large is possible.  */
       /* Now allocate the buffer the array for the group members.  We must
         align the pointer and the base of the h_addr_list pointers.  */
-      align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
+      align1 = ((__alignof__ (char *) - ((uintptr_t) cp))
                & (__alignof__ (char *) - 1));
-      align2 = ((__alignof__ (char *) - ((cp + align1 + serv_resp.s_name_len
-                                         + serv_resp.s_proto_len)
-                                        - ((char *) 0)))
+      align2 = ((__alignof__ (char *) - ((uintptr_t) (cp + align1 + serv_resp.s_name_len
+                                         + serv_resp.s_proto_len)))
                & (__alignof__ (char *) - 1));
       if (buflen < (align1 + serv_resp.s_name_len + serv_resp.s_proto_len
                    + align2
index 1c32884fe7b44b8ecd6fde331fa6a9eefc350449..4601cb6ce92a2fcb360b47439b447d3417ddeb7a 100644 (file)
@@ -281,7 +281,7 @@ get_next_alias (FILE *stream, const char *match, struct aliasent *result,
                      /* Adjust the pointer so it is aligned for
                         storing pointers.  */
                      first_unused += __alignof__ (char *) - 1;
-                     first_unused -= ((first_unused - (char *) 0)
+                     first_unused -= (((uintptr_t) first_unused)
                                       % __alignof__ (char *));
                      result->alias_members = (char **) first_unused;
 
index 632ba0a88082bf297d82cd99bc02d412b5a32ab8..77333b18fea85cae73a46fd5d227c6fba250de28 100644 (file)
@@ -239,7 +239,7 @@ parse_list (char **linep, char *eol, char *buf_end, int terminator_c,
 
   /* Adjust the pointer so it is aligned for storing pointers.  */
   eol += __alignof__ (char *) - 1;
-  eol -= (eol - (char *) 0) % __alignof__ (char *);
+  eol -= ((uintptr_t) eol) % __alignof__ (char *);
   /* We will start the storage here for the vector of pointers.  */
   list = (char **) eol;
 
index 6dd1cc3aa152d0be99e578c8b4853976451057a5..bbaa5e9f82a75b7829a5819d62e881b565cfd01d 100644 (file)
@@ -281,15 +281,15 @@ __qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg)
   else
     {
       if ((s & (sizeof (uint32_t) - 1)) == 0
-         && ((char *) b - (char *) 0) % __alignof__ (uint32_t) == 0)
+         && ((uintptr_t) b) % __alignof__ (uint32_t) == 0)
        {
          if (s == sizeof (uint32_t))
            p.var = 0;
          else if (s == sizeof (uint64_t)
-                  && ((char *) b - (char *) 0) % __alignof__ (uint64_t) == 0)
+                  && ((uintptr_t) b) % __alignof__ (uint64_t) == 0)
            p.var = 1;
          else if ((s & (sizeof (unsigned long) - 1)) == 0
-                  && ((char *) b - (char *) 0)
+                  && ((uintptr_t) b)
                      % __alignof__ (unsigned long) == 0)
            p.var = 2;
        }
index 7065df8acedf4df71a58bf44a5ccdb9f8b3feb53..1b3dd869b59bc189ede5c2799f7ccd7876195768 100644 (file)
@@ -129,7 +129,7 @@ _dl_sysdep_start (void **start_argptr,
        break up that far.  When the user program examines its break, it
        will see this new value and not clobber our data.  */
     __sbrk (GLRO(dl_pagesize)
-           - ((_end - (char *) 0) & (GLRO(dl_pagesize) - 1)));
+           - (((uintptr_t) _end) & (GLRO(dl_pagesize) - 1)));
 
   /* If this is a SUID program we make sure that FDs 0, 1, and 2 are
      allocated.  If necessary we are doing it ourself.  If it is not