From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by sourceware.org (Postfix) with ESMTPS id 4487D385828E for ; Fri, 17 Feb 2023 19:43:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4487D385828E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oi1-x22c.google.com with SMTP id bk11so2200091oib.13 for ; Fri, 17 Feb 2023 11:43:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=PSlFMNnTmJWCLinJZOqGBbh7vAaEMxaQY2pdy2b3l+4=; b=b31XqoOyBhQfRb6+8ZjM5tD3TOTnB72cuao02QzR9Ra6+oCJhq8mnili12mrqn8HBw e0xmNSrGznbLgFDcTLSxFPtDs0G/wkYgpE9L2azOZPgm+9hyIcRLvGM6ucoiB/V2Bz2U EFyLx0gcdesy7tWXTQpnV0/3+RN03JIvUlUxXho/GEtgr0ArkTt4uziavPBelvDpf6iy LOyMeV/JKGZJPYad8ar+zXfxfd0052ujivkGXPgXQt1U+r3l41G6cUhh7ya7i9ZWfl08 qI8U/OCCHz/9zDFek4Enh57lbZJxFKiN/9qcfFCIGaoTw2EV43M392lewZzTwCXgJJiE 2CVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PSlFMNnTmJWCLinJZOqGBbh7vAaEMxaQY2pdy2b3l+4=; b=fsqrJd1nF9Y7qI1QVdLIAbMW1GT3gtNP+YCyLau+5tgXLfZM4r0wuokmEqeSijyPVH CcEfQTlGhvIbM91mk3lMF9KzTse+e2ykwi7RT5YHz45a3pmzNAwzYHWLXJyat5fJ8Ta2 zusPRUCTdDhhSABbK9ZwxMyFfS/9sU/qQ3pz/kpPfGuUJ6ajcl2AEKhp8IBNsZFs04gb 2ZL3X6f6/RCcchz3VMSmssDMX7DFi90uYsgNBf7L80S/RyaSgdaqW/3Fcb/ogdQwbRgE U6UIwFhuz0JfBbUyGAr3nncin/egqrE+euOnBUhOubegKXFz3fKbl4fR5+bk9B61HBO/ 5rtw== X-Gm-Message-State: AO0yUKXhrvHkF9wHUji1zrWn1saEYOe/eP22IAyjDRceH0vIrNeCtgFS os2aOzivZunn9w6vBK31qYbBEzzcE+u8Z5a+xTQ= X-Google-Smtp-Source: AK7set/22SisFiDqNnQiB/lmw3GwacAkKhJoPII68psSIUIUj0gn0GxAOdVMBLwUeiY8SBi+0ZO6ww== X-Received: by 2002:a05:6808:9af:b0:36a:48e7:76da with SMTP id e15-20020a05680809af00b0036a48e776damr794731oig.13.1676663036381; Fri, 17 Feb 2023 11:43:56 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c3:3a5:4cb8:36b0:2056:d611? ([2804:1b3:a7c3:3a5:4cb8:36b0:2056:d611]) by smtp.gmail.com with ESMTPSA id i132-20020acaea8a000000b0037d7f4d0890sm2166395oih.26.2023.02.17.11.43.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Feb 2023 11:43:55 -0800 (PST) Message-ID: <7bd539bd-5df2-43be-e8b5-db01021f88fb@linaro.org> Date: Fri, 17 Feb 2023 16:43:53 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.7.2 Subject: Re: [PATCH v2] Use uintptr_t instead of performing pointer subtraction with a null pointer Content-Language: en-US To: twose@qq.com, libc-alpha@sourceware.org References: From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 28/06/22 05:57, twose--- via Libc-alpha wrote: > From: Qihao Chencao > > Signed-off-by: Qihao Chencao LGTM, thanks. Reviewed-by: Adhemerval Zanella > --- > crypt/md5-crypt.c | 12 ++++++------ > crypt/sha256-crypt.c | 12 ++++++------ > crypt/sha512-crypt.c | 12 ++++++------ > elf/dl-minimal-malloc.c | 4 ++-- > misc/regexp.c | 5 +++-- > 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 +- > 12 files changed, 35 insertions(+), 36 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..81140d5073 100644 > --- a/misc/regexp.c > +++ b/misc/regexp.c > @@ -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 > #include > #include > > @@ -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 > 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