[libc++] Avoid overloaded operator, for (T, Iter) cases (#161049)

Several components in libc++ aren't defending against overloaded
`operator,(T, Iter)` currently. Existing deleted overloads in
`test_iterators.h` are insufficient for such cases.

This PR adds corresponding deleted overloads with reversed order and
fixes these libc++ components.
- `piecewise_linear_distribution`'s iterator pair constructor,
- `piecewise_linear_distribution::param_type`'s iterator pair
constructor,
- `piecewise_constant_distribution`'s iterator pair constructor,
- `piecewise_constant_distribution::param_type`'s iterator pair
constructor,
- `money_get::do_get`,
- `money_put::do_put`, and
- `num_put::do_put`.
This commit is contained in:
A. Jiang 2025-11-10 20:04:50 +08:00 committed by GitHub
parent b18d828eea
commit 1ffe79d092
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
37 changed files with 133 additions and 46 deletions

View File

@ -433,7 +433,7 @@ bool money_get<_CharT, _InputIterator>::__do_get(
__err |= ios_base::failbit;
return false;
}
for (++__b; __fd > 0; --__fd, ++__b) {
for (++__b; __fd > 0; --__fd, (void)++__b) {
if (__b == __e || !__ct.is(ctype_base::digit, *__b)) {
__err |= ios_base::failbit;
return false;
@ -451,7 +451,7 @@ bool money_get<_CharT, _InputIterator>::__do_get(
}
}
if (__trailing_sign) {
for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b) {
for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, (void)++__b) {
if (__b == __e || *__b != (*__trailing_sign)[__i]) {
__err |= ios_base::failbit;
return false;

View File

@ -9,6 +9,7 @@
#ifndef _LIBCPP___LOCALE_DIR_NUM_H
#define _LIBCPP___LOCALE_DIR_NUM_H
#include <__algorithm/copy.h>
#include <__algorithm/find.h>
#include <__algorithm/reverse.h>
#include <__charconv/to_chars_integral.h>
@ -885,9 +886,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_ty
const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
typedef typename numpunct<char_type>::string_type string_type;
string_type __nm = __v ? __np.truename() : __np.falsename();
for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
*__s = *__i;
return __s;
return std::copy(__nm.begin(), __nm.end(), __s);
}
template <class _CharT, class _OutputIterator>

View File

@ -13,6 +13,8 @@
#if _LIBCPP_HAS_LOCALIZATION
# include <__algorithm/copy.h>
# include <__algorithm/fill_n.h>
# include <ios>
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@ -30,12 +32,9 @@ _LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
__ns -= __sz;
else
__ns = 0;
for (; __ob < __op; ++__ob, ++__s)
*__s = *__ob;
for (; __ns; --__ns, ++__s)
*__s = __fl;
for (; __ob < __oe; ++__ob, ++__s)
*__s = *__ob;
__s = std::copy(__ob, __op, __s);
__s = std::fill_n(__s, __ns, __fl);
__s = std::copy(__op, __oe, __s);
__iob.width(0);
return __s;
}

View File

@ -9,9 +9,11 @@
#ifndef _LIBCPP___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
#define _LIBCPP___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
#include <__algorithm/copy_n.h>
#include <__algorithm/upper_bound.h>
#include <__config>
#include <__cstddef/ptrdiff_t.h>
#include <__iterator/back_insert_iterator.h>
#include <__random/is_valid.h>
#include <__random/uniform_real_distribution.h>
#include <__vector/vector.h>
@ -190,8 +192,7 @@ piecewise_constant_distribution<_RealType>::param_type::param_type(
__areas_.assign(1, 0.0);
} else {
__densities_.reserve(__b_.size() - 1);
for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__f_w)
__densities_.push_back(*__f_w);
std::copy_n(__f_w, __b_.size() - 1, std::back_inserter(__densities_));
__init();
}
}

View File

@ -9,9 +9,11 @@
#ifndef _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
#define _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
#include <__algorithm/copy_n.h>
#include <__algorithm/upper_bound.h>
#include <__config>
#include <__cstddef/ptrdiff_t.h>
#include <__iterator/back_insert_iterator.h>
#include <__random/is_valid.h>
#include <__random/uniform_real_distribution.h>
#include <__vector/comparison.h>
@ -194,8 +196,7 @@ piecewise_linear_distribution<_RealType>::param_type::param_type(
__areas_.assign(1, 0.0);
} else {
__densities_.reserve(__b_.size());
for (size_t __i = 0; __i < __b_.size(); ++__i, ++__f_w)
__densities_.push_back(*__f_w);
std::copy_n(__f_w, __b_.size(), std::back_inserter(__densities_));
__init();
}
}

