Dobiasd / FunctionalPlus

Functional Programming Library for C++. Write concise and readable C++ code.
http://www.editgym.com/fplus-api-search/
Boost Software License 1.0
2.1k stars 167 forks source link

container_common_test.: out of bounds warning on GCC 14 #301

Closed offa closed 4 months ago

offa commented 4 months ago

Compiling fplus on GCC 14 issues an out of bounds warning:

 [ 23%] Building CXX object CMakeFiles/function_traits_test.dir/function_traits_test.cpp.o
In file included from /usr/local/include/c++/14.1.0/deque:62,
                 from /__w/FunctionalPlus/FunctionalPlus/test/container_common_test.cpp:7:
In static member function 'static _Up* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [with _Tp = const int; _Up = int; bool _IsMove = false]',
    inlined from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = const int*; _OI = int*]' at /usr/local/include/c++/14.1.0/bits/stl_algobase.h:521:30,
    inlined from '_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = const int*; _OI = int*]' at /usr/local/include/c++/14.1.0/bits/stl_algobase.h:548:42,
    inlined from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const int*, vector<int> >; _OI = int*]' at /usr/local/include/c++/14.1.0/bits/stl_algobase.h:555:31,
    inlined from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const int*, vector<int> >; _OI = int*]' at /usr/local/include/c++/14.1.0/bits/stl_algobase.h:651:7,
    inlined from 'static _ForwardIterator std::__uninitialized_copy<true>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >; _ForwardIterator = int*]' at /usr/local/include/c++/14.1.0/bits/stl_uninitialized.h:147:27,
    inlined from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const int*, vector<int> >; _ForwardIterator = int*]' at /usr/local/include/c++/14.1.0/bits/stl_uninitialized.h:185:15,
    inlined from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const int*, vector<int> >; _ForwardIterator = int*; _Tp = int]' at /usr/local/include/c++/14.1.0/bits/stl_uninitialized.h:373:37,
    inlined from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]' at /usr/local/include/c++/14.1.0/bits/stl_vector.h:606:31,
    inlined from 'Container fplus::intersperse(const X&, const Container&) [with Container = std::vector<int>; X = int]' at /__w/FunctionalPlus/FunctionalPlus/include/fplus/container_common.hpp:1641:16:
/usr/local/include/c++/14.1.0/bits/stl_algobase.h:452:30: error: 'void* __builtin_memmove(void*, const void*, long unsigned int)' forming offset 4 is out of the bounds [0, 4] [-Werror=array-bounds=]
  452 |             __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
      |             ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The mentioned line doesn't look too wrong though:

    if (size_of_cont(xs) == 1)
        return xs; // <-- line 1641

(PR with an GCC 14 CI build is on the way)

Dobiasd commented 4 months ago

Oh, what an interesting false positive (I guess), thanks!

I'll see if I can build a workaround for FunctionalPlus, and maybe even a minimal example to reproduce the problem.

Dobiasd commented 4 months ago

I've experimented locally, and replacing return xs; with return Container({xs.front()}); seems to work. It's not beautiful, but it should also not be a significant performance penalty. Shall we use this? And would you like to push it to your branch, or shall I open a new one?

offa commented 4 months ago

I've experimented locally, and replacing return xs; with return Container({xs.front()}); seems to work.

Interesting :thinking:.

Shall we use this? And would you like to push it to your branch, or shall I open a new one?

If it works, why not? :-) There' should be a comment documenting the reason though.

And would you like to push it to your branch, or shall I open a new one?

You should have write access to me PR, feel free to add commits directly. :+1: Otherwise, what suits you best.

Dobiasd commented 4 months ago

Ok, I pushed the change (including a comment). :heavy_check_mark:

offa commented 4 months ago

Fixed by #302