a41fe4
commit a643f60c53876be0d57b4b7373770e6cb356fd13
a41fe4
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
a41fe4
Date:   Wed Oct 20 18:12:41 2021 +0530
a41fe4
a41fe4
    Make sure that the fortified function conditionals are constant
a41fe4
    
a41fe4
    In _FORTIFY_SOURCE=3, the size expression may be non-constant,
a41fe4
    resulting in branches in the inline functions remaining intact and
a41fe4
    causing a tiny overhead.  Clang (and in future, gcc) make sure that
a41fe4
    the -1 case is always safe, i.e. any comparison of the generated
a41fe4
    expression with (size_t)-1 is always false so that bit is taken care
a41fe4
    of.  The rest is avoidable since we want the _chk variant whenever we
a41fe4
    have a size expression and it's not -1.
a41fe4
    
a41fe4
    Rework the conditionals in a uniform way to clearly indicate two
a41fe4
    conditions at compile time:
a41fe4
    
a41fe4
    - Either the size is unknown (-1) or we know at compile time that the
a41fe4
      operation length is less than the object size.  We can call the
a41fe4
      original function in this case.  It could be that either the length,
a41fe4
      object size or both are non-constant, but the compiler, through
a41fe4
      range analysis, is able to fold the *comparison* to a constant.
a41fe4
    
a41fe4
    - The size and length are known and the compiler can see at compile
a41fe4
      time that operation length > object size.  This is valid grounds for
a41fe4
      a warning at compile time, followed by emitting the _chk variant.
a41fe4
    
a41fe4
    For everything else, emit the _chk variant.
a41fe4
    
a41fe4
    This simplifies most of the fortified function implementations and at
a41fe4
    the same time, ensures that only one call from _chk or the regular
a41fe4
    function is emitted.
a41fe4
    
a41fe4
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
a41fe4
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
a41fe4
a41fe4
diff --git a/io/bits/poll2.h b/io/bits/poll2.h
a41fe4
index f47fd9ad0945234f..6f4dae77e5e2d0d3 100644
a41fe4
--- a/io/bits/poll2.h
a41fe4
+++ b/io/bits/poll2.h
a41fe4
@@ -35,16 +35,9 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
a41fe4
 __fortify_function int
a41fe4
 poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
a41fe4
-    {
a41fe4
-      if (! __builtin_constant_p (__nfds))
a41fe4
-	return __poll_chk (__fds, __nfds, __timeout, __glibc_objsize (__fds));
a41fe4
-      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
a41fe4
-	return __poll_chk_warn (__fds, __nfds, __timeout,
a41fe4
-				__glibc_objsize (__fds));
a41fe4
-    }
a41fe4
-
a41fe4
-  return __poll_alias (__fds, __nfds, __timeout);
a41fe4
+  return __glibc_fortify (poll, __nfds, sizeof (*__fds),
a41fe4
+			  __glibc_objsize (__fds),
a41fe4
+			  __fds, __nfds, __timeout);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -66,17 +59,9 @@ __fortify_function int
a41fe4
 ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
a41fe4
        const __sigset_t *__ss)
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
a41fe4
-    {
a41fe4
-      if (! __builtin_constant_p (__nfds))
a41fe4
-	return __ppoll_chk (__fds, __nfds, __timeout, __ss,
a41fe4
-			    __glibc_objsize (__fds));
a41fe4
-      else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
a41fe4
-	return __ppoll_chk_warn (__fds, __nfds, __timeout, __ss,
a41fe4
-				 __glibc_objsize (__fds));
a41fe4
-    }
a41fe4
-
a41fe4
-  return __ppoll_alias (__fds, __nfds, __timeout, __ss);
a41fe4
+  return __glibc_fortify (ppoll, __nfds, sizeof (*__fds),
a41fe4
+			  __glibc_objsize (__fds),
a41fe4
+			  __fds, __nfds, __timeout, __ss);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
a41fe4
index 2cd69f44cfadfc9f..4630fe0256b1a562 100644
a41fe4
--- a/libio/bits/stdio2.h
a41fe4
+++ b/libio/bits/stdio2.h
a41fe4
@@ -256,15 +256,12 @@ extern char *__REDIRECT (__fgets_chk_warn,
a41fe4
 __fortify_function __wur char *
a41fe4
 fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__s) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n) || __n <= 0)
a41fe4
-	return __fgets_chk (__s, __glibc_objsize (__s), __n, __stream);
a41fe4
-
a41fe4
-      if ((size_t) __n > __glibc_objsize (__s))
a41fe4
-	return __fgets_chk_warn (__s, __glibc_objsize (__s), __n, __stream);
a41fe4
-    }
a41fe4
-  return __fgets_alias (__s, __n, __stream);
a41fe4
+  size_t sz = __glibc_objsize (__s);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
a41fe4
+    return __fgets_alias (__s, __n, __stream);
a41fe4
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
a41fe4
+    return __fgets_chk_warn (__s, sz, __n, __stream);
a41fe4
+  return __fgets_chk (__s, sz, __n, __stream);
a41fe4
 }
a41fe4
 
a41fe4
 extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
a41fe4
@@ -286,19 +283,12 @@ __fortify_function __wur size_t
a41fe4
 fread (void *__restrict __ptr, size_t __size, size_t __n,
a41fe4
        FILE *__restrict __stream)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__ptr) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__size)
a41fe4
-	  || !__builtin_constant_p (__n)
a41fe4
-	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
a41fe4
-	return __fread_chk (__ptr, __glibc_objsize0 (__ptr), __size, __n,
a41fe4
-			    __stream);
a41fe4
-
a41fe4
-      if (__size * __n > __glibc_objsize0 (__ptr))
a41fe4
-	return __fread_chk_warn (__ptr, __glibc_objsize0 (__ptr), __size, __n,
a41fe4
-				 __stream);
a41fe4
-    }
a41fe4
-  return __fread_alias (__ptr, __size, __n, __stream);
a41fe4
+  size_t sz = __glibc_objsize0 (__ptr);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, __size, sz))
a41fe4
+    return __fread_alias (__ptr, __size, __n, __stream);
a41fe4
+  if (__glibc_unsafe_len (__n, __size, sz))
a41fe4
+    return __fread_chk_warn (__ptr, sz, __size, __n, __stream);
a41fe4
+  return __fread_chk (__ptr, sz, __size, __n, __stream);
a41fe4
 }