View File

@ -11,6 +11,7 @@
// template <class InputIterator> deque(InputIterator f, InputIterator l);
#include "asan_testing.h"
#include <algorithm>
#include <deque>
#include <cassert>
#include <cstddef>
@ -28,13 +29,11 @@ void test(InputIterator f, InputIterator l) {
typedef typename std::iterator_traits<InputIterator>::value_type T;
typedef std::allocator<T> Allocator;
typedef std::deque<T, Allocator> C;
typedef typename C::const_iterator const_iterator;
C d(f, l);
assert(d.size() == static_cast<std::size_t>(std::distance(f, l)));
assert(static_cast<std::size_t>(std::distance(d.begin(), d.end())) == d.size());
LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(d));
for (const_iterator i = d.begin(), e = d.end(); i != e; ++i, ++f)
assert(*i == *f);
assert(std::equal(d.begin(), d.end(), f));
}
template <class Allocator, class InputIterator>

View File

@ -12,6 +12,7 @@
// deque(InputIterator f, InputIterator l, const allocator_type& a);
#include "asan_testing.h"
#include <algorithm>
#include <deque>
#include <cassert>
#include <cstddef>
@ -28,14 +29,12 @@ template <class InputIterator, class Allocator>
void test(InputIterator f, InputIterator l, const Allocator& a) {
typedef typename std::iterator_traits<InputIterator>::value_type T;
typedef std::deque<T, Allocator> C;
typedef typename C::const_iterator const_iterator;
C d(f, l, a);
assert(d.get_allocator() == a);
assert(d.size() == static_cast<std::size_t>(std::distance(f, l)));
assert(static_cast<std::size_t>(std::distance(d.begin(), d.end())) == d.size());
LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(d));
for (const_iterator i = d.begin(), e = d.end(); i != e; ++i, ++f)
assert(*i == *f);
assert(std::equal(d.begin(), d.end(), f));
}
void basic_test() {

View File

@ -11,6 +11,7 @@
// template <class InputIter> vector(InputIter first, InputIter last);
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstddef>
@ -24,8 +25,7 @@ TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last) {
C c(first, last);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
assert(*i == *first);
assert(std::equal(c.cbegin(), c.cend(), first));
}
TEST_CONSTEXPR_CXX20 bool tests() {

View File

@ -12,6 +12,7 @@
// template <class InputIter> vector(InputIter first, InputIter last,
// const allocator_type& a);
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstddef>
@ -25,8 +26,7 @@ TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last, const typename C::
C c(first, last, a);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
assert(*i == *first);
assert(std::equal(c.cbegin(), c.cend(), first));
}
TEST_CONSTEXPR_CXX20 bool tests() {

View File

@ -10,6 +10,7 @@
// template <class InputIter> vector(InputIter first, InputIter last);
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstddef>
@ -31,8 +32,7 @@ TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last) {
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
assert(*i == *first);
assert(std::equal(c.cbegin(), c.cend(), first));
}
// Test with an empty range
{

View File

@ -11,6 +11,7 @@
// template <class InputIter> vector(InputIter first, InputIter last,
// const allocator_type& a);
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstddef>
@ -31,8 +32,7 @@ TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last, const A& a) {
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
assert(*i == *first);
assert(std::equal(c.cbegin(), c.cend(), first));
}
#if TEST_STD_VER >= 11

View File

@ -15,6 +15,7 @@
// Bionic has minimal locale support, investigate this later.
// XFAIL: LIBCXX-ANDROID-FIXME
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.en_US.UTF-8

View File

@ -8,6 +8,7 @@
// NetBSD does not support LC_MONETARY at the moment
// XFAIL: netbsd
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.fr_FR.UTF-8

View File

@ -16,6 +16,8 @@
// Ensure that money_get::do_get correct works when the input doesn't fit into the stack buffer
// (100 characters currently).
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <cassert>
#include <cstddef>
#include <ios>

View File

@ -14,6 +14,7 @@
// ADDITIONAL_COMPILE_FLAGS: -DRU_MON_THOU_SEP=%{LOCALE_CONV_RU_RU_UTF_8_MON_THOUSANDS_SEP}
// XFAIL: glibc-old-ru_RU-decimal-point
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// <locale>

View File

@ -10,6 +10,7 @@
// XFAIL: netbsd
// XFAIL: LIBCXX-FREEBSD-FIXME
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.zh_CN.UTF-8

View File

@ -15,6 +15,7 @@
// Bionic has minimal locale support, investigate this later.
// XFAIL: LIBCXX-ANDROID-FIXME
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.en_US.UTF-8

