[Bug libstdc++/108517] std::sort of empty range yield "warning: 'this' pointer is null"

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 24 12:33:52 GMT 2023


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-01-24
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |diagnostic

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Please provide the code here instead of just a link, as per
https://gcc.gnu.org/bugs/

#include <algorithm>

struct object {
    size_t size() const noexcept;
};

struct empty_vec {
    object * begin() noexcept {
        return nullptr;
    }
    object * end() noexcept {
        return nullptr;
    }
};

int main() {
    auto sort_by_size = [](const auto & lhs, const auto & rhs) { return
lhs.size() < rhs.size(); };

    empty_vec vec{};
        std::sort(vec.begin(), vec.end(), sort_by_size);
}



$ g++ -O2 sort.cc -c -Wall 
In lambda function,
    inlined from 'constexpr bool
__gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2)
[with _Iterator1 = object*; _Iterator2 = object*; _Compare =
main()::<lambda(const auto:1&, const auto:2&)>]' at
/usr/include/c++/12/bits/predefined_ops.h:158:30,
    inlined from 'void std::__insertion_sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = object*;
_Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const auto:1&,
const auto:2&)> >]' at /usr/include/c++/12/bits/stl_algo.h:1809:14:
sort.cc:17:94: warning: 'this' pointer is null [-Wnonnull]
   17 |     auto sort_by_size = [](const auto & lhs, const auto & rhs) { return
lhs.size() < rhs.size(); };
      |                                                                        
             ~~~~~~~~^~
sort.cc: In function 'void std::__insertion_sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = object*;
_Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const auto:1&,
const auto:2&)> >]':
sort.cc:4:12: note: in a call to non-static member function 'size_t
object::size() const'
    4 |     size_t size() const noexcept;
      |            ^~~~
In file included from /usr/include/c++/12/algorithm:60,
                 from sort.cc:1:
In static member function 'static _Tp* std::__copy_move_backward<_IsMove, true,
std::random_access_iterator_tag>::__copy_move_b(const _Tp*, const _Tp*, _Tp*)
[with _Tp = object; bool _IsMove = true]',
    inlined from '_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with
bool _IsMove = true; _BI1 = object*; _BI2 = object*]' at
/usr/include/c++/12/bits/stl_algobase.h:760:37,
    inlined from '_BI2 std::__copy_move_backward_a1(_BI1, _BI1, _BI2) [with
bool _IsMove = true; _BI1 = object*; _BI2 = object*]' at
/usr/include/c++/12/bits/stl_algobase.h:769:51,
    inlined from '_OI std::__copy_move_backward_a(_II, _II, _OI) [with bool
_IsMove = true; _II = object*; _OI = object*]' at
/usr/include/c++/12/bits/stl_algobase.h:798:31,
    inlined from '_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 =
object*; _BI2 = object*]' at /usr/include/c++/12/bits/stl_algobase.h:892:47,
    inlined from 'void std::__insertion_sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator = object*;
_Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const auto:1&,
const auto:2&)> >]' at /usr/include/c++/12/bits/stl_algo.h:1813:8:
/usr/include/c++/12/bits/stl_algobase.h:742:30: warning: argument 2 null where
non-null expected [-Wnonnull]
  742 |             __builtin_memmove(__result - _Num, __first, sizeof(_Tp) *
_Num);
      |            
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/12/bits/stl_algobase.h:742:30: note: in a call to built-in
function 'void* __builtin_memmove(void*, const void*, long unsigned int)'



This looks related to PR 105705. Before gcc-12 the warning was already present,
but only when using -Wsystem-headers


More information about the Gcc-bugs mailing list