diff mbox series

Use uintptr_t instead of performing pointer subtraction with a null pointer

Message ID tencent_9EEDF01FDFACB8D7D8925BCE5DF066D36E0A@qq.com
State New
Headers show
Series Use uintptr_t instead of performing pointer subtraction with a null pointer | expand

Commit Message

develop--- via Libc-alpha June 14, 2022, 9:23 a.m. UTC
From: twosee <twose@qq.com>

---
 crypt/md5-crypt.c                           | 12 ++++++------
 crypt/sha256-crypt.c                        | 12 ++++++------
 crypt/sha512-crypt.c                        | 12 ++++++------
 elf/dl-minimal-malloc.c                     |  4 ++--
 misc/regexp.c                               |  4 ++--
 nscd/nscd_getgr_r.c                         |  2 +-
 nscd/nscd_gethst_r.c                        |  5 ++---
 nscd/nscd_getserv_r.c                       |  7 +++----
 nss/nss_files/files-alias.c                 |  2 +-
 nss/nss_files/files-parse.c                 |  2 +-
 stdlib/msort.c                              |  6 +++---
 sysdeps/unix/sysv/linux/dl-sysdep.c         |  2 +-
 sysdeps/unix/sysv/linux/ia64/sys/ucontext.h |  2 +-
 13 files changed, 35 insertions(+), 37 deletions(-)

Comments

Fangrui Song June 14, 2022, 10:24 p.m. UTC | #1
On 2022-06-14, twose--- via Libc-alpha wrote:
>From: twosee <twose@qq.com>
>
>---
> crypt/md5-crypt.c                           | 12 ++++++------
> crypt/sha256-crypt.c                        | 12 ++++++------
> crypt/sha512-crypt.c                        | 12 ++++++------
> elf/dl-minimal-malloc.c                     |  4 ++--
> misc/regexp.c                               |  4 ++--
> nscd/nscd_getgr_r.c                         |  2 +-
> nscd/nscd_gethst_r.c                        |  5 ++---
> nscd/nscd_getserv_r.c                       |  7 +++----
> nss/nss_files/files-alias.c                 |  2 +-
> nss/nss_files/files-parse.c                 |  2 +-
> stdlib/msort.c                              |  6 +++---
> sysdeps/unix/sysv/linux/dl-sysdep.c         |  2 +-
> sysdeps/unix/sysv/linux/ia64/sys/ucontext.h |  2 +-
> 13 files changed, 35 insertions(+), 37 deletions(-)
>
>diff --git a/crypt/md5-crypt.c b/crypt/md5-crypt.c
>index 7c4fb9fb97..4a7f337f5f 100644
>--- a/crypt/md5-crypt.c
>+++ b/crypt/md5-crypt.c
>@@ -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;

Thanks for the patch.
This fixes a UB.

https://port70.net/~nsz/c/c11/n1570.html#6.5.6p9
"When two pointers are subtracted, both shall point to elements of the
same array object, or one past the last element of the array object... "

A null pointer does not point to an element of the object.

