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