diff mbox series

[uclibc-ng-devel] Replace null subtraction with cast

Message ID 20240220132646.247929-1-sven.linker@kernkonzept.com
State Accepted
Headers show
Series [uclibc-ng-devel] Replace null subtraction with cast | expand

Commit Message

Sven Linker Feb. 20, 2024, 1:26 p.m. UTC
Clang warns about null-pointer subtractions, which are undefined
behavior per the C standards. Replace the subtractions with
explicit casts to `uintptr_t`.
---
 libcrypt/sha256-crypt.c | 12 ++++++------
 libcrypt/sha512-crypt.c | 10 +++++-----
 2 files changed, 11 insertions(+), 11 deletions(-)

Comments

Waldemar Brodkorb Feb. 22, 2024, 6:44 p.m. UTC | #1
Hi Sven,
Sven Linker wrote,

> Clang warns about null-pointer subtractions, which are undefined
> behavior per the C standards. Replace the subtractions with
> explicit casts to `uintptr_t`.

Thanks, applied and pushed,
 best regards
  Waldemar
diff mbox series

Patch

diff --git a/libcrypt/sha256-crypt.c b/libcrypt/sha256-crypt.c
index 81bbe6ae6..b2ab62e48 100644
--- a/libcrypt/sha256-crypt.c
+++ b/libcrypt/sha256-crypt.c
@@ -104,24 +104,24 @@  __sha256_crypt_r (const char *key,
   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 = (char *) alloca (key_len + __alignof__ (uint32_t));
       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));
       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);
     }
 
   struct sha256_ctx ctx;
diff --git a/libcrypt/sha512-crypt.c b/libcrypt/sha512-crypt.c
index 9d17255aa..b8984d4ab 100644
--- a/libcrypt/sha512-crypt.c
+++ b/libcrypt/sha512-crypt.c
@@ -104,24 +104,24 @@  __sha512_crypt_r (const char *key,
   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 = (char *) alloca (key_len + __alignof__ (uint64_t));
       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);
     }
 
-  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);
     }
 
   struct sha512_ctx ctx;