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

            Bug ID: 114758
           Summary: The layout of a std::vector<bool> reports a warning
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: clalancette.github at gmail dot com
  Target Milestone: ---

Consider the following program:

#include <memory>
#include <vector>

struct BoundedPlainSequences
{
  BoundedPlainSequences()
  {
    this->bool_values_default.resize(3);
    this->bool_values_default = {{false, true, false}};

    this->uint16_values_default = {{0, 1, 65535}};
  }

  std::vector<bool> bool_values_default;

  std::vector<double> uint16_values_default;
};

int main()
{
  BoundedPlainSequences seq{};

  return 0;
}

If I compile this program with the following command-line on Ubuntu 24.04:

$ g++ -O3 bounded-warn.cpp

Then I get the warning:

In file included from /usr/include/c++/13/bits/stl_uninitialized.h:63,
                 from /usr/include/c++/13/memory:69,
                 from bounded-warn.cpp:1:
In static member function ‘static _Up* std::__copy_move<_IsMove, true,
std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [with _Tp = long
unsigned int; _Up = long unsigned int; bool _IsMove = false]’,
    inlined from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove =
false; _II = long unsigned int*; _OI = long unsigned int*]’ at
/usr/include/c++/13/bits/stl_algobase.h:506:30,
    inlined from ‘_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove =
false; _II = long unsigned int*; _OI = long unsigned int*]’ at
/usr/include/c++/13/bits/stl_algobase.h:533:42,
    inlined from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove =
false; _II = long unsigned int*; _OI = long unsigned int*]’ at
/usr/include/c++/13/bits/stl_algobase.h:540:31,
    inlined from ‘_OI std::copy(_II, _II, _OI) [with _II = long unsigned int*;
_OI = long unsigned int*]’ at /usr/include/c++/13/bits/stl_algobase.h:633:7,
    inlined from ‘std::vector<bool, _Alloc>::iterator std::vector<bool,
_Alloc>::_M_copy_aligned(const_iterator, const_iterator, iterator) [with _Alloc
= std::allocator<bool>]’ at /usr/include/c++/13/bits/stl_bvector.h:1342:28,
    inlined from ‘void std::vector<bool, _Alloc>::_M_fill_insert(iterator,
size_type, bool) [with _Alloc = std::allocator<bool>]’ at
/usr/include/c++/13/bits/vector.tcc:879:34,
    inlined from ‘std::vector<bool, _Alloc>::iterator std::vector<bool,
_Alloc>::insert(const_iterator, size_type, const bool&) [with _Alloc =
std::allocator<bool>]’ at /usr/include/c++/13/bits/stl_bvector.h:1242:16,
    inlined from ‘void std::vector<bool, _Alloc>::resize(size_type, bool) [with
_Alloc = std::allocator<bool>]’ at
/usr/include/c++/13/bits/stl_bvector.h:1288:10,
    inlined from ‘BoundedPlainSequences::BoundedPlainSequences()’ at
bounded-warn.cpp:8:37:
/usr/include/c++/13/bits/stl_algobase.h:437:30: warning: ‘void*
__builtin_memmove(void*, const void*, long unsigned int)’ writing between 9 and
9223372036854775807 bytes into a region of size 8 overflows the destination
[-Wstringop-overflow=]
  437 |             __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
      |             ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from
/usr/include/x86_64-linux-gnu/c++/13/bits/c++allocator.h:33,
                 from /usr/include/c++/13/bits/allocator.h:46,
                 from /usr/include/c++/13/memory:65:
In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const
void*) [with _Tp = long unsigned int]’,
    inlined from ‘static _Tp* std::allocator_traits<std::allocator<_Tp1>
>::allocate(allocator_type&, size_type) [with _Tp = long unsigned int]’ at
/usr/include/c++/13/bits/alloc_traits.h:482:28,
    inlined from ‘std::_Bvector_base<_Alloc>::_Bit_pointer
std::_Bvector_base<_Alloc>::_M_allocate(std::size_t) [with _Alloc =
std::allocator<bool>]’ at /usr/include/c++/13/bits/stl_bvector.h:679:48,
    inlined from ‘void std::vector<bool, _Alloc>::_M_fill_insert(iterator,
size_type, bool) [with _Alloc = std::allocator<bool>]’ at
/usr/include/c++/13/bits/vector.tcc:877:40,
    inlined from ‘std::vector<bool, _Alloc>::iterator std::vector<bool,
_Alloc>::insert(const_iterator, size_type, const bool&) [with _Alloc =
std::allocator<bool>]’ at /usr/include/c++/13/bits/stl_bvector.h:1242:16,
    inlined from ‘void std::vector<bool, _Alloc>::resize(size_type, bool) [with
_Alloc = std::allocator<bool>]’ at
/usr/include/c++/13/bits/stl_bvector.h:1288:10,
    inlined from ‘BoundedPlainSequences::BoundedPlainSequences()’ at
bounded-warn.cpp:8:37:
/usr/include/c++/13/bits/new_allocator.h:151:55: note: destination object of
size 8 allocated by ‘operator new’
  151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n *
sizeof(_Tp)));
      |                                                       ^


If I compile with -O2 or -O0, I don't get the warning.  If I remove the
"this->bool_values_default.resize(3);" line, I don't get the warning.  If I
remove the "this->uint16_values_default = {{0, 1, 65535}};" line, I don't get
the warning.

This kind of feels like something having to do with the fact that
std::vector<bool> is special, but that is just speculation.

If it helps, you can see this problem in action on
https://godbolt.org/z/vavTPvb7s .

Please let me know if you need any other information from me, I'm happy to
provide it.

Reply via email to