[13/20] Use uintptr_t in string/tester for pointer alignment

Message ID 84fb3b5f7c4c0ea690ac3056fb2a8d97610dd3b4.1666877952.git.szabolcs.nagy@arm.com (mailing list archive)
State Committed
Commit f9deea6f81cd5266ffb3d27e2584d609fef9b8d2
Headers
Series patches from the morello port |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Szabolcs Nagy Oct. 27, 2022, 3:33 p.m. UTC
  The code assumed unsigned long can represent pointers.
---
 string/tester.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
  

Comments

Adhemerval Zanella Oct. 28, 2022, 2:11 p.m. UTC | #1
On 27/10/22 12:33, Szabolcs Nagy via Libc-alpha wrote:
> The code assumed unsigned long can represent pointers.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  string/tester.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/string/tester.c b/string/tester.c
> index eed76239f5..ba948c5723 100644
> --- a/string/tester.c
> +++ b/string/tester.c
> @@ -27,6 +27,7 @@
>  #endif
>  
>  #include <errno.h>
> +#include <stdint.h>
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> @@ -529,7 +530,7 @@ test_strlen (void)
>      char *p;
>      for (i=0; i < 0x100; i++)
>        {
> -	p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
> +	p = (char *) ((uintptr_t)(buf + 0xff) & ~0xff) + i;
>  	strcpy (p, "OK");
>  	strcpy (p+3, "BAD/WRONG");
>  	check (strlen (p) == 2, 4+i);
> @@ -554,7 +555,7 @@ test_strnlen (void)
>    char buf[4096];
>    for (int i = 0; i < 0x100; ++i)
>      {
> -      char *p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
> +      char *p = (char *) ((uintptr_t)(buf + 0xff) & ~0xff) + i;
>        strcpy (p, "OK");
>        strcpy (p + 3, "BAD/WRONG");
>        check (strnlen (p, 100) == 2, 10 + i);
> @@ -582,7 +583,7 @@ test_strchr (void)
>      char *p;
>      for (i=0; i < 0x100; i++)
>        {
> -	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
> +	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
>  	strcpy (p, "OK");
>  	strcpy (p+3, "BAD/WRONG");
>  	check (strchr (p, '/') == NULL, 9+i);
> @@ -614,7 +615,7 @@ test_strchrnul (void)
>      char *p;
>      for (i=0; i < 0x100; i++)
>        {
> -	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
> +	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
>  	strcpy (p, "OK");
>  	strcpy (p+3, "BAD/WRONG");
>  	cp = strchrnul (p, '/');
> @@ -643,7 +644,7 @@ test_rawmemchr (void)
>      char *p;
>      for (i=0; i < 0x100; i++)
>        {
> -	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
> +	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
>  	strcpy (p, "OK");
>  	strcpy (p+3, "BAD/WRONG");
>  	check (rawmemchr (p, 'R') == p+8, 6+i);
> @@ -689,7 +690,7 @@ test_strrchr (void)
>      char *p;
>      for (i=0; i < 0x100; i++)
>        {
> -	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
> +	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
>  	strcpy (p, "OK");
>  	strcpy (p+3, "BAD/WRONG");
>  	check (strrchr (p, '/') == NULL, 9+i);
  

Patch

diff --git a/string/tester.c b/string/tester.c
index eed76239f5..ba948c5723 100644
--- a/string/tester.c
+++ b/string/tester.c
@@ -27,6 +27,7 @@ 
 #endif
 
 #include <errno.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -529,7 +530,7 @@  test_strlen (void)
     char *p;
     for (i=0; i < 0x100; i++)
       {
-	p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
+	p = (char *) ((uintptr_t)(buf + 0xff) & ~0xff) + i;
 	strcpy (p, "OK");
 	strcpy (p+3, "BAD/WRONG");
 	check (strlen (p) == 2, 4+i);
@@ -554,7 +555,7 @@  test_strnlen (void)
   char buf[4096];
   for (int i = 0; i < 0x100; ++i)
     {
-      char *p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
+      char *p = (char *) ((uintptr_t)(buf + 0xff) & ~0xff) + i;
       strcpy (p, "OK");
       strcpy (p + 3, "BAD/WRONG");
       check (strnlen (p, 100) == 2, 10 + i);
@@ -582,7 +583,7 @@  test_strchr (void)
     char *p;
     for (i=0; i < 0x100; i++)
       {
-	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
+	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
 	strcpy (p, "OK");
 	strcpy (p+3, "BAD/WRONG");
 	check (strchr (p, '/') == NULL, 9+i);
@@ -614,7 +615,7 @@  test_strchrnul (void)
     char *p;
     for (i=0; i < 0x100; i++)
       {
-	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
+	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
 	strcpy (p, "OK");
 	strcpy (p+3, "BAD/WRONG");
 	cp = strchrnul (p, '/');
@@ -643,7 +644,7 @@  test_rawmemchr (void)
     char *p;
     for (i=0; i < 0x100; i++)
       {
-	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
+	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
 	strcpy (p, "OK");
 	strcpy (p+3, "BAD/WRONG");
 	check (rawmemchr (p, 'R') == p+8, 6+i);
@@ -689,7 +690,7 @@  test_strrchr (void)
     char *p;
     for (i=0; i < 0x100; i++)
       {
-	p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
+	p = (char *) ((uintptr_t) (buf + 0xff) & ~0xff) + i;
 	strcpy (p, "OK");
 	strcpy (p+3, "BAD/WRONG");
 	check (strrchr (p, '/') == NULL, 9+i);