diff mbox

string/stratcliff.c: Replace int with size_t [BZ #21982]

Message ID CAMe9rOpoOVHPS2BEbRYrKs+pkDQLU1b1Ci7_53YX1X8YLLkaVg@mail.gmail.com
State New, archived
Headers show

Commit Message

H.J. Lu Aug. 21, 2017, 3:03 p.m. UTC
On Mon, Aug 21, 2017 at 6:55 AM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Mon, 21 Aug 2017, H.J. Lu wrote:
>
>> On Mon, Aug 21, 2017 at 6:23 AM, Joseph Myers <joseph@codesourcery.com> wrote:
>> > On Sun, 20 Aug 2017, H.J. Lu wrote:
>> >
>> >> @@ -101,7 +101,7 @@ do_test (void)
>> >>
>> >>             if (STRLEN (&adr[outer]) != (size_t) (inner - outer))
>> >>               {
>> >> -               printf ("%s flunked for outer = %d, inner = %d\n",
>> >> +               printf ("%s flunked for outer = %Zd, inner = %Zd\n",
>> >
>> > I don't think we should be using legacy %Z in any new code.  Use C99 %zu
>> > for size_t (%zd only if the argument is the corresponding signed type
>> > rather than size_t itself).
>> >
>>
>> There are some %Zd in string/stratcliff.c.  Should they be changed?
>> Since All of them are size_t, I will replace %Zd with %zu.
>
> In my view it makes sense to clean up existing uses of %Z legacy formats,
> provided there is still stdio test coverage that %Z behaves as expected.
>

Here is the updated patch.  OK for master?

Thanks.
diff mbox

Patch

From a7e6555496d95a9c3abe6bb0f3ec9919873b365e Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Sun, 20 Aug 2017 10:11:38 -0700
Subject: [PATCH] string/stratcliff.c: Replace int with size_t [BZ #21982]
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fix GCC 7 errors when string/stratcliff.c is compiled with -O3:

stratcliff.c: In function ‘do_test’:
cc1: error: assuming signed overflow does not occur when assuming that (X - c) <= X is always true [-Werror=strict-overflow]

	[BZ #21982]
	* string/stratcliff.c (do_test): Declare size, nchars, inner,
	middle and outer with size_t instead of int.  Repleace %d and
	%Zd with %zu in printf.
---
 string/stratcliff.c | 76 ++++++++++++++++++++++++++---------------------------
 1 file changed, 38 insertions(+), 38 deletions(-)

diff --git a/string/stratcliff.c b/string/stratcliff.c
index e28b0c5058..b8557993d1 100644
--- a/string/stratcliff.c
+++ b/string/stratcliff.c
@@ -58,8 +58,8 @@ 
 int
 do_test (void)
 {
-  int size = sysconf (_SC_PAGESIZE);
-  int nchars = size / sizeof (CHAR);
+  size_t size = sysconf (_SC_PAGESIZE);
+  size_t nchars = size / sizeof (CHAR);
   CHAR *adr;
   CHAR *dest;
   int result = 0;
@@ -80,7 +80,7 @@  do_test (void)
     }
   else
     {
-      int inner, middle, outer;
+      size_t inner, middle, outer;
 
       mprotect (adr, size, PROT_NONE);
       mprotect (adr + 2 * nchars, size, PROT_NONE);
@@ -101,7 +101,7 @@  do_test (void)
 
 	      if (STRLEN (&adr[outer]) != (size_t) (inner - outer))
 		{
-		  printf ("%s flunked for outer = %d, inner = %d\n",
+		  printf ("%s flunked for outer = %zu, inner = %zu\n",
 			  STRINGIFY (STRLEN), outer, inner);
 		  result = 1;
 		}
@@ -120,7 +120,7 @@  do_test (void)
 	      if (STRNLEN (&adr[outer], inner - outer + 1)
 		  != (size_t) (inner - outer))
 		{
-		  printf ("%s flunked for outer = %d, inner = %d\n",
+		  printf ("%s flunked for outer = %zu, inner = %zu\n",
 			  STRINGIFY (STRNLEN), outer, inner);
 		  result = 1;
 		}
@@ -135,7 +135,7 @@  do_test (void)
 	      if (STRNLEN (&adr[outer], inner - outer)
 		  != (size_t) (inner - outer))
 		{
-		  printf ("%s flunked bounded for outer = %d, inner = %d\n",
+		  printf ("%s flunked bounded for outer = %zu, inner = %zu\n",
 			  STRINGIFY (STRNLEN), outer, inner);
 		  result = 1;
 		}
@@ -158,8 +158,8 @@  do_test (void)
 		      || (inner != middle
 			  && (cp - &adr[outer]) != middle - outer))
 		    {
-		      printf ("%s flunked for outer = %d, middle = %d, "
-			      "inner = %d\n",
+		      printf ("%s flunked for outer = %zu, middle = %zu, "
+			      "inner = %zu\n",
 			      STRINGIFY (STRCHR), outer, middle, inner);
 		      result = 1;
 		    }
@@ -195,8 +195,8 @@  do_test (void)
 		      || (inner != middle
 			  && (cp - &adr[outer]) != middle - outer))
 		    {
-		      printf ("%s flunked for outer = %d, middle = %d, "
-			      "inner = %d\n",
+		      printf ("%s flunked for outer = %zu, middle = %zu, "
+			      "inner = %zu\n",
 			      STRINGIFY (STRRCHR), outer, middle, inner);
 		      result = 1;
 		    }
@@ -218,7 +218,7 @@  do_test (void)
 
 	      if (cp - &adr[outer] != middle - outer)
 		{
-		  printf ("%s flunked for outer = %d, middle = %d\n",
+		  printf ("%s flunked for outer = %zu, middle = %zu\n",
 			  STRINGIFY (MEMCHR), outer, middle);
 		  result = 1;
 		}
@@ -232,7 +232,7 @@  do_test (void)
 
 	  if (cp != NULL)
 	    {
-	      printf ("%s flunked for outer = %d\n",
+	      printf ("%s flunked for outer = %zu\n",
 		      STRINGIFY (MEMCHR), outer);
 	      result = 1;
 	    }
@@ -251,7 +251,7 @@  do_test (void)
 
 	      if (cp - &adr[outer] != middle - outer)
 		{
-		  printf ("%s flunked for outer = %d, middle = %d\n",
+		  printf ("%s flunked for outer = %zu, middle = %zu\n",
 			  STRINGIFY (rawmemchr), outer, middle);
 		  result = 1;
 		}
@@ -271,7 +271,7 @@  do_test (void)
 
 	      if (cp - &adr[outer] != middle - outer)
 		{
-		  printf ("%s flunked for outer = %d, middle = %d\n",
+		  printf ("%s flunked for outer = %zu, middle = %zu\n",
 			  STRINGIFY (memrchr), outer, middle);
 		  result = 1;
 		}
@@ -285,7 +285,7 @@  do_test (void)
 
 	  if (cp != NULL)
 	    {
-	      printf ("%s flunked for outer = %d\n",
+	      printf ("%s flunked for outer = %zu\n",
 		      STRINGIFY (memrchr), outer);
 	      result = 1;
 	    }
@@ -302,7 +302,7 @@  do_test (void)
 	      if (STRCPY (dest, &adr[outer]) != dest
 		  || STRLEN (dest) != (size_t) (inner - outer))
 		{
-		  printf ("%s flunked for outer = %d, inner = %d\n",
+		  printf ("%s flunked for outer = %zu, inner = %zu\n",
 			  STRINGIFY (STRCPY), outer, inner);
 		  result = 1;
 		}
@@ -322,14 +322,14 @@  do_test (void)
 
 	    if (STRCMP (adr + middle, dest + nchars - outer) <= 0)
 	      {
-		printf ("%s 1 flunked for outer = %d, middle = %d\n",
+		printf ("%s 1 flunked for outer = %zu, middle = %zu\n",
 			STRINGIFY (STRCMP), outer, middle);
 		result = 1;
 	      }
 
 	    if (STRCMP (dest + nchars - outer, adr + middle) >= 0)
 	      {
-		printf ("%s 2 flunked for outer = %d, middle = %d\n",
+		printf ("%s 2 flunked for outer = %zu, middle = %zu\n",
 			STRINGIFY (STRCMP), outer, middle);
 		result = 1;
 	      }
@@ -348,16 +348,16 @@  do_test (void)
 	      {
 		if (STRNCMP (adr + middle, dest + nchars - outer, inner) != 0)
 		  {
-		    printf ("%s 1 flunked for outer = %d, middle = %d, "
-			    "inner = %d\n",
+		    printf ("%s 1 flunked for outer = %zu, middle = %zu, "
+			    "inner = %zu\n",
 			    STRINGIFY (STRNCMP), outer, middle, inner);
 		    result = 1;
 		  }
 
 		if (STRNCMP (dest + nchars - outer, adr + middle, inner) != 0)
 		  {
-		    printf ("%s 2 flunked for outer = %d, middle = %d, "
-			    "inner = %d\n",
+		    printf ("%s 2 flunked for outer = %zu, middle = %zu, "
+			    "inner = %zu\n",
 			    STRINGIFY (STRNCMP), outer, middle, inner);
 		    result = 1;
 		  }
@@ -365,14 +365,14 @@  do_test (void)
 
 	    if (STRNCMP (adr + middle, dest + nchars - outer, outer) >= 0)
 	      {
-		printf ("%s 1 flunked for outer = %d, middle = %d, full\n",
+		printf ("%s 1 flunked for outer = %zu, middle = %zu, full\n",
 			STRINGIFY (STRNCMP), outer, middle);
 		result = 1;
 	      }
 
 	    if (STRNCMP (dest + nchars - outer, adr + middle, outer) <= 0)
 	      {
-		printf ("%s 2 flunked for outer = %d, middle = %d, full\n",
+		printf ("%s 2 flunked for outer = %zu, middle = %zu, full\n",
 			STRINGIFY (STRNCMP), outer, middle);
 		result = 1;
 	      }
@@ -389,7 +389,7 @@  do_test (void)
 	      if (STRNCPY (dest, &adr[outer], len) != dest
 		  || MEMCMP (dest, &adr[outer], len) != 0)
 		{
-		  printf ("outer %s flunked for outer = %d, len = %Zd\n",
+		  printf ("outer %s flunked for outer = %zu, len = %zu\n",
 			  STRINGIFY (STRNCPY), outer, len);
 		  result = 1;
 		}
@@ -413,8 +413,8 @@  do_test (void)
 		      || (inner - outer < len
 			  && STRLEN (dest) != (inner - outer)))
 		    {
-		      printf ("%s flunked for outer = %d, inner = %d, "
-			      "len = %Zd\n",
+		      printf ("%s flunked for outer = %zu, inner = %zu, "
+			      "len = %zu\n",
 			      STRINGIFY (STRNCPY), outer, inner, len);
 		      result = 1;
 		    }
@@ -424,8 +424,8 @@  do_test (void)
 		      || (inner - outer < len
 			  && STRLEN (dest + 1) != (inner - outer)))
 		    {
-		      printf ("%s+1 flunked for outer = %d, inner = %d, "
-			      "len = %Zd\n",
+		      printf ("%s+1 flunked for outer = %zu, inner = %zu, "
+			      "len = %zu\n",
 			      STRINGIFY (STRNCPY), outer, inner, len);
 		      result = 1;
 		    }
@@ -444,7 +444,7 @@  do_test (void)
 
 	      if ((STPCPY (dest, &adr[outer]) - dest) != inner - outer)
 		{
-		  printf ("%s flunked for outer = %d, inner = %d\n",
+		  printf ("%s flunked for outer = %zu, inner = %zu\n",
 			  STRINGIFY (STPCPY), outer, inner);
 		  result = 1;
 		}
@@ -464,7 +464,7 @@  do_test (void)
 	      if (STPNCPY (dest, &adr[outer], len) != dest + len
 		  || MEMCMP (dest, &adr[outer], len) != 0)
 		{
-		  printf ("outer %s flunked for outer = %d, len = %Zd\n",
+		  printf ("outer %s flunked for outer = %zu, len = %zu\n",
 			  STRINGIFY (STPNCPY), outer, len);
 		  result = 1;
 		}
@@ -483,8 +483,8 @@  do_test (void)
 		  if ((STPNCPY (dest, &adr[outer], inner) - dest)
 		      != MIN (inner, middle - outer))
 		    {
-		      printf ("%s flunked for outer = %d, middle = %d, "
-			      "inner = %d\n",
+		      printf ("%s flunked for outer = %zu, middle = %zu, "
+			      "inner = %zu\n",
 			      STRINGIFY (STPNCPY), outer, middle, inner);
 		      result = 1;
 		    }
@@ -499,7 +499,7 @@  do_test (void)
 	for (inner = 0; inner < nchars - outer; ++inner)
 	  if (MEMCPY (dest, &adr[outer], inner) !=  dest)
 	    {
-	      printf ("%s flunked for outer = %d, inner = %d\n",
+	      printf ("%s flunked for outer = %zu, inner = %zu\n",
 		      STRINGIFY (MEMCPY), outer, inner);
 	      result = 1;
 	    }
@@ -509,7 +509,7 @@  do_test (void)
 	for (inner = 0; inner < nchars - outer; ++inner)
 	  if (MEMPCPY (dest, &adr[outer], inner) !=  dest + inner)
 	    {
-	      printf ("%s flunked for outer = %d, inner = %d\n",
+	      printf ("%s flunked for outer = %zu, inner = %zu\n",
 		      STRINGIFY (MEMPCPY), outer, inner);
 	      result = 1;
 	    }
@@ -522,7 +522,7 @@  do_test (void)
 	for (inner = 0; inner < nchars - outer; ++inner)
 	  if (memccpy (dest, &adr[outer], L('\1'), inner) != NULL)
 	    {
-	      printf ("memccpy flunked full copy for outer = %d, inner = %d\n",
+	      printf ("memccpy flunked full copy for outer = %zu, inner = %zu\n",
 		      outer, inner);
 	      result = 1;
 	    }
@@ -538,14 +538,14 @@  do_test (void)
 		    !=  dest + inner + 1)
 		  {
 		    printf ("\
-memccpy flunked partial copy for outer = %d, middle = %d, inner = %d\n",
+memccpy flunked partial copy for outer = %zu, middle = %zu, inner = %zu\n",
 			    outer, middle, inner);
 		    result = 1;
 		  }
 		else if (dest[inner + 1] != L('\2'))
 		  {
 		    printf ("\
-memccpy copied too much for outer = %d, middle = %d, inner = %d\n",
+memccpy copied too much for outer = %zu, middle = %zu, inner = %zu\n",
 			    outer, middle, inner);
 		    result = 1;
 		  }
-- 
2.13.5