Use __builtin___stpncpy_chk when available

Message ID 20201112075915.2172453-1-siddhesh@sourceware.org
State Committed
Headers
Series Use __builtin___stpncpy_chk when available |

Commit Message

Siddhesh Poyarekar Nov. 12, 2020, 7:59 a.m. UTC
  The builtin has been available in gcc since 4.7.0 and in clang since
2.6.  This fixes stpncpy fortification with clang since it does a
better job of plugging in __stpncpy_chk in the right place than the
header hackery.

This has been tested by building and running all tests with gcc 10.2.1
and also with clang tip as of a few days ago (just the tests in debug/
since running all tests don't work with clang at the moment) to make
sure that both compilers pass the stpncpy tests.
---
 string/bits/string_fortified.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
  

Comments

Szabolcs Nagy Nov. 12, 2020, 8:29 a.m. UTC | #1
The 11/12/2020 13:29, Siddhesh Poyarekar via Libc-alpha wrote:
> The builtin has been available in gcc since 4.7.0 and in clang since
> 2.6.  This fixes stpncpy fortification with clang since it does a
> better job of plugging in __stpncpy_chk in the right place than the
> header hackery.
> 
> This has been tested by building and running all tests with gcc 10.2.1
> and also with clang tip as of a few days ago (just the tests in debug/
> since running all tests don't work with clang at the moment) to make
> sure that both compilers pass the stpncpy tests.

this change looks good to me.

> ---
>  string/bits/string_fortified.h | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
> index c8d3051af8..4c1aeb45f1 100644
> --- a/string/bits/string_fortified.h
> +++ b/string/bits/string_fortified.h
> @@ -91,7 +91,13 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
>    return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
>  }
>  
> -/* XXX We have no corresponding builtin yet.  */
> +#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
> +__fortify_function char *
> +__NTH (stpncpy (char *__dest, const char *__src, size_t __n))
> +{
> +  return __builtin___stpncpy_chk (__dest, __src, __n, __bos (__dest));
> +}
> +#else
>  extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
>  			    size_t __destlen) __THROW
>    __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
> @@ -106,6 +112,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
>      return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
>    return __stpncpy_alias (__dest, __src, __n);
>  }
> +#endif
>  
>  
>  __fortify_function char *
> -- 
> 2.26.2
> 

--
  

Patch

diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
index c8d3051af8..4c1aeb45f1 100644
--- a/string/bits/string_fortified.h
+++ b/string/bits/string_fortified.h
@@ -91,7 +91,13 @@  __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
 }
 
-/* XXX We have no corresponding builtin yet.  */
+#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
+__fortify_function char *
+__NTH (stpncpy (char *__dest, const char *__src, size_t __n))
+{
+  return __builtin___stpncpy_chk (__dest, __src, __n, __bos (__dest));
+}
+#else
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW
   __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2));
@@ -106,6 +112,7 @@  __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
   return __stpncpy_alias (__dest, __src, __n);
 }
+#endif
 
 
 __fortify_function char *