Re: Wrong -Wstringop-overflow warning for std::array of vectors?

Jonathan Wakely via Gcc-help <[email protected]> Fri, 13 Feb 2026 15:25:59 +0000
Newsgroups gmane.comp.gcc.help
Message-ID <CAH6eHdTmtku0J+2xVpKQCzfJQMqcfHXNfg1zLQX+3n4wviw=yQ@mail.gmail.com>
On Fri, 13 Feb 2026 at 12:52, Georg-Johann Lay via Gcc-help
<[email protected]> wrote:
>
> I am getting warnings like
>
> main.cpp:45:15: warning: writing 1 byte into a region of size 0
> [-Wstringop-overflow=]
>     45 |         v_[i] = (Point::value_t) val;
>        |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
>
>
> and am unsure whether it is a known PR or worth reporting as PR.

There are dozens and dozens of similar bugs for false positive middle
end warnings (-Wstringop-overflow, -Warray-bounds etc.)

Those warnings are broken by design and give scary-looking warnings
about unreachable code paths. Feel free to report it, it's probably a
dup of another one (and nobody seems to have a plan to fix them).

>
> Attached is as C++17 test case that's compiled like:
>
> $ g++ -c main.cpp -std=c++17 -fno-exceptions -W -Wall -O3 -fmax-errors=1
>   -o main.obj
>
> In member function 'void Point::set(int, int)',
>      inlined from 'void Cluster::shift(int, int)' at main.cpp:135:19,
>      inlined from 'void Cluster::add(Point, bool)' at main.cpp:128:23,
>      inlined from 'Cluster::Cluster(const Cluster*, Point)' at
> main.cpp:81:13,
>      inlined from 'int main()' at main.cpp:201:61:
> main.cpp:45:15: warning: writing 1 byte into a region of size 0
> [-Wstringop-overflow=]
>     45 |         v_[i] = (Point::value_t) val;
>        |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
> main.cpp: In function 'int main()':
> main.cpp:201:61: note: at offset 8 into destination object '<anonymous>'
> of size 8
>    201 |                 set[i].emplace (Cluster (&cs, Point::rand ()));
>        |                                                             ^
>
> and more similar warnings.
>
> I see these warnings with g++ 14 and also with current trunk from today
> on x86_64 gnu/linux.
> The test case is not preprocessed and includes only std headers.
> When a pre-processed test case is needed I can provide one.
>
> The test case defines a struct Point that's a vector type with 2 int8_t
> components. It then defines a struct Cluster that's a std::array<Point,
> 1+CELLS> that can hold 0...CELLS Points.  The number of Points in the
> Cluster is stored in the last array element.
>
> Some observations:
>
> * I only see the warnings with -O3, with lower optimizations it goes away.
>
> * The warning goes away when I add -fsanitize=address.
>
> * The main function iterated over a std::set<Cluster>.  When I adjust
> the code to iterate over std::vector<Cluster> then the warning
> also goes away.
>
>
> best regards
>
> Johann
>