diff mbox

Use gcc stpncpy chk builtin (BZ #20661)

Message ID c6edf6bf-5443-0e05-ac65-b3eb7ed6b688@linaro.org
State New
Headers show

Commit Message

Adhemerval Zanella Sept. 30, 2016, 8:26 p.m. UTC
On 30/09/2016 13:10, Joseph Myers wrote:
> On Fri, 30 Sep 2016, Adhemerval Zanella wrote:

> 

>> Since r182378 GCC provides stpncpy_chk builtin support and it is

>> included in official GCC 4.7.4 package.  It allows GLIBC to use it

>> on string3.h headers without any additional guards.

> 

> You'll need to explain this more.  The installed glibc headers support GCC 

> 2.7 and later.  What is the minimum GCC version for which the installed 

> headers will end up including <bits/string3.h>?  Is it more recent than 

> the minimum version that has this built-in function?


Right, I did not take in consideration case for older GCC using newer
GLIBC.  Since builtin stpncpy_chk was only added on GCC 4.7 and 
string3.h is included for GCC 4.3+ it will required to be guarded
with __GNUC_PREREQ.

What about this:

	[BZ #20661]
	* string/bits/string3 [__USE_GNU] (stpncpy): Add fortify version based
	on GCC builtin for GCC 4.7 and later.
	(__stpncpy_chk): Define only for !__GNUC_PREREQ (4,7).
	(__stpncpy_alias): Likewise.

--
diff mbox

Patch

diff --git a/string/bits/string3.h b/string/bits/string3.h
index 8f13b65..d7440cc 100644
--- a/string/bits/string3.h
+++ b/string/bits/string3.h
@@ -38,6 +38,7 @@  __warndecl (__warn_memset_zero_len,
 # ifdef __USE_GNU
 #  undef mempcpy
 #  undef stpcpy
+#  undef stpncpy
 # endif
 # ifdef __USE_MISC
 #  undef bcopy
@@ -126,20 +127,29 @@  __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.  */
+
+#ifdef __USE_GNU
+
+# if !__GNUC_PREREQ (4,7)
 extern char *__stpncpy_chk (char *__dest, const char *__src, size_t __n,
 			    size_t __destlen) __THROW;
 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest, const char *__src,
 					       size_t __n), stpncpy);
+# endif
 
 __fortify_function char *
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 {
+# if !__GNUC_PREREQ (4,7)
   if (__bos (__dest) != (size_t) -1
       && (!__builtin_constant_p (__n) || __n > __bos (__dest)))
     return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
   return __stpncpy_alias (__dest, __src, __n);
+# else
+  return __builtin___stpncpy_chk (__dest, __src, __n, __bos (__dest));
+# endif
 }
+#endif
 
 
 __fortify_function char *