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.

3
  • 3
    please show a minimal reproducible example Commented May 18, 2021 at 10:15
  • coming in a bit.. Commented May 18, 2021 at 10:53
  • as feared, I can't obtain a small example using Godbolt. This works: godbolt.org/z/dshT1qxcz Commented May 18, 2021 at 12:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.