Bug 105723 - [12 Regression] Optimization false positive warning (-Wstringop-overflow)
Summary: [12 Regression] Optimization false positive warning (-Wstringop-overflow)
Status: RESOLVED DUPLICATE of bug 107852
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.1.1
: P3 normal
Target Milestone: 12.3
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, needs-bisection
Depends on:
Blocks: Wstringop-overflow
  Show dependency treegraph
 
Reported: 2022-05-24 20:22 UTC by Jeffrey Reynolds
Modified: 2023-05-01 13:40 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 13.0
Known to fail:
Last reconfirmed: 2023-02-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jeffrey Reynolds 2022-05-24 20:22:11 UTC
The following code seems to be a regression, or an introduction of a bug.

```
#include <algorithm>
#include <cstdint>
#include <vector>

void some_func(const uint8_t* const& data) {
    std::vector<uint8_t> vec(data, data + 10);

    auto vecend = std::find_if(vec.rbegin(), vec.rend(), [](uint8_t x) {
        return x > 9;
    });

    if (vec.rend() != vecend) {
        vec = std::vector<uint8_t>();
    } else {
        std::transform(vec.rbegin(), vec.rend(), vec.rbegin(),
                       [](const uint8_t b) {
                           return (((b >> 4) & 0xf) | ((b & 0xf) << 4));
                       });
    }
}
```
https://godbolt.org/z/avzPEWKe7
This will cause a warning under GCC 12 (hard fail on my project)
I've checked previous versions of GCC along with several other compilers. All compile it just fine.
There is nothing in this code that i can see that should be triggering a stringop-overflow warning.
If the arch flag is remove, or the optimization turned down to O2 the code will compile.
If you place `vec.resize(vec.size);` before the std::transform, which does nothing since the size is neither less than nor greater than the current size, the code will successfully compile. This last fact, along with the other facts, indicates to me that it is indeed a bug.
Comment 1 Richard Biener 2022-05-25 07:34:20 UTC
In file included from /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/algorithm:61,
                 from <source>:1:
In function '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = reverse_iterator<__gnu_cxx::__normal_iterator<unsigned char*, vector<unsigned char> > >; _OIter = reverse_iterator<__gnu_cxx::__normal_iterator<unsigned char*, vector<unsigned char> > >; _UnaryOperation = some_func(const uint8_t* const&)::<lambda(uint8_t)>]',
    inlined from 'void some_func(const uint8_t* const&)' at <source>:15:23:
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/stl_algo.h:4263:19: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
 4263 |         *__result = __unary_op(*__first);
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
compilation terminated due to -Wfatal-errors.
cc1plus: some warnings being treated as errors
Compiler returned: 1
Comment 2 Richard Biener 2023-02-21 14:31:42 UTC
Confirmed with GCC 12 but it seems that GCC 13 is no longer affected, possibly due to standard library changes.
Comment 3 Richard Biener 2023-04-27 11:33:11 UTC
Needs bisection as to what fixed it on trunk.
Comment 4 Sam James 2023-05-01 09:54:41 UTC
(In reply to Richard Biener from comment #3)
> Needs bisection as to what fixed it on trunk.

Inverse bisect says:

fd8dd6c0384969170e594be34da278a072d5eb76 is the first bad commit
commit fd8dd6c0384969170e594be34da278a072d5eb76
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Nov 29 12:56:22 2022 +0100

    tree-optimization/107852 - missed optimization with PHIs

i.e. r13-4389-gfd8dd6c0384969. It doesn't revert cleanly on trunk so I can't test if it's sane or not.
Comment 5 Andrew Pinski 2023-05-01 13:12:22 UTC
Dup of bug 107852 then.

*** This bug has been marked as a duplicate of bug 107852 ***
Comment 6 Sam James 2023-05-01 13:40:36 UTC
(In reply to Andrew Pinski from comment #5)
> Dup of bug 107852 then.
> 
> *** This bug has been marked as a duplicate of bug 107852 ***

.. would've helped if i'd checked the bug referenced, ha. thank you!