>@@ -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
>diff --git a/crypt/sha256-crypt.c b/crypt/sha256-crypt.c
>index a98a968a8b..3f7dd11140 100644
>--- a/crypt/sha256-crypt.c
>+++ b/crypt/sha256-crypt.c
>@@ -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
>diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c
>index ea13527c09..28e40c3e13 100644
>--- a/crypt/sha512-crypt.c
>+++ b/crypt/sha512-crypt.c
>@@ -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
>diff --git a/elf/dl-minimal-malloc.c b/elf/dl-minimal-malloc.c
>index 7cca54208d..44b54124ad 100644
>--- a/elf/dl-minimal-malloc.c
>+++ b/elf/dl-minimal-malloc.c
>@@ -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)
>diff --git a/misc/regexp.c b/misc/regexp.c
>index b1ea9e7eeb..b20ff4abe3 100644
>--- a/misc/regexp.c
>+++ b/misc/regexp.c
>@@ -50,7 +50,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 +73,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
>diff --git a/nscd/nscd_getgr_r.c b/nscd/nscd_getgr_r.c
>index 3636c031ec..caea464d15 100644
>--- a/nscd/nscd_getgr_r.c
>+++ b/nscd/nscd_getgr_r.c
>@@ -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);
>diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c
>index 9becb62033..f3e22f8cef 100644
>--- a/nscd/nscd_gethst_r.c
>+++ b/nscd/nscd_gethst_r.c
>@@ -246,10 +246,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
>diff --git a/nscd/nscd_getserv_r.c b/nscd/nscd_getserv_r.c
>index 42b875d024..9603c66330 100644
>--- a/nscd/nscd_getserv_r.c
>+++ b/nscd/nscd_getserv_r.c
>@@ -207,11 +207,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
>diff --git a/nss/nss_files/files-alias.c b/nss/nss_files/files-alias.c
>index 505721388f..2fd08f1d34 100644
>--- a/nss/nss_files/files-alias.c
>+++ b/nss/nss_files/files-alias.c
>@@ -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;
>
>diff --git a/nss/nss_files/files-parse.c b/nss/nss_files/files-parse.c
>index c90e293802..a3c4970491 100644
>--- a/nss/nss_files/files-parse.c
>+++ b/nss/nss_files/files-parse.c
>@@ -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;
>
>diff --git a/stdlib/msort.c b/stdlib/msort.c
>index cbe9a4a8fd..50e1afbe59 100644
>--- a/stdlib/msort.c
>+++ b/stdlib/msort.c
>@@ -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;
> 	}
>diff --git a/sysdeps/unix/sysv/linux/dl-sysdep.c b/sysdeps/unix/sysv/linux/dl-sysdep.c
>index 9e15c5bf69..dcb0601207 100644
>--- a/sysdeps/unix/sysv/linux/dl-sysdep.c
>+++ b/sysdeps/unix/sysv/linux/dl-sysdep.c
>@@ -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)));

I've checked all the above changes. All good.

>   /* 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
>diff --git a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>index 7e85688aa5..db2d91e43f 100644
>--- a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>+++ b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>@@ -77,7 +77,7 @@ typedef struct
> 	__builtin_offsetof (mcontext_t, __ctx(sc_gr)[0])
> #elif defined __GNUC__
> # define _SC_GR0_OFFSET	\
>-	(((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]) - (char *) 0)
>+	((uintptr_t) ((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]))

This is an UB-style manual implementation of offsetof.
If we touch this line, better to fix both UB. But you or someone else
can fix this in another patch.

> #else
> # define _SC_GR0_OFFSET	0xc8	/* pray that this is correct... */
> #endif
>-- 
>2.35.1
>
Carlos O'Donell June 15, 2022, 12:59 p.m. UTC | #2
On 6/14/22 05:23, twose--- via Libc-alpha wrote:
> From: twosee <twose@qq.com>

Please specify copyright status, either assignment to the FSF or DCO.

Please see the Contribution Checklist:
https://sourceware.org/glibc/wiki/Contribution%20checklist

> ---
>  crypt/md5-crypt.c                           | 12 ++++++------
>  crypt/sha256-crypt.c                        | 12 ++++++------
>  crypt/sha512-crypt.c                        | 12 ++++++------
>  elf/dl-minimal-malloc.c                     |  4 ++--
>  misc/regexp.c                               |  4 ++--

This fails pre-commit CI for i686.

Please see:
https://patchwork.sourceware.org/project/glibc/patch/tencent_9EEDF01FDFACB8D7D8925BCE5DF066D36E0A@qq.com/

