[libcxx-commits] [libcxx] [libc++] Add input validation for set_intersection() in debug mode. (PR #101508)

Iuri Chaer via libcxx-commits libcxx-commits at lists.llvm.org
Sat Nov 9 08:51:11 PST 2024


================
@@ -20,18 +20,18 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _Compare, class _ForwardIterator>
+template <class _Compare, class _ForwardIterator, class _Sent>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
-__is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
+__is_sorted_until(_ForwardIterator __first, _Sent __last, _Compare&& __comp) {
   if (__first != __last) {
     _ForwardIterator __i = __first;
-    while (++__i != __last) {
-      if (__comp(*__i, *__first))
-        return __i;
-      __first = __i;
+    while (++__first != __last) {
----------------
ichaer wrote:

It's because we might skip the loop altogether `if (__first == __last)`, and with the sentinel-friendly interface we can no longer `return __last`. In the old version `__first` would always be behind `__i`, so a fully-sorted non-empty input would have us returning the one-before-last position.

https://github.com/llvm/llvm-project/pull/101508


More information about the libcxx-commits mailing list