diff mbox series

[31/39] string: Suppress clang -Wfortify-source warning on tester.c

Message ID 20241222000509.2205895-32-hjl.tools@gmail.com
State New
Headers show
Series [01/39] conform: Use -dD instead of -dN on compiler invocation | expand

Commit Message

H.J. Lu Dec. 22, 2024, 12:04 a.m. UTC
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>

Suppress clang -Wfortify-source warnings on tester.c, like:

tester.c:385:10: error: 'strncat' size argument is too large; destination buffer has size 50, but size argument is 99 [-Werror,-Wfortify-source]
  385 |   check (strncat (one, "lmn", 99) == one, 1);   /* Returned value. */
---
 string/tester.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

Comments

Sam James Dec. 22, 2024, 4:53 a.m. UTC | #1
"H.J. Lu" <hjl.tools@gmail.com> writes:

> From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>
> Suppress clang -Wfortify-source warnings on tester.c, like:
>
> tester.c:385:10: error: 'strncat' size argument is too large; destination buffer has size 50, but size argument is 99 [-Werror,-Wfortify-source]
>   385 |   check (strncat (one, "lmn", 99) == one, 1);   /* Returned value. */
> ---
>  string/tester.c | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
>
> diff --git a/string/tester.c b/string/tester.c
> index 3e3660777e..aa880be46c 100644
> --- a/string/tester.c
> +++ b/string/tester.c
> @@ -391,11 +391,19 @@ test_strncat (void)
>       mechanism.  */
>    it = "strncat";
>    (void) strcpy (one, "ijk");
> +  /* clang complains that size argument is too large for the destination
> +     buffer.  */
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
>    check (strncat (one, "lmn", 99) == one, 1);	/* Returned value. */
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>    equal (one, "ijklmn", 2);		/* Basic test. */
>  
>    (void) strcpy (one, "x");
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
>    (void) strncat (one, "yz", 99);
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>    equal (one, "xyz", 3);		/* Writeover. */
>    equal (one+4, "mn", 4);		/* Wrote too much? */
>  
> @@ -408,6 +416,7 @@ test_strncat (void)
>       GCC 7 or newer.  */
>  #if __GNUC_PREREQ (7, 0)
>    (void) strncat (one, two, 99);
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>    equal (one, "ghef", 5);			/* Basic test encore. */
>  #else
>    equal (one, "gh", 2);
> @@ -415,13 +424,22 @@ test_strncat (void)
>    equal (two, "ef", 6);			/* Stomped on source? */
>  
>    (void) strcpy (one, "");
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
>    (void) strncat (one, "", 99);
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>    equal (one, "", 7);			/* Boundary conditions. */
>    (void) strcpy (one, "ab");
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
>    (void) strncat (one, "", 99);
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>    equal (one, "ab", 8);
>    (void) strcpy (one, "");
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
>    (void) strncat (one, "cd", 99);
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>    equal (one, "cd", 9);
>  
>    (void) strcpy (one, "ab");
> @@ -434,7 +452,10 @@ test_strncat (void)
>    (void) strncat (one, "gh", 2);
>    equal (one, "abcdgh", 12);		/* Count and length equal. */
>  
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
>    (void) strncat (one, "ij", (size_t)-1);	/* set sign bit in count */
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>    equal (one, "abcdghij", 13);
>  
>    int ntest = 14;
> @@ -1438,7 +1459,12 @@ test_bzero (void)
>    equal(one+4, "ef", 3);
>  
>    (void) strcpy(one, "abcdef");
> +
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  /* clang complains about the 0 size argument for bzero.  */
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wsuspicious-bzero");
>    bzero(one+2, 0);
> +  DIAG_POP_NEEDS_COMMENT_CLANG
>    equal(one, "abcdef", 4);		/* Zero-length copy. */
>  }

Same comments as before apply (i.e. it's not perfect but you know that
and I know that, and it's not worth blocking anything over).

Reviewed-by: Sam James <sam@gentoo.org>
diff mbox series

Patch

diff --git a/string/tester.c b/string/tester.c
index 3e3660777e..aa880be46c 100644
--- a/string/tester.c
+++ b/string/tester.c
@@ -391,11 +391,19 @@  test_strncat (void)
      mechanism.  */
   it = "strncat";
   (void) strcpy (one, "ijk");
+  /* clang complains that size argument is too large for the destination
+     buffer.  */
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
   check (strncat (one, "lmn", 99) == one, 1);	/* Returned value. */
+  DIAG_POP_NEEDS_COMMENT_CLANG;
   equal (one, "ijklmn", 2);		/* Basic test. */
 
   (void) strcpy (one, "x");
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
   (void) strncat (one, "yz", 99);
+  DIAG_POP_NEEDS_COMMENT_CLANG;
   equal (one, "xyz", 3);		/* Writeover. */
   equal (one+4, "mn", 4);		/* Wrote too much? */
 
@@ -408,6 +416,7 @@  test_strncat (void)
      GCC 7 or newer.  */
 #if __GNUC_PREREQ (7, 0)
   (void) strncat (one, two, 99);
+  DIAG_POP_NEEDS_COMMENT_CLANG;
   equal (one, "ghef", 5);			/* Basic test encore. */
 #else
   equal (one, "gh", 2);
@@ -415,13 +424,22 @@  test_strncat (void)
   equal (two, "ef", 6);			/* Stomped on source? */
 
   (void) strcpy (one, "");
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
   (void) strncat (one, "", 99);
+  DIAG_POP_NEEDS_COMMENT_CLANG;
   equal (one, "", 7);			/* Boundary conditions. */
   (void) strcpy (one, "ab");
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
   (void) strncat (one, "", 99);
+  DIAG_POP_NEEDS_COMMENT_CLANG;
   equal (one, "ab", 8);
   (void) strcpy (one, "");
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
   (void) strncat (one, "cd", 99);
+  DIAG_POP_NEEDS_COMMENT_CLANG;
   equal (one, "cd", 9);
 
   (void) strcpy (one, "ab");
@@ -434,7 +452,10 @@  test_strncat (void)
   (void) strncat (one, "gh", 2);
   equal (one, "abcdgh", 12);		/* Count and length equal. */
 
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wfortify-source");
   (void) strncat (one, "ij", (size_t)-1);	/* set sign bit in count */
+  DIAG_POP_NEEDS_COMMENT_CLANG;
   equal (one, "abcdghij", 13);
 
   int ntest = 14;
@@ -1438,7 +1459,12 @@  test_bzero (void)
   equal(one+4, "ef", 3);
 
   (void) strcpy(one, "abcdef");
+
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  /* clang complains about the 0 size argument for bzero.  */
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wsuspicious-bzero");
   bzero(one+2, 0);
+  DIAG_POP_NEEDS_COMMENT_CLANG
   equal(one, "abcdef", 4);		/* Zero-length copy. */
 }