gcc -m32 regexp.c -c -std=gnu11 -fgnu89-inline  -g -O2 -Wall -Wwrite-strings -Wundef -Werror -fmerge-all-constants -frounding-math -fno-stack-protector -fno-common -Wstrict-prototypes -Wold-style-definition -fmath-errno    -fPIC -Wa,-mtune=i686    -ftls-model=initial-exec      -I../include -I/build/misc  -I/build  -I../sysdeps/unix/sysv/linux/i386/i686  -I../sysdeps/i386/i686/nptl  -I../sysdeps/unix/sysv/linux/i386  -I../sysdeps/unix/sysv/linux/x86/include -I../sysdeps/unix/sysv/linux/x86  -I../sysdeps/x86/nptl  -I../sysdeps/i386/nptl  -I../sysdeps/unix/sysv/linux/include -I../sysdeps/unix/sysv/linux  -I../sysdeps/nptl  -I../sysdeps/pthread  -I../sysdeps/gnu  -I../sysdeps/unix/inet  -I../sysdeps/unix/sysv  -I../sysdeps/unix/i386  -I../sysdeps/unix  -I../sysdeps/posix  -I../sysdeps/i386/i686/fpu/multiarch  -I../sysdeps/i386/i686/fpu  -I../sysdeps/i386/i686/multiarch  -I../sysdeps/i386/i686  -I../sysdeps/i386/fpu  -I../sysdeps/x86/fpu  -I../sysdeps/i386  -I../sysdeps/x86/include -I../sysdeps/x86  -I../sysdeps/wordsize-32  -I../sysdeps/ieee754/float128  -I../sysdeps/ieee754/ldbl-96/include -I../sysdeps/ieee754/ldbl-96  -I../sysdeps/ieee754/dbl-64  -I../sysdeps/ieee754/flt-32  -I../sysdeps/ieee754  -I../sysdeps/generic  -I.. -I../libio -I.  -D_LIBC_REENTRANT -include /build/libc-modules.h -DMODULE_NAME=libc -include ../include/libc-symbols.h  -DPIC -DSHARED     -DTOP_NAMESPACE=glibc -o /build/misc/regexp.os -MD -MP -MF /build/misc/regexp.os.dt -MT /build/misc/regexp.os
regexp.c: In function 'step':
regexp.c:53:15: error: 'uintptr_t' undeclared (first use in this function)
   53 |   expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
      |               ^~~~~~~~~
regexp.c:28:1: note: 'uintptr_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?
   27 | #include <shlib-compat.h>
  +++ |+#include <stdint.h>
   28 | 
regexp.c:53:15: note: each undeclared identifier is reported only once for each function it appears in
   53 |   expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
      |               ^~~~~~~~~
regexp.c:53:25: error: expected ')' before 'expbuf'
   53 |   expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
      |             ~           ^~~~~~~
      |                         )
regexp.c: In function 'advance':
regexp.c:76:15: error: 'uintptr_t' undeclared (first use in this function)
   76 |   expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
      |               ^~~~~~~~~
regexp.c:76:15: note: 'uintptr_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?
regexp.c:76:25: error: expected ')' before 'expbuf'
   76 |   expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
      |             ~           ^~~~~~~
      |                         )
make[2]: *** [../o-iterator.mk:9: /build/misc/regexp.os] Error 1
make[2]: Target 'subdir_lib' not remade because of errors.
make[2]: Leaving directory '/glibc/misc'
make[1]: *** [Makefile:484: misc/subdir_lib] Error 2

