Description of problem: At -O3, g++ generates a lot of code and warnings for a loop that copies a variable amount of data to a small array in a class member, which I speculate is because it is optimising expecting to copy more than fits in the array. Version-Release number of selected component (if applicable): gcc-10.1.1-1.fc32, x86_64 How reproducible: 100% Steps to Reproduce: Compile this with g++ -c -O3: struct A { char v[8]; }; void f(A & a, char * s, int c) { for (int i = 0; i < c; ++i) a.v[i] = s[i]; } Then inspect the results with objdump --disassemble Actual results: Several warnings of the form: test.cc: In function ‘void f(A&, char*, int)’: test.cc:9:10: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=] 9 | a.v[i] = s[i]; | ~~~~~~~^~~~~~ test.cc:3:7: note: at offset [8, 2147483640] to object ‘A::v’ with size 8 declared here 3 | char v[8]; | ^ objdump shows quite a lot of code for function f, a lot of which appears to handle the case where the c parameter is greater than 8. Expected results: No warnings about an overflow, and little or no code to handle c > 8, as that would be undefined behaviour. It would be nice if GCC still warned if a function like f was called with a value of c that was a compile time constant > 8 however. Additional info: GCC 9.3 didn't generate the warnings, and had smaller code size for function f (but it still appears to handle values of c greater than 8). GCC 10.1 generates reasonable code and no warnings if the array is in a global variable. In that case, there is a an -Waggressive-loop-optimizations warning if the function is called with a compile time constant value of c which is large enough to cause an out of bounds write.
At -O3 GCC unrolls the loop and, because the member array is last in the enclosing struct, makes the "conservative" assumption that the array could be bigger than its bound implies. This conservatism then backfires by enabling this dubious optimization. (It doesn't make that assumption for interior arrays.) The warning is then issued for the unrolled assignments past the array size. Unlike the optimizer, the warning doesn't consider valid past the end accesses to trailing arrays of size of more than one element. The optimizer should respect array bounds even for trailing arrays with more than zero (or one) element and avoid loop unrolling past it. Accesses to those are not documented as supported (https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html) and the warning was put in place to help enforce that.
I opened https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95140 in GCC Bugzilla. As a possible workaround, replacing the loop with memcpy prevents the unrolling and avoids the warning.
This message is a reminder that Fedora 32 is nearing its end of life. Fedora will stop maintaining and issuing updates for Fedora 32 on 2021-05-25. It is Fedora's policy to close all bug reports from releases that are no longer maintained. At that time this bug will be closed as EOL if it remains open with a Fedora 'version' of '32'. Package Maintainer: If you wish for this bug to remain open because you plan to fix it in a currently maintained version, simply change the 'version' to a later Fedora version. Thank you for reporting this issue and we are sorry that we were not able to fix it before Fedora 32 is end of life. If you would still like to see this bug fixed and are able to reproduce it against a later version of Fedora, you are encouraged change the 'version' to a later Fedora version prior this bug is closed as described in the policy above. Although we aim to fix as many bugs as possible during every release's lifetime, sometimes those efforts are overtaken by events. Often a more recent Fedora release includes newer upstream software that fixes bugs or makes them obsolete.
Fedora 32 changed to end-of-life (EOL) status on 2021-05-25. Fedora 32 is no longer maintained, which means that it will not receive any further security or bug fix updates. As a result we are closing this bug. If you can reproduce this bug against a currently maintained version of Fedora please feel free to reopen this bug against that version. If you are unable to reopen this bug, please file a new report against the current release. If you experience problems, please add a comment to this bug. Thank you for reporting this bug and we are sorry it could not be fixed.