a41fe4
 
a41fe4
 #ifdef __USE_GNU
a41fe4
@@ -316,17 +306,12 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn,
a41fe4
 __fortify_function __wur char *
a41fe4
 fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__s) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n) || __n <= 0)
a41fe4
-	return __fgets_unlocked_chk (__s, __glibc_objsize (__s), __n,
a41fe4
-				     __stream);
a41fe4
-
a41fe4
-      if ((size_t) __n > __glibc_objsize (__s))
a41fe4
-	return __fgets_unlocked_chk_warn (__s, __glibc_objsize (__s), __n,
a41fe4
-					  __stream);
a41fe4
-    }
a41fe4
-  return __fgets_unlocked_alias (__s, __n, __stream);
a41fe4
+  size_t sz = __glibc_objsize (__s);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
a41fe4
+    return __fgets_unlocked_alias (__s, __n, __stream);
a41fe4
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
a41fe4
+    return __fgets_unlocked_chk_warn (__s, sz, __n, __stream);
a41fe4
+  return __fgets_unlocked_chk (__s, sz, __n, __stream);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
@@ -351,41 +336,36 @@ __fortify_function __wur size_t
a41fe4
 fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n,
a41fe4
 		FILE *__restrict __stream)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__ptr) != (size_t) -1)
a41fe4
+  size_t sz = __glibc_objsize0 (__ptr);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, __size, sz))
a41fe4
     {
a41fe4
-      if (!__builtin_constant_p (__size)
a41fe4
-	  || !__builtin_constant_p (__n)
a41fe4
-	  || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2)))
a41fe4
-	return __fread_unlocked_chk (__ptr, __glibc_objsize0 (__ptr), __size,
a41fe4
-				     __n, __stream);
a41fe4
-
a41fe4
-      if (__size * __n > __glibc_objsize0 (__ptr))
a41fe4
-	return __fread_unlocked_chk_warn (__ptr, __glibc_objsize0 (__ptr),
a41fe4
-					  __size, __n, __stream);
a41fe4
-    }
a41fe4
-
a41fe4
 # ifdef __USE_EXTERN_INLINES
a41fe4
-  if (__builtin_constant_p (__size)
a41fe4
-      && __builtin_constant_p (__n)
a41fe4
-      && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
a41fe4
-      && __size * __n <= 8)
a41fe4
-    {
a41fe4
-      size_t __cnt = __size * __n;
a41fe4
-      char *__cptr = (char *) __ptr;
a41fe4
-      if (__cnt == 0)
a41fe4
-	return 0;
a41fe4
-
a41fe4
-      for (; __cnt > 0; --__cnt)
a41fe4
+      if (__builtin_constant_p (__size)
a41fe4
+	  && __builtin_constant_p (__n)
a41fe4
+	  && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2))
a41fe4
+	  && __size * __n <= 8)
a41fe4
 	{
a41fe4
-	  int __c = getc_unlocked (__stream);
a41fe4
-	  if (__c == EOF)
a41fe4
-	    break;
a41fe4
-	  *__cptr++ = __c;
a41fe4
+	  size_t __cnt = __size * __n;
a41fe4
+	  char *__cptr = (char *) __ptr;
a41fe4
+	  if (__cnt == 0)
a41fe4
+	    return 0;
a41fe4
+
a41fe4
+	  for (; __cnt > 0; --__cnt)
a41fe4
+	    {
a41fe4
+	      int __c = getc_unlocked (__stream);
a41fe4
+	      if (__c == EOF)
a41fe4
+		break;
a41fe4
+	      *__cptr++ = __c;
a41fe4
+	    }
a41fe4
+	  return (__cptr - (char *) __ptr) / __size;
a41fe4
 	}
a41fe4
-      return (__cptr - (char *) __ptr) / __size;
a41fe4
-    }
a41fe4
 # endif
a41fe4
-  return __fread_unlocked_alias (__ptr, __size, __n, __stream);
a41fe4
+      return __fread_unlocked_alias (__ptr, __size, __n, __stream);
a41fe4
+    }
a41fe4
+  if (__glibc_unsafe_len (__n, __size, sz))
a41fe4
+    return __fread_unlocked_chk_warn (__ptr, sz, __size, __n, __stream);
a41fe4
+  return __fread_unlocked_chk (__ptr, sz, __size, __n, __stream);
a41fe4
+
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
a41fe4
index 1e39307b0ebcf38f..17b84a2e6c69d961 100644
a41fe4
--- a/misc/sys/cdefs.h
a41fe4
+++ b/misc/sys/cdefs.h
a41fe4
@@ -132,6 +132,53 @@
a41fe4
 # define __glibc_objsize(__o) __bos (__o)
a41fe4
 #endif
a41fe4
 