>  nscd/nscd_getgr_r.c                         |  2 +-
>  nscd/nscd_gethst_r.c                        |  5 ++---
>  nscd/nscd_getserv_r.c                       |  7 +++----
>  nss/nss_files/files-alias.c                 |  2 +-
>  nss/nss_files/files-parse.c                 |  2 +-
>  stdlib/msort.c                              |  6 +++---
>  sysdeps/unix/sysv/linux/dl-sysdep.c         |  2 +-
>  sysdeps/unix/sysv/linux/ia64/sys/ucontext.h |  2 +-
>  13 files changed, 35 insertions(+), 37 deletions(-)
> 
> diff --git a/crypt/md5-crypt.c b/crypt/md5-crypt.c
> index 7c4fb9fb97..4a7f337f5f 100644
> --- a/crypt/md5-crypt.c
> +++ b/crypt/md5-crypt.c
> @@ -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
> diff --git a/crypt/sha256-crypt.c b/crypt/sha256-crypt.c
> index a98a968a8b..3f7dd11140 100644
> --- a/crypt/sha256-crypt.c
> +++ b/crypt/sha256-crypt.c
> @@ -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
> diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c
> index ea13527c09..28e40c3e13 100644
> --- a/crypt/sha512-crypt.c
> +++ b/crypt/sha512-crypt.c
> @@ -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
> diff --git a/elf/dl-minimal-malloc.c b/elf/dl-minimal-malloc.c
> index 7cca54208d..44b54124ad 100644
> --- a/elf/dl-minimal-malloc.c
> +++ b/elf/dl-minimal-malloc.c
> @@ -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)
> diff --git a/misc/regexp.c b/misc/regexp.c
> index b1ea9e7eeb..b20ff4abe3 100644
> --- a/misc/regexp.c
> +++ b/misc/regexp.c
> @@ -50,7 +50,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 +73,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
> diff --git a/nscd/nscd_getgr_r.c b/nscd/nscd_getgr_r.c
> index 3636c031ec..caea464d15 100644
> --- a/nscd/nscd_getgr_r.c
> +++ b/nscd/nscd_getgr_r.c
> @@ -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);
> diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c
> index 9becb62033..f3e22f8cef 100644
> --- a/nscd/nscd_gethst_r.c
> +++ b/nscd/nscd_gethst_r.c
> @@ -246,10 +246,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
> diff --git a/nscd/nscd_getserv_r.c b/nscd/nscd_getserv_r.c
> index 42b875d024..9603c66330 100644
> --- a/nscd/nscd_getserv_r.c
> +++ b/nscd/nscd_getserv_r.c
> @@ -207,11 +207,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
> diff --git a/nss/nss_files/files-alias.c b/nss/nss_files/files-alias.c
> index 505721388f..2fd08f1d34 100644
> --- a/nss/nss_files/files-alias.c
> +++ b/nss/nss_files/files-alias.c
> @@ -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;
>  
> diff --git a/nss/nss_files/files-parse.c b/nss/nss_files/files-parse.c
> index c90e293802..a3c4970491 100644
> --- a/nss/nss_files/files-parse.c
> +++ b/nss/nss_files/files-parse.c
> @@ -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;
>  
> diff --git a/stdlib/msort.c b/stdlib/msort.c
> index cbe9a4a8fd..50e1afbe59 100644
> --- a/stdlib/msort.c
> +++ b/stdlib/msort.c
> @@ -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;
>  	}
> diff --git a/sysdeps/unix/sysv/linux/dl-sysdep.c b/sysdeps/unix/sysv/linux/dl-sysdep.c
> index 9e15c5bf69..dcb0601207 100644
> --- a/sysdeps/unix/sysv/linux/dl-sysdep.c
> +++ b/sysdeps/unix/sysv/linux/dl-sysdep.c
> @@ -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
> diff --git a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
> index 7e85688aa5..db2d91e43f 100644
> --- a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
> +++ b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
> @@ -77,7 +77,7 @@ typedef struct
>  	__builtin_offsetof (mcontext_t, __ctx(sc_gr)[0])
>  #elif defined __GNUC__
>  # define _SC_GR0_OFFSET	\
> -	(((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]) - (char *) 0)
> +	((uintptr_t) ((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]))

The sys/ucontext.h header is a shipped public header, and therefore must be usable
in various compilation modes that may not include a definition for uintptr_t, therefore
we cannot unconditionally use such types in public headers.

