https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98465

--- Comment #3 from Romain Geissler <romain.geissler at amadeus dot com> ---
I have found another example in my code base raising this error:

#include <string>

std::string f1()
{
    std::string aString = "string";
    aString = "bigger str";

    return aString;
}

With: -O2 -std=gnu++20 -Werror=stringop-overread -Wno-system-headers -g
raises:

/opt/compiler-explorer/gcc-trunk-20201230/include/c++/11.0.0/bits/char_traits.h:402:56:
error: 'void* __builtin_memcpy(void*, const void*, long unsigned int)' reading
10 bytes from a region of size 7 [-Werror=stringop-overread]
  402 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2,
__n));
      |                                       
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

Yet now what confuses me is that if I add copy of this function later using
bigger strings (so not exposing the bug), the new f2 function makes the warning
disappear form the f1 function:

#include <string>

std::string f1()
{
    std::string aString = "string";
    aString = "bigger str"; // <--- no more warning here when we have function
f2 !

    return aString;
}

std::string f2()
{
    std::string aString = "initial string with enough capacity";
    aString = "smaller str";

    return aString;
}

Why does having a function f2 affects warnings in function f1 ?

Reply via email to