a41fe4
commit 7163ace3318d666d40771f5c8e7c4a148827070f
a41fe4
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
a41fe4
Date:   Thu Nov 12 12:09:56 2020 +0530
a41fe4
a41fe4
    Use __builtin___stpncpy_chk when available
a41fe4
    
a41fe4
    The builtin has been available in gcc since 4.7.0 and in clang since
a41fe4
    2.6.  This fixes stpncpy fortification with clang since it does a
a41fe4
    better job of plugging in __stpncpy_chk in the right place than the
a41fe4
    header hackery.
a41fe4
    
a41fe4
    This has been tested by building and running all tests with gcc 10.2.1
a41fe4
    and also with clang tip as of a few days ago (just the tests in debug/
a41fe4
    since running all tests don't work with clang at the moment) to make
a41fe4
    sure that both compilers pass the stpncpy tests.
a41fe4
a41fe4
diff --git a/string/bits/string_fortified.h b/string/bits/string_fortified.h
a41fe4
index a07ab0dbc8c8dd5b..4ed6755a6c1ca247 100644
a41fe4
--- a/string/bits/string_fortified.h
a41fe4
+++ b/string/bits/string_fortified.h
a41fe4
@@ -106,7 +106,13 @@ __NTH (strncpy (char *__restrict __dest, const char *__restrict __src,
a41fe4
   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
a41fe4
 }
a41fe4
 
a41fe4
-/* XXX We have no corresponding builtin yet.  */
a41fe4
+#if __GNUC_PREREQ (4, 7) || __glibc_clang_prereq (2, 6)
a41fe4
+__fortify_function char *
a41fe4
+__NTH (stpncpy (char *__dest, const char *__src, size_t __n))
a41fe4
+{
a41fe4
+  return __builtin___stpncpy_chk (__dest, __src, __n, __bos (__dest));
a41fe4
+}
a41fe4
+#else
a41fe4
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
a41fe4
 			    size_t __destlen) __THROW;
a41fe4
 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
a41fe4
@@ -120,6 +126,7 @@ __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
a41fe4
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
a41fe4
   return __stpncpy_alias (__dest, __src, __n);
a41fe4
 }
a41fe4
+#endif
a41fe4
 
a41fe4
 
a41fe4
 __fortify_function char *