>  #else
>  # define _SC_GR0_OFFSET	0xc8	/* pray that this is correct... */
>  #endif
Fangrui Song June 15, 2022, 8:14 p.m. UTC | #3
On 2022-06-15, Carlos O'Donell wrote:
>On 6/14/22 05:23, twose--- via Libc-alpha wrote:
>> From: twosee <twose@qq.com>
>
>Please specify copyright status, either assignment to the FSF or DCO.
>
>Please see the Contribution Checklist:
>https://sourceware.org/glibc/wiki/Contribution%20checklist
>
>> ---
>>  crypt/md5-crypt.c                           | 12 ++++++------
>>  crypt/sha256-crypt.c                        | 12 ++++++------
>>  crypt/sha512-crypt.c                        | 12 ++++++------
>>  elf/dl-minimal-malloc.c                     |  4 ++--
>>  misc/regexp.c                               |  4 ++--
>
>This fails pre-commit CI for i686.
>
>Please see:
>https://patchwork.sourceware.org/project/glibc/patch/tencent_9EEDF01FDFACB8D7D8925BCE5DF066D36E0A@qq.com/

Thanks:) I'll check patchwork state in the future
twosee June 16, 2022, 10:20 a.m. UTC | #4
>> /* 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
>> diff --git a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>> index 7e85688aa5..db2d91e43f 100644
>> --- a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>> +++ b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>> @@ -77,7 +77,7 @@ typedef struct
>> 	__builtin_offsetof (mcontext_t, __ctx(sc_gr)[0])
>> #elif defined __GNUC__
>> # define _SC_GR0_OFFSET	\
>> -	(((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]) - (char *) 0)
>> +	((uintptr_t) ((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]))
> 
> This is an UB-style manual implementation of offsetof.
> If we touch this line, better to fix both UB. But you or someone else
> can fix this in another patch.

Thank you for your review and explanation!

But I'm confused, is `((size_t)(&((type *)0)->field))` also a UB implementation?
I can't be sure if the following patch is correct right now:

-	(((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]) - (char *) 0)
+	((unsigned long int) (&((mcontext_t *) 0)->__ctx(sc_gr)[0]))

Regards,
Twosee
twosee June 16, 2022, 10:36 a.m. UTC | #5
> 2022年6月15日 20:59,Carlos O'Donell <carlos@redhat.com> 写道:
> 
> On 6/14/22 05:23, twose--- via Libc-alpha wrote:
>> From: twosee <twose@qq.com <mailto:twose@qq.com>>
> 
> Please specify copyright status, either assignment to the FSF or DCO.
> 
> Please see the Contribution Checklist:
> https://sourceware.org/glibc/wiki/Contribution%20checklist <https://sourceware.org/glibc/wiki/Contribution%20checklist>
> 
>> ---
>> crypt/md5-crypt.c | 12 ++++++------
>> crypt/sha256-crypt.c | 12 ++++++------
>> crypt/sha512-crypt.c | 12 ++++++------
>> elf/dl-minimal-malloc.c | 4 ++--
>> misc/regexp.c | 4 ++--
> 
> This fails pre-commit CI for i686.
> 
> Please see:
> https://patchwork.sourceware.org/project/glibc/patch/tencent_9EEDF01FDFACB8D7D8925BCE5DF066D36E0A@qq.com/ <https://patchwork.sourceware.org/project/glibc/patch/tencent_9EEDF01FDFACB8D7D8925BCE5DF066D36E0A@qq.com/>
> 
> gcc -m32 regexp.c -c -std=gnu11 -fgnu89-inline -g -O2 -Wall -Wwrite-strings -Wundef -Werror -fmerge-all-constants -frounding-math -fno-stack-protector -fno-common -Wstrict-prototypes -Wold-style-definition -fmath-errno -fPIC -Wa,-mtune=i686 -ftls-model=initial-exec -I../include -I/build/misc -I/build -I../sysdeps/unix/sysv/linux/i386/i686 -I../sysdeps/i386/i686/nptl -I../sysdeps/unix/sysv/linux/i386 -I../sysdeps/unix/sysv/linux/x86/include -I../sysdeps/unix/sysv/linux/x86 -I../sysdeps/x86/nptl -I../sysdeps/i386/nptl -I../sysdeps/unix/sysv/linux/include -I../sysdeps/unix/sysv/linux -I../sysdeps/nptl -I../sysdeps/pthread -I../sysdeps/gnu -I../sysdeps/unix/inet -I../sysdeps/unix/sysv -I../sysdeps/unix/i386 -I../sysdeps/unix -I../sysdeps/posix -I../sysdeps/i386/i686/fpu/multiarch -I../sysdeps/i386/i686/fpu -I../sysdeps/i386/i686/multiarch -I../sysdeps/i386/i686 -I../sysdeps/i386/fpu -I../sysdeps/x86/fpu -I../sysdeps/i386 -I../sysdeps/x86/include -I../sysdeps/x86 -I../sysdeps/wordsize-32 -I../sysdeps/ieee754/float128 -I../sysdeps/ieee754/ldbl-96/include -I../sysdeps/ieee754/ldbl-96 -I../sysdeps/ieee754/dbl-64 -I../sysdeps/ieee754/flt-32 -I../sysdeps/ieee754 -I../sysdeps/generic -I.. -I../libio -I. -D_LIBC_REENTRANT -include /build/libc-modules.h -DMODULE_NAME=libc -include ../include/libc-symbols.h -DPIC -DSHARED -DTOP_NAMESPACE=glibc -o /build/misc/regexp.os -MD -MP -MF /build/misc/regexp.os.dt -MT /build/misc/regexp.os
> regexp.c: In function 'step':
> regexp.c:53:15: error: 'uintptr_t' undeclared (first use in this function)
> 53 | expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
> | ^~~~~~~~~
> regexp.c:28:1: note: 'uintptr_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?
> 27 | #include <shlib-compat.h>
> +++ |+#include <stdint.h>
> 28 | 
> regexp.c:53:15: note: each undeclared identifier is reported only once for each function it appears in
> 53 | expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
> | ^~~~~~~~~
> regexp.c:53:25: error: expected ')' before 'expbuf'
> 53 | expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
> | ~ ^~~~~~~
> | )
> regexp.c: In function 'advance':
> regexp.c:76:15: error: 'uintptr_t' undeclared (first use in this function)
> 76 | expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
> | ^~~~~~~~~
> regexp.c:76:15: note: 'uintptr_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?
> regexp.c:76:25: error: expected ')' before 'expbuf'
> 76 | expbuf -= ((uintptr_t) expbuf) % __alignof__ (regex_t *);
> | ~ ^~~~~~~
> | )
> make[2]: *** [../o-iterator.mk:9: /build/misc/regexp.os] Error 1
> make[2]: Target 'subdir_lib' not remade because of errors.
> make[2]: Leaving directory '/glibc/misc'
> make[1]: *** [Makefile:484: misc/subdir_lib] Error 2
> 
>> nscd/nscd_getgr_r.c | 2 +-
>> nscd/nscd_gethst_r.c | 5 ++---
>> nscd/nscd_getserv_r.c | 7 +++----
>> nss/nss_files/files-alias.c | 2 +-
>> nss/nss_files/files-parse.c | 2 +-
>> stdlib/msort.c | 6 +++---
>> sysdeps/unix/sysv/linux/dl-sysdep.c | 2 +-
>> sysdeps/unix/sysv/linux/ia64/sys/ucontext.h | 2 +-
>> 13 files changed, 35 insertions(+), 37 deletions(-)
>> 
>> diff --git a/crypt/md5-crypt.c b/crypt/md5-crypt.c
>> index 7c4fb9fb97..4a7f337f5f 100644
>> --- a/crypt/md5-crypt.c
>> +++ b/crypt/md5-crypt.c
>> @@ -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
>> diff --git a/crypt/sha256-crypt.c b/crypt/sha256-crypt.c
>> index a98a968a8b..3f7dd11140 100644
>> --- a/crypt/sha256-crypt.c
>> +++ b/crypt/sha256-crypt.c
>> @@ -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
>> diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c
>> index ea13527c09..28e40c3e13 100644
>> --- a/crypt/sha512-crypt.c
>> +++ b/crypt/sha512-crypt.c
>> @@ -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
>> diff --git a/elf/dl-minimal-malloc.c b/elf/dl-minimal-malloc.c
>> index 7cca54208d..44b54124ad 100644
>> --- a/elf/dl-minimal-malloc.c
>> +++ b/elf/dl-minimal-malloc.c
>> @@ -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)
>> diff --git a/misc/regexp.c b/misc/regexp.c
>> index b1ea9e7eeb..b20ff4abe3 100644
>> --- a/misc/regexp.c
>> +++ b/misc/regexp.c
>> @@ -50,7 +50,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 +73,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
>> diff --git a/nscd/nscd_getgr_r.c b/nscd/nscd_getgr_r.c
>> index 3636c031ec..caea464d15 100644
>> --- a/nscd/nscd_getgr_r.c
>> +++ b/nscd/nscd_getgr_r.c
>> @@ -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);
>> diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c
>> index 9becb62033..f3e22f8cef 100644
>> --- a/nscd/nscd_gethst_r.c
>> +++ b/nscd/nscd_gethst_r.c
>> @@ -246,10 +246,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
>> diff --git a/nscd/nscd_getserv_r.c b/nscd/nscd_getserv_r.c
>> index 42b875d024..9603c66330 100644
>> --- a/nscd/nscd_getserv_r.c
>> +++ b/nscd/nscd_getserv_r.c
>> @@ -207,11 +207,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
>> diff --git a/nss/nss_files/files-alias.c b/nss/nss_files/files-alias.c
>> index 505721388f..2fd08f1d34 100644
>> --- a/nss/nss_files/files-alias.c
>> +++ b/nss/nss_files/files-alias.c
>> @@ -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;
>> 
>> diff --git a/nss/nss_files/files-parse.c b/nss/nss_files/files-parse.c
>> index c90e293802..a3c4970491 100644
>> --- a/nss/nss_files/files-parse.c
>> +++ b/nss/nss_files/files-parse.c
>> @@ -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;
>> 
>> diff --git a/stdlib/msort.c b/stdlib/msort.c
>> index cbe9a4a8fd..50e1afbe59 100644
>> --- a/stdlib/msort.c
>> +++ b/stdlib/msort.c
>> @@ -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;
>> 	}
>> diff --git a/sysdeps/unix/sysv/linux/dl-sysdep.c b/sysdeps/unix/sysv/linux/dl-sysdep.c
>> index 9e15c5bf69..dcb0601207 100644
>> --- a/sysdeps/unix/sysv/linux/dl-sysdep.c
>> +++ b/sysdeps/unix/sysv/linux/dl-sysdep.c
>> @@ -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
>> diff --git a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>> index 7e85688aa5..db2d91e43f 100644
>> --- a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>> +++ b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
>> @@ -77,7 +77,7 @@ typedef struct
>> 	__builtin_offsetof (mcontext_t, __ctx(sc_gr)[0])
>> #elif defined __GNUC__
>> # define _SC_GR0_OFFSET	\
>> -	(((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]) - (char *) 0)
>> +	((uintptr_t) ((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]))
> 
> The sys/ucontext.h header is a shipped public header, and therefore must be usable
> in various compilation modes that may not include a definition for uintptr_t, therefore
> we cannot unconditionally use such types in public headers.
> 
>> #else
>> # define _SC_GR0_OFFSET	0xc8	/* pray that this is correct... */
>> #endif
> 
> 
> -- 
> Cheers,
> Carlos.