a41fe4
+/* Compile time conditions to choose between the regular, _chk and _chk_warn
a41fe4
+   variants.  These conditions should get evaluated to constant and optimized
a41fe4
+   away.  */
a41fe4
+
a41fe4
+#define __glibc_safe_len_cond(__l, __s, __osz) ((__l) <= (__osz) / (__s))
a41fe4
+#define __glibc_unsigned_or_positive(__l) \
a41fe4
+  ((__typeof (__l)) 0 < (__typeof (__l)) -1				      \
a41fe4
+   || (__builtin_constant_p (__l) && (__l) > 0))
a41fe4
+
a41fe4
+/* Length is known to be safe at compile time if the __L * __S <= __OBJSZ
a41fe4
+   condition can be folded to a constant and if it is true.  The -1 check is
a41fe4
+   redundant because since it implies that __glibc_safe_len_cond is true.  */
a41fe4
+#define __glibc_safe_or_unknown_len(__l, __s, __osz) \
a41fe4
+  (__glibc_unsigned_or_positive (__l)					      \
a41fe4
+   && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l),     \
a41fe4
+						   __s, __osz))		      \
a41fe4
+   && __glibc_safe_len_cond ((__SIZE_TYPE__) (__l), __s, __osz))
a41fe4
+
a41fe4
+/* Conversely, we know at compile time that the length is safe if the
a41fe4
+   __L * __S <= __OBJSZ condition can be folded to a constant and if it is
a41fe4
+   false.  */
a41fe4
+#define __glibc_unsafe_len(__l, __s, __osz) \
a41fe4
+  (__glibc_unsigned_or_positive (__l)					      \
a41fe4
+   && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l),     \
a41fe4
+						   __s, __osz))		      \
a41fe4
+   && !__glibc_safe_len_cond ((__SIZE_TYPE__) (__l), __s, __osz))
a41fe4
+
a41fe4
+/* Fortify function f.  __f_alias, __f_chk and __f_chk_warn must be
a41fe4
+   declared.  */
a41fe4
+
a41fe4
+#define __glibc_fortify(f, __l, __s, __osz, ...) \
a41fe4
+  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
a41fe4
+   ? __ ## f ## _alias (__VA_ARGS__)					      \
a41fe4
+   : (__glibc_unsafe_len (__l, __s, __osz)				      \
a41fe4
+      ? __ ## f ## _chk_warn (__VA_ARGS__, __osz)			      \
a41fe4
+      : __ ## f ## _chk (__VA_ARGS__, __osz)))			      \
a41fe4
+
a41fe4
+/* Fortify function f, where object size argument passed to f is the number of
a41fe4
+   elements and not total size.  */
a41fe4
+
a41fe4
+#define __glibc_fortify_n(f, __l, __s, __osz, ...) \
a41fe4
+  (__glibc_safe_or_unknown_len (__l, __s, __osz)			      \
a41fe4
+   ? __ ## f ## _alias (__VA_ARGS__)					      \
a41fe4
+   : (__glibc_unsafe_len (__l, __s, __osz)				      \
a41fe4
+      ? __ ## f ## _chk_warn (__VA_ARGS__, (__osz) / (__s))		      \
a41fe4
+      : __ ## f ## _chk (__VA_ARGS__, (__osz) / (__s))))		      \
a41fe4
+
a41fe4
 #if __GNUC_PREREQ (4,3)
a41fe4
 # define __warndecl(name, msg) \
a41fe4
   extern void name (void) __attribute__((__warning__ (msg)))
a41fe4
diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h
a41fe4
index a0c4dcfe9c61a7b8..a456d1723547db70 100644
a41fe4
--- a/posix/bits/unistd.h
a41fe4
+++ b/posix/bits/unistd.h
a41fe4
@@ -33,16 +33,9 @@ extern ssize_t __REDIRECT (__read_chk_warn,
a41fe4
 __fortify_function __wur ssize_t
a41fe4
 read (int __fd, void *__buf, size_t __nbytes)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__nbytes))
a41fe4
-	return __read_chk (__fd, __buf, __nbytes, __glibc_objsize0 (__buf));
a41fe4
-
a41fe4
-      if (__nbytes > __glibc_objsize0 (__buf))
a41fe4
-	return __read_chk_warn (__fd, __buf, __nbytes,
a41fe4
-				__glibc_objsize0 (__buf));
a41fe4
-    }
a41fe4
-  return __read_alias (__fd, __buf, __nbytes);
a41fe4
+  return __glibc_fortify (read, __nbytes, sizeof (char),
a41fe4
+			  __glibc_objsize0 (__buf),
a41fe4
+			  __fd, __buf, __nbytes);
a41fe4
 }
a41fe4
 
a41fe4
 #ifdef __USE_UNIX98
a41fe4
@@ -72,34 +65,17 @@ extern ssize_t __REDIRECT (__pread64_chk_warn,
a41fe4
 __fortify_function __wur ssize_t
a41fe4
 pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__nbytes))
a41fe4
-	return __pread_chk (__fd, __buf, __nbytes, __offset,
a41fe4
-			    __glibc_objsize0 (__buf));
a41fe4
-
a41fe4
-      if ( __nbytes > __glibc_objsize0 (__buf))
a41fe4
-	return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
a41fe4
-				 __glibc_objsize0 (__buf));
a41fe4
-    }
a41fe4
-  return __pread_alias (__fd, __buf, __nbytes, __offset);
a41fe4
+  return __glibc_fortify (pread, __nbytes, sizeof (char),
a41fe4
+			  __glibc_objsize0 (__buf),
a41fe4
+			  __fd, __buf, __nbytes, __offset);
a41fe4
 }
a41fe4
 # else
a41fe4
 __fortify_function __wur ssize_t
a41fe4
 pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__nbytes))
a41fe4
-	return __pread64_chk (__fd, __buf, __nbytes, __offset,
a41fe4
-			      __glibc_objsize0 (__buf));
a41fe4
-
a41fe4
-      if ( __nbytes > __glibc_objsize0 (__buf))
a41fe4
-	return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
a41fe4
-				   __glibc_objsize0 (__buf));
a41fe4
-    }
a41fe4
-
a41fe4
-  return __pread64_alias (__fd, __buf, __nbytes, __offset);
a41fe4
+  return __glibc_fortify (pread64, __nbytes, sizeof (char),
a41fe4
+			  __glibc_objsize0 (__buf),
a41fe4
+			  __fd, __buf, __nbytes, __offset);
a41fe4
 }
a41fe4
 # endif
a41fe4
 
a41fe4
@@ -107,18 +83,9 @@ pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
a41fe4
 __fortify_function __wur ssize_t
a41fe4
 pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__nbytes))
a41fe4
-	return __pread64_chk (__fd, __buf, __nbytes, __offset,
a41fe4
-			      __glibc_objsize0 (__buf));
a41fe4
-
a41fe4
-      if ( __nbytes > __glibc_objsize0 (__buf))
a41fe4
-	return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
a41fe4
-				   __glibc_objsize0 (__buf));
a41fe4
-    }
a41fe4
-
a41fe4
-  return __pread64_alias (__fd, __buf, __nbytes, __offset);
a41fe4
+  return __glibc_fortify (pread64, __nbytes, sizeof (char),
a41fe4
+			  __glibc_objsize0 (__buf),
a41fe4
+			  __fd, __buf, __nbytes, __offset);
a41fe4
 }
