[Bug libstdc++/60519] Debug mode should check comparators for irreflexivity

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sat Apr 11 19:09:00 GMT 2015


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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I was thinking we'd add a new macro to <debug/macros.h>

#define __glibcxx_check_strict_weak_order(_First,_Last,_Pred)          \
_GLIBCXX_DEBUG_VERIFY(_First==_Last || !_Pred(*_First, *_First),       \
                     _M_message(__gnu_debug::__msg_strict_weak_order))

(with accompanying __msg_strict_weak_order message)

Then simply add that where appropriate:

--- bits/stl_algo.h.orig        2015-04-11 13:58:05.253760716 +0100
+++ bits/stl_algo.h     2015-04-11 13:58:11.483766861 +0100
@@ -4725,6 +4725,7 @@
            typename iterator_traits<_RandomAccessIterator>::value_type,
            typename iterator_traits<_RandomAccessIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
+      __glibcxx_check_strict_weak_order(__first, __last, __comp);

       std::__sort(__first, __last,
__gnu_cxx::__ops::__iter_comp_iter(__comp));
     }

We could also consider including the function name in the message, either
passed to the macro directly:

#define __glibcxx_check_strict_weak_order(_First,_Last,_Pred,_Where)   \
_GLIBCXX_DEBUG_VERIFY(_First==_Last || !_Pred(*_First, *_First),       \
                     _M_message(__gnu_debug::__msg_strict_weak_order)) \
                     ._M_string(_Where)

or using __func__ or __PRETTY_FUNCTION__:

#define __glibcxx_check_strict_weak_order(_First,_Last,_Pred)          \
_GLIBCXX_DEBUG_VERIFY(_First==_Last || !_Pred(*_First, *_First),       \
                     _M_message(__gnu_debug::__msg_strict_weak_order)) \
                     ._M_string(__func__)

This would allow something like:

/home/jwakely/gcc/5/include/c++/5.0.0/bits/stl_algo.h:4729:error: 
    comparison function passed to 'std::sort' is invalid because comp(x, x)
returns true (the function must define a Strict Weak Ordering, so must be
irreflexive).
Aborted (core dumped)


If the checks are added in _Iter_comp_iter we lost the ability to include the
location string in the output.



More information about the Gcc-bugs mailing list