Thank you!
Forgive me, I'm still a rookie, I wanted to ask some questions here:
So next, I should send a new emaill message which included PATCH v2, and I should add sign-off line to the end of my git commit message (use my realname).
In the patch v2, I should add <stdint.h> to regexp.c, drop ucontext.h changes and consider fixing it in another patch, right?

Regards,
Twosee
diff mbox series

Patch

diff --git a/crypt/md5-crypt.c b/crypt/md5-crypt.c
index 7c4fb9fb97..4a7f337f5f 100644
--- a/crypt/md5-crypt.c
+++ b/crypt/md5-crypt.c
@@ -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
diff --git a/crypt/sha256-crypt.c b/crypt/sha256-crypt.c
index a98a968a8b..3f7dd11140 100644
--- a/crypt/sha256-crypt.c
+++ b/crypt/sha256-crypt.c
@@ -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
diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c
index ea13527c09..28e40c3e13 100644
--- a/crypt/sha512-crypt.c
+++ b/crypt/sha512-crypt.c
@@ -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
diff --git a/elf/dl-minimal-malloc.c b/elf/dl-minimal-malloc.c
index 7cca54208d..44b54124ad 100644
--- a/elf/dl-minimal-malloc.c
+++ b/elf/dl-minimal-malloc.c
@@ -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)
diff --git a/misc/regexp.c b/misc/regexp.c
index b1ea9e7eeb..b20ff4abe3 100644
--- a/misc/regexp.c
+++ b/misc/regexp.c
@@ -50,7 +50,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 +73,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
diff --git a/nscd/nscd_getgr_r.c b/nscd/nscd_getgr_r.c
index 3636c031ec..caea464d15 100644
--- a/nscd/nscd_getgr_r.c
+++ b/nscd/nscd_getgr_r.c
@@ -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);
diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c
index 9becb62033..f3e22f8cef 100644
--- a/nscd/nscd_gethst_r.c
+++ b/nscd/nscd_gethst_r.c
@@ -246,10 +246,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
diff --git a/nscd/nscd_getserv_r.c b/nscd/nscd_getserv_r.c
index 42b875d024..9603c66330 100644
--- a/nscd/nscd_getserv_r.c
+++ b/nscd/nscd_getserv_r.c
@@ -207,11 +207,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
diff --git a/nss/nss_files/files-alias.c b/nss/nss_files/files-alias.c
index 505721388f..2fd08f1d34 100644
--- a/nss/nss_files/files-alias.c
+++ b/nss/nss_files/files-alias.c
@@ -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;
 
