Skip to content

Compiler warning when building under GCC #8802

Closed

Description

@gafferongames

I'm building flat buffers source under my own CMake-built makefile on Ubuntu 24.04 LTS

When building with GCC I get these errors, which look like memory overflows... are these real?

In file included from /usr/include/c++/13/algorithm:60,
                 from /home/ubuntu/space/game/ThirdParty/flatbuffers/flatbuffers.h:20,
                 from /home/ubuntu/space/game/ThirdParty/flatbuffers/reflection_generated.h:7,
                 from /home/ubuntu/space/game/ThirdParty/flatbuffers/reflection.h:25,
                 from /home/ubuntu/space/game/ThirdParty/flatbuffers/reflection.cpp:17:
In static member function ‘static constexpr _Up* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [with _Tp = const unsigned char; _Up = unsigned char; bool _IsMove = false]’,
    inlined from ‘constexpr _OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = const unsigned char*; _OI = unsigned char*]’ at /usr/include/c++/13/bits/stl_algobase.h:506:30,
    inlined from ‘constexpr _OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = const unsigned char*; _OI = unsigned char*]’ at /usr/include/c++/13/bits/stl_algobase.h:533:42,
    inlined from ‘constexpr _OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const unsigned char*; _OI = unsigned char*]’ at /usr/include/c++/13/bits/stl_algobase.h:540:31,
    inlined from ‘constexpr _OI std::copy(_II, _II, _OI) [with _II = const unsigned char*; _OI = unsigned char*]’ at /usr/include/c++/13/bits/stl_algobase.h:633:7,
    inlined from ‘static _ForwardIterator std::__uninitialized_copy<true>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const unsigned char*; _ForwardIterator = unsigned char*]’ at /usr/include/c++/13/bits/stl_uninitialized.h:147:27,
    inlined from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const unsigned char*; _ForwardIterator = unsigned char*]’ at /usr/include/c++/13/bits/stl_uninitialized.h:185:15,
    inlined from ‘constexpr _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = const unsigned char*; _ForwardIterator = unsigned char*; _Tp = unsigned char]’ at /usr/include/c++/13/bits/stl_uninitialized.h:373:37,
    inlined from ‘constexpr void std::vector<_Tp, _Alloc>::_M_range_insert(iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const unsigned char*; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ at /usr/include/c++/13/bits/vector.tcc:814:38,
    inlined from ‘constexpr std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(const_iterator, _InputIterator, _InputIterator) [with _InputIterator = const unsigned char*; <template-parameter-2-2> = void; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ at /usr/include/c++/13/bits/stl_vector.h:1486:19,
    inlined from ‘const uint8_t* flatbuffers::AddFlatBuffer(std::vector<unsigned char>&, const uint8_t*, size_t)’ at /home/ubuntu/space/game/ThirdParty/flatbuffers/reflection.cpp:649:17:
/usr/include/c++/13/bits/stl_algobase.h:437:30: warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ writing between 2 and 9223372036854775803 bytes into a region of size 0 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,
                 from /home/ubuntu/space/game/ThirdParty/flatbuffers/array.h:21,
                 from /home/ubuntu/space/game/ThirdParty/flatbuffers/flatbuffers.h:24:
In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = unsigned char]’,
    inlined from ‘constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = unsigned char]’ at /usr/include/c++/13/bits/allocator.h:198:40,
    inlined from ‘static constexpr _Tp* std::allocator_traits<std::allocator<_Up> >::allocate(allocator_type&, size_type) [with _Tp = unsigned char]’ at /usr/include/c++/13/bits/alloc_traits.h:482:28,
    inlined from ‘constexpr std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ at /usr/include/c++/13/bits/stl_vector.h:381:33,
    inlined from ‘constexpr void std::vector<_Tp, _Alloc>::_M_range_insert(iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const unsigned char*; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ at /usr/include/c++/13/bits/vector.tcc:805:40,
    inlined from ‘constexpr std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(const_iterator, _InputIterator, _InputIterator) [with _InputIterator = const unsigned char*; <template-parameter-2-2> = void; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ at /usr/include/c++/13/bits/stl_vector.h:1486:19,
    inlined from ‘const uint8_t* flatbuffers::AddFlatBuffer(std::vector<unsigned char>&, const uint8_t*, size_t)’ at /home/ubuntu/space/game/ThirdParty/flatbuffers/reflection.cpp:649:17:
/usr/include/c++/13/bits/new_allocator.h:151:55: note: at offset [-9223372036854775808, -4] into destination object of size [4, 9223372036854775807] allocated by ‘operator new’
  151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
      |                                                       ^

Activity

  1. jtdavis777 commented on Nov 25, 2025

    @jtdavis777
    Collaborator

    I believe gcc is technically correct with this one, though the actual issue is unlikely to occur in practice -- it is warning that if you call the below function with a newlen of size less than sizeof(uoffset_t) (which is 4) then you'll try to insert close to uint64_t::max bytes into the vector.

    It may be worth adding an assertion here FLATBUFFERS_ASSERT(newlen >= sizeof(uoffset_t)); which should quiet the warning.

    If you want to test and PR that, I'd be happy to review it.

    const uint8_t* AddFlatBuffer(std::vector<uint8_t>& flatbuf,
                                 const uint8_t* newbuf, size_t newlen) {
      // Align to sizeof(uoffset_t) past sizeof(largest_scalar_t) since we're
      // going to chop off the root offset.
      while ((flatbuf.size() & (sizeof(uoffset_t) - 1)) ||
             !(flatbuf.size() & (sizeof(largest_scalar_t) - 1))) {
        flatbuf.push_back(0);
      }
      auto insertion_point = static_cast<uoffset_t>(flatbuf.size());
      // Insert the entire FlatBuffer minus the root pointer.
      flatbuf.insert(flatbuf.end(), newbuf + sizeof(uoffset_t), newbuf + newlen);
      auto root_offset = ReadScalar<uoffset_t>(newbuf) - sizeof(uoffset_t);
      return flatbuf.data() + insertion_point + root_offset;
    }
  2. gafferongames commented on Nov 25, 2025

    @gafferongames
    ContributorAuthor

    Yes, adding the assert does fix the warning. I'll get a PR ready now

  3. gafferongames commented on Nov 25, 2025

    @gafferongames
    ContributorAuthor

    PR is up: #8804

  4. self-assigned this
    on Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions