https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121487

            Bug ID: 121487
           Summary: False positive -Warray-bounds (triggered via
                    std::ranges::copy and
                    std::string::resize_and_overwrite)
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: diagnostics
          Assignee: dmalcolm at redhat dot com
          Reporter: vermaelen.wouter at gmail dot com
  Target Milestone: ---

Created attachment 62089
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62089&action=edit
test case to reproduce the -Warray-bounds warning

Hi,

The attached test case triggers a false positive -Warray-bounds warning (full
output below).

Compile with:
  gcc-snapshot/bin/g++ -O2 -std=c++23 -Wall test.cpp

I've used a gcc snapshot from today (aug 9, 2025) git revision: 3e3f452a9355446

I'm fairly sure the code is correct. Also, compiling/running the test case
(extended with a main() function) with -fsanitizer=address or with valgrind
doesn't trigger any memory errors.


In file included from
/home/wouter/gcc-snapshot/include/c++/16.0.0/bits/ranges_algo.h:39,
                 from
/home/wouter/gcc-snapshot/include/c++/16.0.0/algorithm:65,
                 from /home/wouter/test.cpp:1:
In function ‘constexpr std::__conditional_t<_IsMove,
std::ranges::in_out_result<_Iter, _Out>, std::ranges::in_out_result<_Iter,
_Out> > std::ranges::__copy_or_move(_Iter, _Sent, _Out) [with bool _IsMove =
false; _Iter = const char*; _Sent = const char*; _Out = char*]’,
    inlined from ‘constexpr std::ranges::copy_result<_Iter, _Out>
std::ranges::__copy_fn::operator()(_Iter, _Sent, _Out) const [with _Iter =
const char*; _Sent = const char*; _Out = char*]’ at
/home/wouter/gcc-snapshot/include/c++/16.0.0/bits/ranges_algobase.h:341:38,
    inlined from ‘constexpr
std::ranges::copy_result<std::ranges::borrowed_iterator_t<_Range>, _Out>
std::ranges::__copy_fn::operator()(_Range&&, _Out) const [with _Range =
std::basic_string_view<char>&; _Out = char*]’ at
/home/wouter/gcc-snapshot/include/c++/16.0.0/bits/ranges_algobase.h:351:16,
    inlined from ‘concat<FormatHex>(const char*, FormatHex&)::<lambda(char*,
size_t)>’ at /home/wouter/test.cpp:32:20,
    inlined from ‘constexpr void std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::resize_and_overwrite(size_type, _Operation) [with _Operation =
concat<FormatHex>(const char*, FormatHex&)::<lambda(char*, size_t)>; _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at
/home/wouter/gcc-snapshot/include/c++/16.0.0/bits/basic_string.tcc:633:33,
    inlined from ‘std::string concat(const char*, T2&) [with T2 = FormatHex]’
at /home/wouter/test.cpp:31:29,
    inlined from ‘void test(unsigned int)’ at /home/wouter/test.cpp:43:8:
/home/wouter/gcc-snapshot/include/c++/16.0.0/bits/ranges_algobase.h:305:38:
warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’
forming offset [32, 38] is out of the bounds [0, 32] of object ‘<anonymous>’
with type ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’}
[-Warray-bounds=]
  305 |                     __builtin_memmove(__result, __first,
      |                     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
  306 |                                       sizeof(_ValueTypeI) * __num);
      |                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wouter/test.cpp: In function ‘void test(unsigned int)’:
/home/wouter/test.cpp:43:15: note: ‘<anonymous>’ declared here
   43 |         concat("12345678901234567890123", hex);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to