diff --git a/nss/nss_files/files-parse.c b/nss/nss_files/files-parse.c
index c90e293802..a3c4970491 100644
--- a/nss/nss_files/files-parse.c
+++ b/nss/nss_files/files-parse.c
@@ -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;
 
diff --git a/stdlib/msort.c b/stdlib/msort.c
index cbe9a4a8fd..50e1afbe59 100644
--- a/stdlib/msort.c
+++ b/stdlib/msort.c
@@ -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;
 	}
diff --git a/sysdeps/unix/sysv/linux/dl-sysdep.c b/sysdeps/unix/sysv/linux/dl-sysdep.c
index 9e15c5bf69..dcb0601207 100644
--- a/sysdeps/unix/sysv/linux/dl-sysdep.c
+++ b/sysdeps/unix/sysv/linux/dl-sysdep.c
@@ -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
diff --git a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
index 7e85688aa5..db2d91e43f 100644
--- a/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
+++ b/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
@@ -77,7 +77,7 @@  typedef struct
 	__builtin_offsetof (mcontext_t, __ctx(sc_gr)[0])
 #elif defined __GNUC__
 # define _SC_GR0_OFFSET	\
-	(((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]) - (char *) 0)
+	((uintptr_t) ((char *) &((mcontext_t *) 0)->__ctx(sc_gr)[0]))
 #else
 # define _SC_GR0_OFFSET	0xc8	/* pray that this is correct... */
 #endif