View File

@ -15,6 +15,7 @@
// Bionic has minimal locale support, investigate this later.
// XFAIL: LIBCXX-ANDROID-FIXME
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.en_US.UTF-8

View File

@ -9,6 +9,8 @@
// NetBSD does not support LC_MONETARY at the moment
// XFAIL: netbsd
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.fr_FR.UTF-8
// ADDITIONAL_COMPILE_FLAGS: -DFR_MON_THOU_SEP=%{LOCALE_CONV_FR_FR_UTF_8_MON_THOUSANDS_SEP}

View File

@ -9,6 +9,8 @@
// NetBSD does not support LC_MONETARY at the moment
// XFAIL: netbsd
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.ru_RU.UTF-8
// ADDITIONAL_COMPILE_FLAGS: -DRU_MON_THOU_SEP=%{LOCALE_CONV_RU_RU_UTF_8_MON_THOUSANDS_SEP}

View File

@ -10,6 +10,7 @@
// XFAIL: netbsd
// XFAIL: LIBCXX-FREEBSD-FIXME
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.zh_CN.UTF-8

View File

@ -16,6 +16,8 @@
// Bionic has minimal locale support, investigate this later.
// XFAIL: LIBCXX-ANDROID-FIXME
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
// REQUIRES: locale.en_US.UTF-8
#include <locale>

View File

@ -12,6 +12,8 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, bool v) const;
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>
#include <cassert>

View File

@ -13,6 +13,7 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, double v) const;
// XFAIL: win32-broken-printf-a-precision
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>

View File

@ -13,6 +13,7 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, double v) const;
// XFAIL: win32-broken-printf-g-precision
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>

View File

@ -12,6 +12,8 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, long v) const;
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>
#include <cassert>

View File

@ -13,6 +13,7 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, long double v) const;
// XFAIL: win32-broken-printf-a-precision
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>

View File

@ -13,6 +13,7 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, long double v) const;
// XFAIL: win32-broken-printf-g-precision
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>

View File

@ -12,6 +12,8 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, long long v) const;
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>
#include <cassert>

View File

@ -12,6 +12,8 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, void* v) const;
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <cassert>
#include <ios>
#include <locale>

View File

@ -12,6 +12,8 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, unsigned long v) const;
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>
#include <cassert>

View File

@ -12,6 +12,8 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, unsigned long long v) const;
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <locale>
#include <ios>
#include <cassert>

View File