a41fe4
 # endif
a41fe4
 #endif
a41fe4
@@ -143,16 +110,9 @@ __fortify_function __nonnull ((1, 2)) __wur ssize_t
a41fe4
 __NTH (readlink (const char *__restrict __path, char *__restrict __buf,
a41fe4
 		 size_t __len))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __readlink_chk (__path, __buf, __len, __glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if ( __len > __glibc_objsize (__buf))
a41fe4
-	return __readlink_chk_warn (__path, __buf, __len,
a41fe4
-				    __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __readlink_alias (__path, __buf, __len);
a41fe4
+  return __glibc_fortify (readlink, __len, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __path, __buf, __len);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
@@ -178,17 +138,9 @@ __fortify_function __nonnull ((2, 3)) __wur ssize_t
a41fe4
 __NTH (readlinkat (int __fd, const char *__restrict __path,
a41fe4
 		   char *__restrict __buf, size_t __len))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __readlinkat_chk (__fd, __path, __buf, __len,
a41fe4
-				 __glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if (__len > __glibc_objsize (__buf))
a41fe4
-	return __readlinkat_chk_warn (__fd, __path, __buf, __len,
a41fe4
-				      __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __readlinkat_alias (__fd, __path, __buf, __len);
a41fe4
+  return __glibc_fortify (readlinkat, __len, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __fd, __path, __buf, __len);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
@@ -205,15 +157,9 @@ extern char *__REDIRECT_NTH (__getcwd_chk_warn,
a41fe4
 __fortify_function __wur char *
a41fe4
 __NTH (getcwd (char *__buf, size_t __size))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__size))
a41fe4
-	return __getcwd_chk (__buf, __size, __glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if (__size > __glibc_objsize (__buf))
a41fe4
-	return __getcwd_chk_warn (__buf, __size, __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __getcwd_alias (__buf, __size);
a41fe4
+  return __glibc_fortify (getcwd, __size, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __buf, __size);
a41fe4
 }
a41fe4
 
a41fe4
 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
a41fe4
@@ -245,16 +191,9 @@ extern size_t __REDIRECT_NTH (__confstr_chk_warn,
a41fe4
 __fortify_function size_t
a41fe4
 __NTH (confstr (int __name, char *__buf, size_t __len))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __confstr_chk (__name, __buf, __len, __glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if (__glibc_objsize (__buf) < __len)
a41fe4
-	return __confstr_chk_warn (__name, __buf, __len,
a41fe4
-				   __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __confstr_alias (__name, __buf, __len);
a41fe4
+  return __glibc_fortify (confstr, __len, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __name, __buf, __len);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -271,15 +210,9 @@ extern int __REDIRECT_NTH (__getgroups_chk_warn,
a41fe4
 __fortify_function int
a41fe4
 __NTH (getgroups (int __size, __gid_t __list[]))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__list) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__size) || __size < 0)
a41fe4
-	return __getgroups_chk (__size, __list, __glibc_objsize (__list));
a41fe4
-
a41fe4
-      if (__size * sizeof (__gid_t) > __glibc_objsize (__list))
a41fe4
-	return __getgroups_chk_warn (__size, __list, __glibc_objsize (__list));
a41fe4
-    }
a41fe4
-  return __getgroups_alias (__size, __list);
a41fe4
+  return __glibc_fortify (getgroups, __size, sizeof (__gid_t),
a41fe4
+			  __glibc_objsize (__list),
a41fe4
+			  __size, __list);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -297,17 +230,9 @@ extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
a41fe4
 __fortify_function int
a41fe4
 __NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__buflen))
a41fe4
-	return __ttyname_r_chk (__fd, __buf, __buflen,
a41fe4
-				__glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if (__buflen > __glibc_objsize (__buf))
a41fe4
-	return __ttyname_r_chk_warn (__fd, __buf, __buflen,
a41fe4
-				     __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __ttyname_r_alias (__fd, __buf, __buflen);
a41fe4
+  return __glibc_fortify (ttyname_r, __buflen, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __fd, __buf, __buflen);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -325,16 +250,9 @@ extern int __REDIRECT (__getlogin_r_chk_warn,
a41fe4
 __fortify_function int
a41fe4
 getlogin_r (char *__buf, size_t __buflen)
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__buflen))
a41fe4
-	return __getlogin_r_chk (__buf, __buflen, __glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if (__buflen > __glibc_objsize (__buf))
a41fe4
-	return __getlogin_r_chk_warn (__buf, __buflen,
a41fe4
-				      __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __getlogin_r_alias (__buf, __buflen);
a41fe4
+  return __glibc_fortify (getlogin_r, __buflen, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __buf, __buflen);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
@@ -353,16 +271,9 @@ extern int __REDIRECT_NTH (__gethostname_chk_warn,
a41fe4
 __fortify_function int
a41fe4
 __NTH (gethostname (char *__buf, size_t __buflen))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__buflen))
a41fe4
-	return __gethostname_chk (__buf, __buflen, __glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if (__buflen > __glibc_objsize (__buf))
a41fe4
-	return __gethostname_chk_warn (__buf, __buflen,
a41fe4
-				       __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __gethostname_alias (__buf, __buflen);
a41fe4
+  return __glibc_fortify (gethostname, __buflen, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __buf, __buflen);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
@@ -383,15 +294,8 @@ extern int __REDIRECT_NTH (__getdomainname_chk_warn,
a41fe4
 __fortify_function int
a41fe4
 __NTH (getdomainname (char *__buf, size_t __buflen))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__buflen))
a41fe4
-	return __getdomainname_chk (__buf, __buflen, __glibc_objsize (__buf));
a41fe4
-
a41fe4
-      if (__buflen > __glibc_objsize (__buf))
a41fe4
-	return __getdomainname_chk_warn (__buf, __buflen,
a41fe4
-					 __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __getdomainname_alias (__buf, __buflen);
a41fe4
+  return __glibc_fortify (getdomainname, __buflen, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __buf, __buflen);
a41fe4
 }
a41fe4
 #endif
a41fe4
diff --git a/socket/bits/socket2.h b/socket/bits/socket2.h
a41fe4
index 729e5a4cc1f4cb92..68fe5435b3b29c2a 100644
a41fe4
--- a/socket/bits/socket2.h
a41fe4
+++ b/socket/bits/socket2.h
a41fe4
@@ -33,17 +33,12 @@ extern ssize_t __REDIRECT (__recv_chk_warn,
a41fe4
 __fortify_function ssize_t
a41fe4
 recv (int __fd, void *__buf, size_t __n, int __flags)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __recv_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
a41fe4
-			   __flags);
a41fe4
-
a41fe4
-      if (__n > __glibc_objsize0 (__buf))
a41fe4
-	return __recv_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
a41fe4
-				__flags);
a41fe4
-    }
a41fe4
-  return __recv_alias (__fd, __buf, __n, __flags);
a41fe4
+  size_t sz = __glibc_objsize0 (__buf);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
a41fe4
+    return __recv_alias (__fd, __buf, __n, __flags);
a41fe4
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
a41fe4
+    return __recv_chk_warn (__fd, __buf, __n, sz, __flags);
a41fe4
+  return __recv_chk (__fd, __buf, __n, sz, __flags);
a41fe4
 }
a41fe4
 
a41fe4
 extern ssize_t __recvfrom_chk (int __fd, void *__restrict __buf, size_t __n,
a41fe4
@@ -66,14 +61,11 @@ __fortify_function ssize_t
a41fe4
 recvfrom (int __fd, void *__restrict __buf, size_t __n, int __flags,
a41fe4
 	  __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len)
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __recvfrom_chk (__fd, __buf, __n, __glibc_objsize0 (__buf),
a41fe4
-			       __flags, __addr, __addr_len);
a41fe4
-      if (__n > __glibc_objsize0 (__buf))
a41fe4
-	return __recvfrom_chk_warn (__fd, __buf, __n, __glibc_objsize0 (__buf),
a41fe4
-				    __flags, __addr, __addr_len);
a41fe4
-    }
a41fe4
-  return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
a41fe4
+  size_t sz = __glibc_objsize0 (__buf);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz))
a41fe4
+    return __recvfrom_alias (__fd, __buf, __n, __flags, __addr, __addr_len);
a41fe4
+  if (__glibc_unsafe_len (__n, sizeof (char), sz))
a41fe4
+    return __recvfrom_chk_warn (__fd, __buf, __n, sz, __flags, __addr,
a41fe4
+				__addr_len);
a41fe4
+  return __recvfrom_chk (__fd, __buf, __n, sz, __flags, __addr, __addr_len);
a41fe4
 }
