https://gcc.gnu.org/g:994feb9edd8a243283d5996667f89351c2c66e26
commit r16-2646-g994feb9edd8a243283d5996667f89351c2c66e26 Author: Jan Hubicka <j...@suse.cz> Date: Wed Jul 30 16:09:12 2025 +0200 Report read errors when reading auto-profile 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. 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: --- gcc/auto-profile.cc | 12 +++++++++++- gcc/gcov-io.cc | 2 +- gcc/gcov-io.h | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc index 53e7b51e8832..7ff952632c35 100644 --- a/gcc/auto-profile.cc +++ b/gcc/auto-profile.cc @@ -885,6 +885,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 +2749,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 f39b4bd565ad..dd3fc88f4b51 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 diff --git a/gcc/gcov-io.h b/gcc/gcov-io.h index f3e3a1c08da8..313c15cecbb8 100644 --- a/gcc/gcov-io.h +++ b/gcc/gcov-io.h @@ -387,6 +387,7 @@ char *mangle_path (char const *base); /* Available outside gcov */ GCOV_LINKAGE void gcov_write (const void *, unsigned) ATTRIBUTE_HIDDEN; GCOV_LINKAGE void gcov_write_unsigned (gcov_unsigned_t) ATTRIBUTE_HIDDEN; +GCOV_LINKAGE int gcov_is_error (void); #endif #if !IN_GCOV && !IN_LIBGCOV