Patchwork Use gcc stpncpy chk builtin (BZ #20661)

login
register
mail settings
Submitter Adhemerval Zanella Netto
Date Sept. 30, 2016, 7:14 p.m.
Message ID <1475262842-6724-1-git-send-email-adhemerval.zanella@linaro.com>
Download mbox | patch
Permalink /patch/16165/
State New
Headers show

Comments

Adhemerval Zanella Netto - Sept. 30, 2016, 7:14 p.m.
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.

This patch changes current fortify code to use it instead. The
fortify definition is also new guarded with __USE_GNU, similar to
stpcpy.

Check on x86_64 with GCC 5.4.

	[BZ #20661]
	* string/bits/string3 [__USE_GNU] (stpncpy): Add fortify version based
	on GCC builtin.
---
 ChangeLog             |  6 ++++++
 string/bits/string3.h | 13 ++++---------
 2 files changed, 10 insertions(+), 9 deletions(-)
Joseph Myers - Sept. 30, 2016, 8:10 p.m.
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?

Patch

diff --git a/string/bits/string3.h b/string/bits/string3.h
index 8f13b65..e19947c 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,14 @@  __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.  */
-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);
 
+#ifdef __USE_GNU
 __fortify_function char *
 __NTH (stpncpy (char *__dest, const char *__src, size_t __n))
 {
-  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);
+  return __builtin___stpncpy_chk (__dest, __src, __n, __bos (__dest));
 }
+#endif
 
 
 __fortify_function char *