a41fe4
diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h
a41fe4
index 5e4114ded33f2033..7ea364a276497720 100644
a41fe4
--- a/stdlib/bits/stdlib.h
a41fe4
+++ b/stdlib/bits/stdlib.h
a41fe4
@@ -36,17 +36,16 @@ extern char *__REDIRECT_NTH (__realpath_chk_warn,
a41fe4
 __fortify_function __wur char *
a41fe4
 __NTH (realpath (const char *__restrict __name, char *__restrict __resolved))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__resolved) != (size_t) -1)
a41fe4
-    {
a41fe4
+  size_t sz = __glibc_objsize (__resolved);
a41fe4
+
a41fe4
+  if (sz == (size_t) -1)
a41fe4
+    return __realpath_alias (__name, __resolved);
a41fe4
+
a41fe4
 #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
a41fe4
-      if (__glibc_objsize (__resolved) < PATH_MAX)
a41fe4
-	return __realpath_chk_warn (__name, __resolved,
a41fe4
-				    __glibc_objsize (__resolved));
a41fe4
+  if (__glibc_unsafe_len (sz, sizeof (char), PATH_MAX))
a41fe4
+    return __realpath_chk_warn (__name, __resolved, sz);
a41fe4
 #endif
a41fe4
-      return __realpath_chk (__name, __resolved, __glibc_objsize (__resolved));
a41fe4
-    }
a41fe4
-
a41fe4
-  return __realpath_alias (__name, __resolved);
a41fe4
+  return __realpath_chk (__name, __resolved, sz);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -64,16 +63,9 @@ extern int __REDIRECT_NTH (__ptsname_r_chk_warn,
a41fe4
 __fortify_function int
a41fe4
 __NTH (ptsname_r (int __fd, char *__buf, size_t __buflen))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__buf) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__buflen))
a41fe4
-	return __ptsname_r_chk (__fd, __buf, __buflen,
a41fe4
-				__glibc_objsize (__buf));
a41fe4
-      if (__buflen > __glibc_objsize (__buf))
a41fe4
-	return __ptsname_r_chk_warn (__fd, __buf, __buflen,
a41fe4
-				     __glibc_objsize (__buf));
a41fe4
-    }
a41fe4
-  return __ptsname_r_alias (__fd, __buf, __buflen);
a41fe4
+  return __glibc_fortify (ptsname_r, __buflen, sizeof (char),
a41fe4
+			  __glibc_objsize (__buf),
a41fe4
+			  __fd, __buf, __buflen);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -117,18 +109,9 @@ __fortify_function size_t
a41fe4
 __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src,
a41fe4
 		 size_t __len))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dst) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __mbstowcs_chk (__dst, __src, __len,
a41fe4
-			       __glibc_objsize (__dst) / sizeof (wchar_t));
a41fe4
-
a41fe4
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
a41fe4
-	return __mbstowcs_chk_warn (__dst, __src, __len,
a41fe4
-				    (__glibc_objsize (__dst)
a41fe4
-				     / sizeof (wchar_t)));
a41fe4
-    }
a41fe4
-  return __mbstowcs_alias (__dst, __src, __len);
a41fe4
+  return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize (__dst),
a41fe4
+			    __dst, __src, __len);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -149,13 +132,7 @@ __fortify_function size_t
a41fe4
 __NTH (wcstombs (char *__restrict __dst, const wchar_t *__restrict __src,
a41fe4
 		 size_t __len))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dst) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __wcstombs_chk (__dst, __src, __len, __glibc_objsize (__dst));
