Std::sort calls comparator with an invalid iterator

Hi, std::sort() calls the comparator with out of bounds iterators when being passed a stupid comparator function (which returns always true for example).
I’m using xcode14.3 (clang version 14.0.3 (clang-1403.0.22.14.1))
At any optimisation level, -std=c++20

I wonder if it can be considered a bug in libc++ or an undefined behaviour?
Note that if I comment out the copy constructor, there’s no problem.

My app accepts user defined comparator functions, so I need to take care of such cases…

	struct Value
	{
		Value():i(123)    {}
		Value( const Value& inOther):i(123){}  // comment this line and it doesn't abort
		int    i;
	};
 	std::vector<Value> v( 8);
	std::sort( v.begin(), v.end(), []( auto& a1, auto& a2){
		   if (a1.i != 123 || a2.i != 123)
			   abort();  // got an invalid iterator!
		   return true;
	});

The following patch seems related: D147089

Thanks for the link!
I’m not sure what version of libc++ is provided with Xcode 14.3, _LIBCPP_VERSION is 15006 so I guess it’s version 15, and the link is talking about a patch for version 16 reverting the optimisation. But maybe llvm version and libc++ versions are not in sync.
Anyway my understanding is that I need to check the iterators myself if the comparator is untrusted.

thanks again for your help!

You are welcome!
The ultimate way to check is to compare your system headers with contents of the patch. If changes are present, then I think it deserves a bug report on bug tracker.