@@ -616,21 +616,33 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
616616 return false;
617617}
618618
619+ /*
620+ * To work around what seems to be an optimizer bug, the macro arguments
621+ * need to have const copies or the values end up changed by the time they
622+ * reach fortify_warn_once(). See commit 6f7630b1b5bc ("fortify: Capture
623+ * __bos() results in const temp vars") for more details.
624+ */
619625#define __fortify_memcpy_chk (p , q , size , p_size , q_size , \
620626 p_size_field , q_size_field , op ) ({ \
621627 const size_t __fortify_size = (size_t)(size); \
622628 const size_t __p_size = (p_size); \
623629 const size_t __q_size = (q_size); \
624630 const size_t __p_size_field = (p_size_field); \
625631 const size_t __q_size_field = (q_size_field); \
632+ /* Keep a mutable version of the size for the final copy. */ \
633+ size_t __copy_size = __fortify_size ; \
626634 fortify_warn_once (fortify_memcpy_chk (__fortify_size , __p_size , \
627635 __q_size , __p_size_field , \
628636 __q_size_field , FORTIFY_FUNC_ ##op ), \
629637 #op ": detected field-spanning write (size %zu) of single %s (size %zu)\n", \
630638 __fortify_size, \
631639 "field \"" #p "\" at " FILE_LINE, \
632640 __p_size_field); \
633- __underlying_##op(p, q, __fortify_size); \
641+ /* Hide only the run-time size from value range tracking to */ \
642+ /* silence compile-time false positive bounds warnings. */ \
643+ if (!__builtin_constant_p (__copy_size )) \
644+ OPTIMIZER_HIDE_VAR (__copy_size ); \
645+ __underlying_ ##op (p, q, __copy_size); \
634646})
635647
636648/*
0 commit comments