https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113960
--- Comment #3 from Stefan Schulze Frielinghaus <stefansf at linux dot ibm.com> --- This seems to be a bug in the three way comparison introduced with C++20. The bug happens while deciding whether key v2 already exists in the map or not. template<typename _InputIter1, typename _InputIter2, typename _Comp> constexpr auto lexicographical_compare_three_way(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2, _InputIter2 __last2, _Comp __comp) -> decltype(__comp(*__first1, *__first2)) { // concept requirements __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>) __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>) __glibcxx_requires_valid_range(__first1, __last1); __glibcxx_requires_valid_range(__first2, __last2); using _Cat = decltype(__comp(*__first1, *__first2)); static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>); if (!std::__is_constant_evaluated()) if constexpr (same_as<_Comp, __detail::_Synth3way> || same_as<_Comp, compare_three_way>) if constexpr (__is_byte_iter<_InputIter1>) if constexpr (__is_byte_iter<_InputIter2>) { const auto [__len, __lencmp] = _GLIBCXX_STD_A:: __min_cmp(__last1 - __first1, __last2 - __first2); if (__len) { const auto __c = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0; if (__c != 0) return __c; } return __lencmp; } __len equals 1 since both vectors have length 1. However, memcmp should be called with the number of bytes and not the number of elements of the vector. That means memcmp is called with two pointers to MEMs of unsigned shorts 1 and 2 where the high-bytes equal 0 and therefore memcmp returns with 0 on big-endian targets. Ultimately __lencmp is returned which itself equals std::strong_ordering::equal rendering v2 replacing v1. Fixed by diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index d534e02871f..6ebece315f7 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -1867,8 +1867,10 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO __min_cmp(__last1 - __first1, __last2 - __first2); if (__len) { + const auto __len_bytes = __len * sizeof (*first1); const auto __c - = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0; + = __builtin_memcmp(&*__first1, &*__first2, __len_bytes) + <=> 0; if (__c != 0) return __c; } Can you give the patch a try?