Skip to content

Commit 1ffe79d

Browse files
[libc++] Avoid overloaded operator, for (T, Iter) cases (llvm#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`.
1 parent b18d828 commit 1ffe79d

37 files changed

+133
-46
lines changed

libcxx/include/__locale_dir/money.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ bool money_get<_CharT, _InputIterator>::__do_get(
433433
__err |= ios_base::failbit;
434434
return false;
435435
}
436-
for (++__b; __fd > 0; --__fd, ++__b) {
436+
for (++__b; __fd > 0; --__fd, (void)++__b) {
437437
if (__b == __e || !__ct.is(ctype_base::digit, *__b)) {
438438
__err |= ios_base::failbit;
439439
return false;
@@ -451,7 +451,7 @@ bool money_get<_CharT, _InputIterator>::__do_get(
451451
}
452452
}
453453
if (__trailing_sign) {
454-
for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b) {
454+
for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, (void)++__b) {
455455
if (__b == __e || *__b != (*__trailing_sign)[__i]) {
456456
__err |= ios_base::failbit;
457457
return false;

libcxx/include/__locale_dir/num.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef _LIBCPP___LOCALE_DIR_NUM_H
1010
#define _LIBCPP___LOCALE_DIR_NUM_H
1111

12+
#include <__algorithm/copy.h>
1213
#include <__algorithm/find.h>
1314
#include <__algorithm/reverse.h>
1415
#include <__charconv/to_chars_integral.h>
@@ -885,9 +886,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_ty
885886
const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
886887
typedef typename numpunct<char_type>::string_type string_type;
887888
string_type __nm = __v ? __np.truename() : __np.falsename();
888-
for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
889-
*__s = *__i;
890-
return __s;
889+
return std::copy(__nm.begin(), __nm.end(), __s);
891890
}
892891

893892
template <class _CharT, class _OutputIterator>

libcxx/include/__locale_dir/pad_and_output.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#if _LIBCPP_HAS_LOCALIZATION
1515

16+
# include <__algorithm/copy.h>
17+
# include <__algorithm/fill_n.h>
1618
# include <ios>
1719

1820
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -30,12 +32,9 @@ _LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
3032
__ns -= __sz;
3133
else
3234
__ns = 0;
33-
for (; __ob < __op; ++__ob, ++__s)
34-
*__s = *__ob;
35-
for (; __ns; --__ns, ++__s)
36-
*__s = __fl;
37-
for (; __ob < __oe; ++__ob, ++__s)
38-
*__s = *__ob;
35+
__s = std::copy(__ob, __op, __s);
36+
__s = std::fill_n(__s, __ns, __fl);
37+
__s = std::copy(__op, __oe, __s);
3938
__iob.width(0);
4039
return __s;
4140
}

libcxx/include/__random/piecewise_constant_distribution.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#ifndef _LIBCPP___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
1010
#define _LIBCPP___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
1111

12+
#include <__algorithm/copy_n.h>
1213
#include <__algorithm/upper_bound.h>
1314
#include <__config>
1415
#include <__cstddef/ptrdiff_t.h>
16+
#include <__iterator/back_insert_iterator.h>
1517
#include <__random/is_valid.h>
1618
#include <__random/uniform_real_distribution.h>
1719
#include <__vector/vector.h>
@@ -190,8 +192,7 @@ piecewise_constant_distribution<_RealType>::param_type::param_type(
190192
__areas_.assign(1, 0.0);
191193
} else {
192194
__densities_.reserve(__b_.size() - 1);
193-
for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__f_w)
194-
__densities_.push_back(*__f_w);
195+
std::copy_n(__f_w, __b_.size() - 1, std::back_inserter(__densities_));
195196
__init();
196197
}
197198
}

libcxx/include/__random/piecewise_linear_distribution.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#ifndef _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
1010
#define _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
1111

12+
#include <__algorithm/copy_n.h>
1213
#include <__algorithm/upper_bound.h>
1314
#include <__config>
1415
#include <__cstddef/ptrdiff_t.h>
16+
#include <__iterator/back_insert_iterator.h>
1517
#include <__random/is_valid.h>
1618
#include <__random/uniform_real_distribution.h>
1719
#include <__vector/comparison.h>
@@ -194,8 +196,7 @@ piecewise_linear_distribution<_RealType>::param_type::param_type(
194196
__areas_.assign(1, 0.0);
195197
} else {
196198
__densities_.reserve(__b_.size());
197-
for (size_t __i = 0; __i < __b_.size(); ++__i, ++__f_w)
198-
__densities_.push_back(*__f_w);
199+
std::copy_n(__f_w, __b_.size(), std::back_inserter(__densities_));
199200
__init();
200201
}
201202
}