a41fe4
-      if (__len > __glibc_objsize (__dst))
a41fe4
-	return __wcstombs_chk_warn (__dst, __src, __len,
a41fe4
-				    __glibc_objsize (__dst));
a41fe4
-    }
a41fe4
-  return __wcstombs_alias (__dst, __src, __len);
a41fe4
+  return __glibc_fortify (wcstombs, __len, sizeof (char),
a41fe4
+			  __glibc_objsize (__dst),
a41fe4
+			  __dst, __src, __len);
a41fe4
 }
a41fe4
diff --git a/wcsmbs/bits/wchar2.h b/wcsmbs/bits/wchar2.h
a41fe4
index 838ba877ee4b4afe..f82bba481981e4fb 100644
a41fe4
--- a/wcsmbs/bits/wchar2.h
a41fe4
+++ b/wcsmbs/bits/wchar2.h
a41fe4
@@ -39,17 +39,9 @@ __fortify_function wchar_t *
a41fe4
 __NTH (wmemcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
a41fe4
 		size_t __n))
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __wmemcpy_chk (__s1, __s2, __n,
a41fe4
-			      __glibc_objsize0 (__s1) / sizeof (wchar_t));
a41fe4
-
a41fe4
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
a41fe4
-	return __wmemcpy_chk_warn (__s1, __s2, __n,
a41fe4
-				   __glibc_objsize0 (__s1) / sizeof (wchar_t));
a41fe4
-    }
a41fe4
-  return __wmemcpy_alias (__s1, __s2, __n);
a41fe4
+  return __glibc_fortify_n (wmemcpy, __n, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize0 (__s1),
a41fe4
+			    __s1, __s2, __n);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -67,18 +59,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemmove_chk_warn,
a41fe4
 __fortify_function wchar_t *
a41fe4
 __NTH (wmemmove (wchar_t *__s1, const wchar_t *__s2, size_t __n))
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __wmemmove_chk (__s1, __s2, __n,
a41fe4
-			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
a41fe4
-
a41fe4
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
a41fe4
-	return __wmemmove_chk_warn (__s1, __s2, __n,
a41fe4
-				    (__glibc_objsize0 (__s1)
a41fe4
-				     / sizeof (wchar_t)));
a41fe4
-    }
a41fe4
-  return __wmemmove_alias (__s1, __s2, __n);
a41fe4
+  return __glibc_fortify_n (wmemmove, __n, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize0 (__s1),
a41fe4
+			    __s1, __s2, __n);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -101,18 +84,9 @@ __fortify_function wchar_t *
a41fe4
 __NTH (wmempcpy (wchar_t *__restrict __s1, const wchar_t *__restrict __s2,
a41fe4
 		 size_t __n))
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__s1) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __wmempcpy_chk (__s1, __s2, __n,
a41fe4
-			       __glibc_objsize0 (__s1) / sizeof (wchar_t));
a41fe4
-
a41fe4
-      if (__n > __glibc_objsize0 (__s1) / sizeof (wchar_t))
a41fe4
-	return __wmempcpy_chk_warn (__s1, __s2, __n,
a41fe4
-				    (__glibc_objsize0 (__s1)
a41fe4
-				     / sizeof (wchar_t)));
a41fe4
-    }
a41fe4
-  return __wmempcpy_alias (__s1, __s2, __n);
a41fe4
+  return __glibc_fortify_n (wmempcpy, __n, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize0 (__s1),
a41fe4
+			    __s1, __s2, __n);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
@@ -130,17 +104,9 @@ extern wchar_t *__REDIRECT_NTH (__wmemset_chk_warn,
a41fe4
 __fortify_function wchar_t *
a41fe4
 __NTH (wmemset (wchar_t *__s, wchar_t __c, size_t __n))
a41fe4
 {
a41fe4
-  if (__glibc_objsize0 (__s) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __wmemset_chk (__s, __c, __n,
a41fe4
-			      __glibc_objsize0 (__s) / sizeof (wchar_t));
a41fe4
-
a41fe4
-      if (__n > __glibc_objsize0 (__s) / sizeof (wchar_t))
a41fe4
-	return __wmemset_chk_warn (__s, __c, __n,
a41fe4
-				   __glibc_objsize0 (__s) / sizeof (wchar_t));
a41fe4
-    }
a41fe4
-  return __wmemset_alias (__s, __c, __n);
a41fe4
+  return __glibc_fortify_n (wmemset, __n, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize0 (__s),
a41fe4
+			    __s, __c, __n);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -154,9 +120,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscpy_alias,
a41fe4
 __fortify_function wchar_t *
a41fe4
 __NTH (wcscpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dest) != (size_t) -1)
a41fe4
-    return __wcscpy_chk (__dest, __src,
a41fe4
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
a41fe4
+  size_t sz = __glibc_objsize (__dest);
a41fe4
+  if (sz != (size_t) -1)
a41fe4
+    return __wcscpy_chk (__dest, __src, sz / sizeof (wchar_t));
a41fe4
   return __wcscpy_alias (__dest, __src);
a41fe4
 }
a41fe4
 
a41fe4
@@ -171,9 +137,9 @@ extern wchar_t *__REDIRECT_NTH (__wcpcpy_alias,
a41fe4
 __fortify_function wchar_t *
a41fe4
 __NTH (wcpcpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dest) != (size_t) -1)
a41fe4
-    return __wcpcpy_chk (__dest, __src,
a41fe4
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
a41fe4
+  size_t sz = __glibc_objsize (__dest);
a41fe4
+  if (sz != (size_t) -1)
a41fe4
+    return __wcpcpy_chk (__dest, __src, sz / sizeof (wchar_t));
a41fe4
   return __wcpcpy_alias (__dest, __src);
a41fe4
 }
a41fe4
 
a41fe4
@@ -196,17 +162,9 @@ __fortify_function wchar_t *
a41fe4
 __NTH (wcsncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
a41fe4
 		size_t __n))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dest) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __wcsncpy_chk (__dest, __src, __n,
a41fe4
-			      __glibc_objsize (__dest) / sizeof (wchar_t));
a41fe4
-      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
a41fe4
-	return __wcsncpy_chk_warn (__dest, __src, __n,
a41fe4
-				   (__glibc_objsize (__dest)
a41fe4
-				    / sizeof (wchar_t)));
a41fe4
-    }
a41fe4
-  return __wcsncpy_alias (__dest, __src, __n);
a41fe4
+  return __glibc_fortify_n (wcsncpy, __n, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize (__dest),
a41fe4
+			    __dest, __src, __n);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -228,17 +186,9 @@ __fortify_function wchar_t *
a41fe4
 __NTH (wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
a41fe4
 		size_t __n))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dest) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n))
