[3/3] string: Fix tester with fortify enabled

Message ID 20230721121817.1978446-4-adhemerval.zanella@linaro.org
State Committed
Commit c2dc8ab6a5a4e67f96ddd3f6bfdf2c991385ec38
Headers
Series Fix some issues with tests when fortify is enabled |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed

Commit Message

Adhemerval Zanella Netto July 21, 2023, 12:18 p.m. UTC
  If fortify is enabled, the truncated output warning is issued by
the wrapper itself:

In function ‘strncpy’,
    inlined from ‘test_strncpy’ at tester.c:505:10:
../string/bits/string_fortified.h:95:10: error: ‘__builtin_strncpy’
destination unchanged after copying no bytes from a string of length 3
[-Werror=stringop-truncation]
   95 |   return __builtin___strncpy_chk (__dest, __src, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   96 |                                   __glibc_objsize (__dest));
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../include/bits/string_fortified.h:1,
                 from ../string/string.h:548,
                 from ../include/string.h:60,
                 from tester.c:33,
                 from inl-tester.c:6:
In function ‘strncpy’,
    inlined from ‘test_strncpy’ at tester.c:505:10:

Checked on x86_64-linux-gnu.
---
 string/tester.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
  

Comments

Carlos O'Donell July 24, 2023, 12:38 p.m. UTC | #1
On 7/21/23 08:18, Adhemerval Zanella via Libc-alpha wrote:
> If fortify is enabled, the truncated output warning is issued by
> the wrapper itself:
> 
> In function ‘strncpy’,
>     inlined from ‘test_strncpy’ at tester.c:505:10:
> ../string/bits/string_fortified.h:95:10: error: ‘__builtin_strncpy’
> destination unchanged after copying no bytes from a string of length 3
> [-Werror=stringop-truncation]
>    95 |   return __builtin___strncpy_chk (__dest, __src, __len,
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>    96 |                                   __glibc_objsize (__dest));
>       |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~
> In file included from ../include/bits/string_fortified.h:1,
>                  from ../string/string.h:548,
>                  from ../include/string.h:60,
>                  from tester.c:33,
>                  from inl-tester.c:6:
> In function ‘strncpy’,
>     inlined from ‘test_strncpy’ at tester.c:505:10:
> 
> Checked on x86_64-linux-gnu.

LGTM.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> ---
>  string/tester.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/string/tester.c b/string/tester.c
> index 8de70ad3ce..da42c72141 100644
> --- a/string/tester.c
> +++ b/string/tester.c
> @@ -26,6 +26,14 @@
>  #undef __USE_STRING_INLINES
>  #endif
>  
> +#include <sys/cdefs.h>
> +#include <libc-diag.h>
> +
> +/* Triggered by strncpy fortify wrapper when it is enabled.  */

OK. Comment and disable.

> +#if __GNUC_PREREQ (8, 0)

OK. GCC 8 is the latest version at which the behaviour starts (noted in your cover letter).

> +DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
> +#endif
> +
>  #include <errno.h>
>  #include <stdint.h>
>  #include <stdio.h>
> @@ -33,7 +41,6 @@
>  #include <string.h>
>  #include <strings.h>
>  #include <fcntl.h>
> -#include <libc-diag.h>
>  
>  /* This file tests a range of corner cases of string functions,
>     including cases where truncation occurs or where sizes specified
> @@ -45,9 +52,6 @@ DIAG_IGNORE_NEEDS_COMMENT (5.0, "-Wmemset-transposed-args");
>  DIAG_IGNORE_NEEDS_COMMENT (9, "-Wrestrict");
>  DIAG_IGNORE_NEEDS_COMMENT (7, "-Wstringop-overflow=");
>  #endif
> -#if __GNUC_PREREQ (8, 0)
> -DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
> -#endif

OK.

>  #if __GNUC_PREREQ (11, 0)
>  DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
>  #endif
  

Patch

diff --git a/string/tester.c b/string/tester.c
index 8de70ad3ce..da42c72141 100644
--- a/string/tester.c
+++ b/string/tester.c
@@ -26,6 +26,14 @@ 
 #undef __USE_STRING_INLINES
 #endif
 
+#include <sys/cdefs.h>
+#include <libc-diag.h>
+
+/* Triggered by strncpy fortify wrapper when it is enabled.  */
+#if __GNUC_PREREQ (8, 0)
+DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
+#endif
+
 #include <errno.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -33,7 +41,6 @@ 
 #include <string.h>
 #include <strings.h>
 #include <fcntl.h>
-#include <libc-diag.h>
 
 /* This file tests a range of corner cases of string functions,
    including cases where truncation occurs or where sizes specified
@@ -45,9 +52,6 @@  DIAG_IGNORE_NEEDS_COMMENT (5.0, "-Wmemset-transposed-args");
 DIAG_IGNORE_NEEDS_COMMENT (9, "-Wrestrict");
 DIAG_IGNORE_NEEDS_COMMENT (7, "-Wstringop-overflow=");
 #endif
-#if __GNUC_PREREQ (8, 0)
-DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
-#endif
 #if __GNUC_PREREQ (11, 0)
 DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
 #endif