Hi,
currently -fauto-profile will happily read truncated file without any warning
and interpret it as a zero profile which will in turn result in slow code.
This patch exports gcov_is_error and adds checks so truncated files are 
detected.

Bootstrapped/regtesed x86_64-linux, comitted.

gcc/ChangeLog:

        * auto-profile.cc (string_table::read): Check gcov_is_error.
        (read_profile): Likewise.
        * gcov-io.cc (gcov_is_error): Export for gcc linkage.
        * gcov-io.h (gcov_is_error): Declare.

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index 53e7b51e883..f8deeb7fab7 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -885,6 +923,8 @@ string_table::read ()
     {
       vector_.quick_push (xstrdup (gcov_read_string ()));
       map_[vector_.last ()] = i;
+      if (gcov_is_error ())
+       return false;
     }
   return true;
 }
@@ -2747,14 +2847,22 @@ read_profile (void)
 
   /* autofdo_source_profile.  */
   afdo_source_profile = autofdo_source_profile::create ();
-  if (afdo_source_profile == NULL)
+  if (afdo_source_profile == NULL
+      || gcov_is_error ())
     {
       error ("cannot read function profile from %s", auto_profile_file);
+      delete afdo_source_profile;
+      afdo_source_profile = NULL;
       return;
     }
 
   /* autofdo_module_profile.  */
   fake_read_autofdo_module_profile ();
+  if (gcov_is_error ())
+    {
+      error ("cannot read module profile from %s", auto_profile_file);
+      return;
+    }
 }
 
 /* From AutoFDO profiles, find values inside STMT for that we want to measure
diff --git a/gcc/gcov-io.cc b/gcc/gcov-io.cc
index f39b4bd565a..dd3fc88f4b5 100644
--- a/gcc/gcov-io.cc
+++ b/gcc/gcov-io.cc
@@ -69,7 +69,7 @@ gcov_position (void)
 
 /* Return nonzero if the error flag is set.  */
 /* We need to expose this function when compiling for gcov-tool.  */
-#ifndef IN_GCOV_TOOL
+#if !defined (IN_GCOV_TOOL) && !defined (IN_GCC)
 static inline
 #endif
 int

Reply via email to