a41fe4
-	return __wcpncpy_chk (__dest, __src, __n,
a41fe4
-			      __glibc_objsize (__dest) / sizeof (wchar_t));
a41fe4
-      if (__n > __glibc_objsize (__dest) / sizeof (wchar_t))
a41fe4
-	return __wcpncpy_chk_warn (__dest, __src, __n,
a41fe4
-				   (__glibc_objsize (__dest)
a41fe4
-				    / sizeof (wchar_t)));
a41fe4
-    }
a41fe4
-  return __wcpncpy_alias (__dest, __src, __n);
a41fe4
+  return __glibc_fortify_n (wcpncpy, __n, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize (__dest),
a41fe4
+			    __dest, __src, __n);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -252,9 +202,9 @@ extern wchar_t *__REDIRECT_NTH (__wcscat_alias,
a41fe4
 __fortify_function wchar_t *
a41fe4
 __NTH (wcscat (wchar_t *__restrict __dest, const wchar_t *__restrict __src))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dest) != (size_t) -1)
a41fe4
-    return __wcscat_chk (__dest, __src,
a41fe4
-			 __glibc_objsize (__dest) / sizeof (wchar_t));
a41fe4
+  size_t sz = __glibc_objsize (__dest);
a41fe4
+  if (sz != (size_t) -1)
a41fe4
+    return __wcscat_chk (__dest, __src, sz / sizeof (wchar_t));
a41fe4
   return __wcscat_alias (__dest, __src);
a41fe4
 }
a41fe4
 
a41fe4
@@ -271,9 +221,9 @@ __fortify_function wchar_t *
a41fe4
 __NTH (wcsncat (wchar_t *__restrict __dest, const wchar_t *__restrict __src,
a41fe4
 		size_t __n))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dest) != (size_t) -1)
a41fe4
-    return __wcsncat_chk (__dest, __src, __n,
a41fe4
-			  __glibc_objsize (__dest) / sizeof (wchar_t));
a41fe4
+  size_t sz = __glibc_objsize (__dest);
a41fe4
+  if (sz != (size_t) -1)
a41fe4
+    return __wcsncat_chk (__dest, __src, __n, sz / sizeof (wchar_t));
a41fe4
   return __wcsncat_alias (__dest, __src, __n);
a41fe4
 }
a41fe4
 
a41fe4
@@ -293,10 +243,10 @@ __fortify_function int
a41fe4
 __NTH (swprintf (wchar_t *__restrict __s, size_t __n,
a41fe4
 		 const wchar_t *__restrict __fmt, ...))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
a41fe4
+  size_t sz = __glibc_objsize (__s);
a41fe4
+  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
a41fe4
     return __swprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
a41fe4
-			   __glibc_objsize (__s) / sizeof (wchar_t),
a41fe4
-			   __fmt, __va_arg_pack ());
a41fe4
+			   sz / sizeof (wchar_t), __fmt, __va_arg_pack ());
a41fe4
   return __swprintf_alias (__s, __n, __fmt, __va_arg_pack ());
a41fe4
 }
a41fe4
 #elif !defined __cplusplus
a41fe4
@@ -323,10 +273,10 @@ __fortify_function int
a41fe4
 __NTH (vswprintf (wchar_t *__restrict __s, size_t __n,
a41fe4
 		  const wchar_t *__restrict __fmt, __gnuc_va_list __ap))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__s) != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
a41fe4
+  size_t sz = __glibc_objsize (__s);
a41fe4
+  if (sz != (size_t) -1 || __USE_FORTIFY_LEVEL > 1)
a41fe4
     return __vswprintf_chk (__s, __n,  __USE_FORTIFY_LEVEL - 1,
a41fe4
-			    __glibc_objsize (__s) / sizeof (wchar_t), __fmt,
a41fe4
-			    __ap);
a41fe4
+			    sz / sizeof (wchar_t), __fmt, __ap);
a41fe4
   return __vswprintf_alias (__s, __n, __fmt, __ap);
a41fe4
 }
a41fe4
 
a41fe4
@@ -392,18 +342,12 @@ extern wchar_t *__REDIRECT (__fgetws_chk_warn,
a41fe4
 __fortify_function __wur wchar_t *
a41fe4
 fgetws (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__s) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n) || __n <= 0)
a41fe4
-	return __fgetws_chk (__s, __glibc_objsize (__s) / sizeof (wchar_t),
a41fe4
-			     __n, __stream);
a41fe4
-
a41fe4
-      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
a41fe4
-	return __fgetws_chk_warn (__s,
a41fe4
-				  __glibc_objsize (__s) / sizeof (wchar_t),
a41fe4
-				  __n, __stream);
a41fe4
-    }
a41fe4
-  return __fgetws_alias (__s, __n, __stream);
a41fe4
+  size_t sz = __glibc_objsize (__s);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
a41fe4
+    return __fgetws_alias (__s, __n, __stream);
a41fe4
+  if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
a41fe4
+    return __fgetws_chk_warn (__s, sz / sizeof (wchar_t), __n, __stream);
a41fe4
+  return __fgetws_chk (__s, sz / sizeof (wchar_t), __n, __stream);
a41fe4
 }
a41fe4
 
a41fe4
 #ifdef __USE_GNU
