Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<ranges>: Fix cartesian_product_view::size in debug mode #3733

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -10061,11 +10061,14 @@ namespace ranges {
{
return [&]<size_t... _Indices>(index_sequence<_Indices...>) {
#if _CONTAINER_DEBUG_LEVEL > 0
const array _Sizes = {static_cast<_Size_type<false>>(_RANGES size(_STD get<_Indices>(_Bases)))...};
const bool _Any_zero = ((_Sizes[_Indices] == 0) || ...);
if (_Any_zero) {
return _Size_type<false>{0};
}

_Size_type<false> _Product{1};
const bool _Overflow =
(_Mul_overflow(
_Product, static_cast<_Size_type<false>>(_RANGES size(_STD get<_Indices>(_Bases))), _Product)
|| ...);
const bool _Overflow = (_Mul_overflow(_Product, _Sizes[_Indices], _Product) || ...);
_STL_VERIFY(!_Overflow, "Size of cartesian product cannot be represented by size type (N4950 "
"[range.cartesian.view]/10).");
return _Product;
Expand All @@ -10080,11 +10083,14 @@ namespace ranges {
{
return [&]<size_t... _Indices>(index_sequence<_Indices...>) {
#if _CONTAINER_DEBUG_LEVEL > 0
const array _Sizes = {static_cast<_Size_type<true>>(_RANGES size(_STD get<_Indices>(_Bases)))...};
const bool _Any_zero = ((_Sizes[_Indices] == 0) || ...);
if (_Any_zero) {
return _Size_type<true>{0};
}

_Size_type<true> _Product{1};
const bool _Overflow =
(_Mul_overflow(
_Product, static_cast<_Size_type<true>>(_RANGES size(_STD get<_Indices>(_Bases))), _Product)
|| ...);
const bool _Overflow = (_Mul_overflow(_Product, _Sizes[_Indices], _Product) || ...);
_STL_VERIFY(!_Overflow, "Size of cartesian product cannot be represented by size type (N4950 "
"[range.cartesian.view]/10).");
return _Product;
Expand Down
13 changes: 13 additions & 0 deletions tests/std/tests/P2374R4_views_cartesian_product/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,16 @@ namespace check_recommended_practice_implementation { // MSVC STL specific behav
STATIC_ASSERT(sizeof(range_difference_t<cartesian_product_view<all_t<Vec>, all_t<Vec>>>) > sizeof(ptrdiff_t));
} // namespace check_recommended_practice_implementation

// GH-3733: cartesian_product_view would incorrectly reject a call to size() claiming that big*big*big*0 is not
// representable as range_size_t because big*big*big is not.
constexpr void test_gh_3733() {
JMazurkiewicz marked this conversation as resolved.
Show resolved Hide resolved
const auto r1 = views::repeat(0, (numeric_limits<ptrdiff_t>::max)());
const auto r2 = views::repeat(1, 0);
const auto cart = views::cartesian_product(r1, r1, r1, r2);
assert(cart.size() == 0);
assert(as_const(cart).size() == 0);
}

int main() {
// Check views
{ // ... copyable
Expand Down Expand Up @@ -1029,4 +1039,7 @@ int main() {
STATIC_ASSERT((instantiation_test(), true));
#endif // TRANSITION, GH-1030
instantiation_test();

STATIC_ASSERT((test_gh_3733(), true));
test_gh_3733();
}