076f82
commit b72bbba23687ed67887d1d18c51cce5cc9c575ca
076f82
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
076f82
Date:   Fri May 13 10:01:47 2022 +0530
076f82
076f82
    fortify: Ensure that __glibc_fortify condition is a constant [BZ #29141]
076f82
    
076f82
    The fix c8ee1c85 introduced a -1 check for object size without also
076f82
    checking that object size is a constant.  Because of this, the tree
076f82
    optimizer passes in gcc fail to fold away one of the branches in
076f82
    __glibc_fortify and trips on a spurious Wstringop-overflow.  The warning
076f82
    itself is incorrect and the branch does go away eventually in DCE in the
076f82
    rtl passes in gcc, but the constant check is a helpful hint to simplify
076f82
    code early, so add it in.
076f82
    
076f82
    Resolves: BZ #29141
076f82
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
076f82
    (cherry picked from commit 61a87530108ec9181e1b18a9b727ec3cc3ba7532)
076f82
076f82
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
076f82
index b36013b9a6b4d9c3..e0ecd9147ee3ce48 100644
076f82
--- a/misc/sys/cdefs.h
076f82
+++ b/misc/sys/cdefs.h
076f82
@@ -163,7 +163,7 @@
076f82
 /* Length is known to be safe at compile time if the __L * __S <= __OBJSZ
076f82
    condition can be folded to a constant and if it is true, or unknown (-1) */
076f82
 #define __glibc_safe_or_unknown_len(__l, __s, __osz) \
076f82
-  ((__osz) == (__SIZE_TYPE__) -1					      \
076f82
+  ((__builtin_constant_p (__osz) && (__osz) == (__SIZE_TYPE__) -1)	      \
076f82
    || (__glibc_unsigned_or_positive (__l)				      \
076f82
        && __builtin_constant_p (__glibc_safe_len_cond ((__SIZE_TYPE__) (__l), \
076f82
 						       (__s), (__osz)))	      \