libcxx/test/std/containers/sequences/deque/deque.cons/iter_iter.pass.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// template <class InputIterator> deque(InputIterator f, InputIterator l);
1212

1313
#include "asan_testing.h"
14+
#include <algorithm>
1415
#include <deque>
1516
#include <cassert>
1617
#include <cstddef>
@@ -28,13 +29,11 @@ void test(InputIterator f, InputIterator l) {
2829
typedef typename std::iterator_traits<InputIterator>::value_type T;
2930
typedef std::allocator<T> Allocator;
3031
typedef std::deque<T, Allocator> C;
31-
typedef typename C::const_iterator const_iterator;
3232
C d(f, l);
3333
assert(d.size() == static_cast<std::size_t>(std::distance(f, l)));
3434
assert(static_cast<std::size_t>(std::distance(d.begin(), d.end())) == d.size());
3535
LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(d));
36-
for (const_iterator i = d.begin(), e = d.end(); i != e; ++i, ++f)
37-
assert(*i == *f);
36+
assert(std::equal(d.begin(), d.end(), f));
3837
}
3938

4039
template <class Allocator, class InputIterator>

libcxx/test/std/containers/sequences/deque/deque.cons/iter_iter_alloc.pass.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// deque(InputIterator f, InputIterator l, const allocator_type& a);
1313

1414
#include "asan_testing.h"
15+
#include <algorithm>
1516
#include <deque>
1617
#include <cassert>
1718
#include <cstddef>
@@ -28,14 +29,12 @@ template <class InputIterator, class Allocator>
2829
void test(InputIterator f, InputIterator l, const Allocator& a) {
2930
typedef typename std::iterator_traits<InputIterator>::value_type T;
3031
typedef std::deque<T, Allocator> C;
31-
typedef typename C::const_iterator const_iterator;
3232
C d(f, l, a);
3333
assert(d.get_allocator() == a);
3434
assert(d.size() == static_cast<std::size_t>(std::distance(f, l)));
3535
assert(static_cast<std::size_t>(std::distance(d.begin(), d.end())) == d.size());
3636
LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(d));
37-
for (const_iterator i = d.begin(), e = d.end(); i != e; ++i, ++f)
38-
assert(*i == *f);
37+
assert(std::equal(d.begin(), d.end(), f));
3938
}
4039

4140
void basic_test() {

libcxx/test/std/containers/sequences/vector.bool/construct_iter_iter.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// template <class InputIter> vector(InputIter first, InputIter last);
1313

14+
#include <algorithm>
1415
#include <vector>
1516
#include <cassert>
1617
#include <cstddef>
@@ -24,8 +25,7 @@ TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last) {
2425
C c(first, last);
2526
LIBCPP_ASSERT(c.__invariants());
2627
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
27-
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
28-
assert(*i == *first);
28+
assert(std::equal(c.cbegin(), c.cend(), first));
2929
}
3030

3131
TEST_CONSTEXPR_CXX20 bool tests() {

libcxx/test/std/containers/sequences/vector.bool/construct_iter_iter_alloc.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// template <class InputIter> vector(InputIter first, InputIter last,
1313
// const allocator_type& a);
1414

15+
#include <algorithm>
1516
#include <vector>
1617
#include <cassert>
1718
#include <cstddef>
@@ -25,8 +26,7 @@ TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last, const typename C::
2526
C c(first, last, a);
2627
LIBCPP_ASSERT(c.__invariants());
2728
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
28-
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
29-
assert(*i == *first);
29+
assert(std::equal(c.cbegin(), c.cend(), first));
3030
}
3131

3232
TEST_CONSTEXPR_CXX20 bool tests() {

libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// template <class InputIter> vector(InputIter first, InputIter last);
1212

13+
#include <algorithm>
1314
#include <vector>
1415
#include <cassert>
1516
#include <cstddef>
@@ -31,8 +32,7 @@ TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last) {
3132
LIBCPP_ASSERT(c.__invariants());
3233
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
3334
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
34-
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
35-
assert(*i == *first);
35+
assert(std::equal(c.cbegin(), c.cend(), first));
3636
}
3737
// Test with an empty range
3838
{

0 commit comments

Comments
 (0)