@ -16,20 +16,24 @@
// InputIteratorB lastB,
// InputIteratorW firstW);
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <random>
#include <cassert>
#include <vector>
#include "test_iterators.h"
#include "test_macros.h"
int main(int, char**)
{
{
typedef std::piecewise_constant_distribution<> D;
typedef cpp17_input_iterator<const double*> InIt;
double b[] = {10};
double p[] = {12};
D d(b, b, p);
D d((InIt(b)), (InIt(b)), (InIt(p)));
std::vector<double> iv = d.intervals();
assert(iv.size() == 2);
assert(iv[0] == 0);

View File

@ -15,11 +15,14 @@
// param_type(InputIteratorB firstB, InputIteratorB lastB,
// InputIteratorW firstW);
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <random>
#include <cassert>
#include <vector>
#include "test_iterators.h"
#include "test_macros.h"
int main(int, char**)
@ -27,9 +30,10 @@ int main(int, char**)
{
typedef std::piecewise_constant_distribution<> D;
typedef D::param_type P;
typedef cpp17_input_iterator<const double*> InIt;
double b[] = {10};
double p[] = {12};
P pa(b, b, p);
P pa((InIt(b)), (InIt(b)), (InIt(p)));
std::vector<double> iv = pa.intervals();
assert(iv.size() == 2);
assert(iv[0] == 0);

View File

@ -16,20 +16,24 @@
// InputIteratorB lastB,
// InputIteratorW firstW);
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <random>
#include <cassert>
#include <vector>
#include "test_iterators.h"
#include "test_macros.h"
int main(int, char**)
{
{
typedef std::piecewise_linear_distribution<> D;
typedef cpp17_input_iterator<const double*> InIt;
double b[] = {10};
double p[] = {12};
D d(b, b, p);
D d((InIt(b)), (InIt(b)), (InIt(p)));
std::vector<double> iv = d.intervals();
assert(iv.size() == 2);
assert(iv[0] == 0);

View File

@ -15,11 +15,14 @@
// param_type(InputIteratorB firstB, InputIteratorB lastB,
// InputIteratorW firstW);
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <random>
#include <cassert>
#include <vector>
#include "test_iterators.h"
#include "test_macros.h"
int main(int, char**)
@ -27,9 +30,10 @@ int main(int, char**)
{
typedef std::piecewise_linear_distribution<> D;
typedef D::param_type P;
typedef cpp17_input_iterator<const double*> InIt;
double b[] = {10};
double p[] = {12};
P pa(b, b, p);
P pa((InIt(b)), (InIt(b)), (InIt(p)));
std::vector<double> iv = pa.intervals();
assert(iv.size() == 2);
assert(iv[0] == 0);

View File

@ -59,6 +59,9 @@ public:
template <class T>
void operator,(T const &) = delete;
template <class T>
friend void operator,(const T&, const cpp17_output_iterator&) = delete;
};
#if TEST_STD_VER > 14
template <class It>
@ -109,6 +112,9 @@ public:
template <class T>
void operator,(T const &) = delete;
template <class T>
friend void operator,(const T&, const cpp17_input_iterator&) = delete;
};
#if TEST_STD_VER > 14
template <class It>
@ -157,6 +163,9 @@ public:
template <class T>
void operator,(T const &) = delete;
template <class T>
friend void operator,(const T&, const forward_iterator&) = delete;
};
#if TEST_STD_VER > 14
template <class It>
@ -203,6 +212,9 @@ public:
template <class T>
void operator,(T const &) = delete;
template <class T>
friend void operator,(const T&, const bidirectional_iterator&) = delete;
};
#if TEST_STD_VER > 14
template <class It>
@ -261,6 +273,9 @@ public:
template <class T>
void operator,(T const &) = delete;
template <class T>
friend void operator,(const T&, const random_access_iterator&) = delete;
};
#if TEST_STD_VER > 14
template <class It>
@ -390,6 +405,9 @@ public:
template <class T>
void operator,(T const&) = delete;
template <class T>
friend void operator,(const T&, const three_way_random_access_iterator&) = delete;
};
#if TEST_STD_VER > 14
template <class It>
@ -485,6 +503,9 @@ public:
template <class T>
void operator,(T const&) = delete;
template <class T>
friend void operator,(const T&, const cpp20_random_access_iterator&) = delete;
};
template <class It>
cpp20_random_access_iterator(It) -> cpp20_random_access_iterator<It>;
@ -578,6 +599,9 @@ public:
template <class T>
void operator,(T const&) = delete;
template <class T>
friend void operator,(const T&, const contiguous_iterator&) = delete;
};
template <class It>
contiguous_iterator(It) -> contiguous_iterator<It>;
@ -635,6 +659,9 @@ public:
template <class T>
void operator,(T const &) = delete;
template <class T>
friend void operator,(const T&, const three_way_contiguous_iterator&) = delete;
};
template <class It>
three_way_contiguous_iterator(It) -> three_way_contiguous_iterator<It>;
@ -746,7 +773,10 @@ struct ThrowingIterator {
template <class T2>
void operator,(T2 const &) = delete;
private:
template <class T2>
friend void operator,(const T2&, const ThrowingIterator&) = delete;
private:
const T* begin_;
const T* end_;
const T* current_;
@ -817,7 +847,10 @@ struct NonThrowingIterator {
template <class T2>
void operator,(T2 const &) = delete;
private:
template <class T2>
friend void operator,(const T2&, const NonThrowingIterator&) = delete;
private:
const T *begin_;
const T *end_;
const T *current_;
@ -847,6 +880,9 @@ public:
template <class T>
void operator,(T const &) = delete;
template <class T>
friend void operator,(const T&, const cpp20_input_iterator&) = delete;
};
template <class It>
cpp20_input_iterator(It) -> cpp20_input_iterator<It>;
@ -884,6 +920,9 @@ public:
template <class T>
void operator,(T const&) = delete;
template <class T>
friend void operator,(const T&, const cpp20_output_iterator&) = delete;
};
template <class It>
cpp20_output_iterator(It) -> cpp20_output_iterator<It>;
@ -1077,17 +1116,20 @@ public:
template <class T>
void operator,(T const &) = delete;
private:
constexpr void moved_by(difference_type n) {
if (counts_ == nullptr)
return;
if (n > 0)
++counts_->increments;
else if (n < 0)
++counts_->decrements;
else
++counts_->zero_moves;
}
template <class T>
friend void operator,(const T&, const operation_counting_iterator&) = delete;
private:
constexpr void moved_by(difference_type n) {
if (counts_ == nullptr)
return;
if (n > 0)
++counts_->increments;
else if (n < 0)
++counts_->decrements;
else
++counts_->zero_moves;
}
decltype(base(std::declval<It>())) base_;
IteratorOpCounts* counts_ = nullptr;