]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix undefined behaviour in profile_count::differs_from_p
authorJan Hubicka <jh@suse.cz>
Thu, 10 Aug 2023 16:35:13 +0000 (18:35 +0200)
committerJan Hubicka <jh@suse.cz>
Thu, 10 Aug 2023 16:35:13 +0000 (18:35 +0200)
This patch avoid overflow in profile_count::differs_from_p and also makes it to
return false from one of the values is undefined while other is defined.

gcc/ChangeLog:

* profile-count.cc (profile_count::differs_from_p): Fix overflow and
handling of undefined values.

gcc/profile-count.cc

index e63c9432388b0c827341890632a4c87edaa04f19..a14f379db8fa40f548f142101eb30dd3f99472a8 100644 (file)
@@ -128,13 +128,14 @@ profile_count::differs_from_p (profile_count other) const
 {
   gcc_checking_assert (compatible_p (other));
   if (!initialized_p () || !other.initialized_p ())
-    return false;
+    return initialized_p () != other.initialized_p ();
   if ((uint64_t)m_val - (uint64_t)other.m_val < 100
       || (uint64_t)other.m_val - (uint64_t)m_val < 100)
     return false;
   if (!other.m_val)
     return true;
-  int64_t ratio = (int64_t)m_val * 100 / other.m_val;
+  uint64_t ratio;
+  safe_scale_64bit (m_val, 100, other.m_val, &ratio);
   return ratio < 99 || ratio > 101;
 }