1

Been struggling with a false warning on switching from GCC 10.2 to GCC 11 and I'm looking for a workaround.

        char* const dest = data_.data();
        if (src.length() > max_chars)
        {
            // does not fit, copy first max_chars (truncation occurs)
            std::memcpy(dest, src.data(), max_chars);

In the above code src is a std::string_view and dest is a std::array of static size of precisely max_chars. The memcpy line generates the warning (with -O3):

array subscript ‘unsigned char [81][1]’ is partly outside array bounds of ‘myns::TrivialString<81> [1]’ [-Werror=array-bounds]

though there are no subscripts directly involved in the code. I'm aware of new capability to detect accesses with are "partly" out of bounds in newer GCC versions, and the issue is most similar to this bug, though I see there are a few bugs filed with similar symptoms.

haelix
  • 4,245
  • 4
  • 34
  • 56

1 Answers1

2

A workaround for me was to replace

std::memcpy(dest, src.data(), max_chars);

with

std::copy_n(src.data(), max_chars, dest);

The workaround comes with no readability or performance degradation.

haelix
  • 4,245
  • 4
  • 34
  • 56