a41fe4
@@ -424,20 +368,13 @@ extern wchar_t *__REDIRECT (__fgetws_unlocked_chk_warn,
a41fe4
 __fortify_function __wur wchar_t *
a41fe4
 fgetws_unlocked (wchar_t *__restrict __s, int __n, __FILE *__restrict __stream)
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__s) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__n) || __n <= 0)
a41fe4
-	return __fgetws_unlocked_chk (__s,
a41fe4
-				      __glibc_objsize (__s) / sizeof (wchar_t),
a41fe4
-				      __n, __stream);
a41fe4
-
a41fe4
-      if ((size_t) __n > __glibc_objsize (__s) / sizeof (wchar_t))
a41fe4
-	return __fgetws_unlocked_chk_warn (__s,
a41fe4
-					   (__glibc_objsize (__s)
a41fe4
-					    / sizeof (wchar_t)),
a41fe4
-					   __n, __stream);
a41fe4
-    }
a41fe4
-  return __fgetws_unlocked_alias (__s, __n, __stream);
a41fe4
+  size_t sz = __glibc_objsize (__s);
a41fe4
+  if (__glibc_safe_or_unknown_len (__n, sizeof (wchar_t), sz))
a41fe4
+    return __fgetws_unlocked_alias (__s, __n, __stream);
a41fe4
+  if (__glibc_unsafe_len (__n, sizeof (wchar_t), sz))
a41fe4
+    return __fgetws_unlocked_chk_warn (__s, sz / sizeof (wchar_t), __n,
a41fe4
+				       __stream);
a41fe4
+  return __fgetws_unlocked_chk (__s, sz / sizeof (wchar_t), __n, __stream);
a41fe4
 }
a41fe4
 #endif
a41fe4
 
a41fe4
@@ -488,18 +425,9 @@ __fortify_function size_t
a41fe4
 __NTH (mbsrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
a41fe4
 		  size_t __len, mbstate_t *__restrict __ps))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dst) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __mbsrtowcs_chk (__dst, __src, __len, __ps,
a41fe4
-				__glibc_objsize (__dst) / sizeof (wchar_t));
a41fe4
-
a41fe4
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
a41fe4
-	return __mbsrtowcs_chk_warn (__dst, __src, __len, __ps,
a41fe4
-				     (__glibc_objsize (__dst)
a41fe4
-				      / sizeof (wchar_t)));
a41fe4
-    }
a41fe4
-  return __mbsrtowcs_alias (__dst, __src, __len, __ps);
a41fe4
+  return __glibc_fortify_n (mbsrtowcs, __len, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize (__dst),
a41fe4
+			    __dst, __src, __len, __ps);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -523,17 +451,9 @@ __fortify_function size_t
a41fe4
 __NTH (wcsrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
a41fe4
 		  size_t __len, mbstate_t *__restrict __ps))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dst) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __wcsrtombs_chk (__dst, __src, __len, __ps,
a41fe4
-				__glibc_objsize (__dst));
a41fe4
-
a41fe4
-      if (__len > __glibc_objsize (__dst))
a41fe4
-	return __wcsrtombs_chk_warn (__dst, __src, __len, __ps,
a41fe4
-				     __glibc_objsize (__dst));
a41fe4
-    }
a41fe4
-  return __wcsrtombs_alias (__dst, __src, __len, __ps);
a41fe4
+  return __glibc_fortify (wcsrtombs, __len, sizeof (char),
a41fe4
+			  __glibc_objsize (__dst),
a41fe4
+			  __dst, __src, __len, __ps);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -559,18 +479,9 @@ __fortify_function size_t
a41fe4
 __NTH (mbsnrtowcs (wchar_t *__restrict __dst, const char **__restrict __src,
a41fe4
 		   size_t __nmc, size_t __len, mbstate_t *__restrict __ps))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dst) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __mbsnrtowcs_chk (__dst, __src, __nmc, __len, __ps,
a41fe4
-				 __glibc_objsize (__dst) / sizeof (wchar_t));
a41fe4
-
a41fe4
-      if (__len > __glibc_objsize (__dst) / sizeof (wchar_t))
a41fe4
-	return __mbsnrtowcs_chk_warn (__dst, __src, __nmc, __len, __ps,
a41fe4
-				      (__glibc_objsize (__dst)
a41fe4
-				       / sizeof (wchar_t)));
a41fe4
-    }
a41fe4
-  return __mbsnrtowcs_alias (__dst, __src, __nmc, __len, __ps);
a41fe4
+  return __glibc_fortify_n (mbsnrtowcs, __len, sizeof (wchar_t),
a41fe4
+			    __glibc_objsize (__dst),
a41fe4
+			    __dst, __src, __nmc, __len, __ps);
a41fe4
 }
a41fe4
 
a41fe4
 
a41fe4
@@ -596,16 +507,8 @@ __fortify_function size_t
a41fe4
 __NTH (wcsnrtombs (char *__restrict __dst, const wchar_t **__restrict __src,
a41fe4
 		   size_t __nwc, size_t __len, mbstate_t *__restrict __ps))
a41fe4
 {
a41fe4
-  if (__glibc_objsize (__dst) != (size_t) -1)
a41fe4
-    {
a41fe4
-      if (!__builtin_constant_p (__len))
a41fe4
-	return __wcsnrtombs_chk (__dst, __src, __nwc, __len, __ps,
a41fe4
-				 __glibc_objsize (__dst));
a41fe4
-
a41fe4
-      if (__len > __glibc_objsize (__dst))
a41fe4
-	return __wcsnrtombs_chk_warn (__dst, __src, __nwc, __len, __ps,
a41fe4
-				      __glibc_objsize (__dst));
a41fe4
-    }
a41fe4
-  return __wcsnrtombs_alias (__dst, __src, __nwc, __len, __ps);
a41fe4
+  return __glibc_fortify (wcsnrtombs, __len, sizeof (char),
a41fe4
+			  __glibc_objsize (__dst),
a41fe4
+			  __dst, __src, __nwc, __len, __ps);
a41fe4
 }
a41fe4
 #endif