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

<algorithm>: ranges::minmax accidentally constructed remove_cvref_t<iter_reference_t<I>> #4102

Closed
hewillk opened this issue Oct 18, 2023 · 1 comment · Fixed by #4841
Closed
Labels
bug Something isn't working fixed Something works now, yay! ranges C++20/23 ranges

Comments

@hewillk
Copy link
Contributor

hewillk commented Oct 18, 2023

STL/stl/inc/algorithm

Lines 10108 to 10116 in 1c59a20

using _Vty = range_value_t<_Rng>;
// This initialization is correct, similar to the N4950 [dcl.init.aggr]/6 example
minmax_result<_Vty> _Found = {static_cast<_Vty>(*_UFirst), _Found.min};
if (_UFirst == _ULast) {
return _Found;
}
while (++_UFirst != _ULast) { // process one or two elements
auto _Prev = *_UFirst;

The last auto _Prev = *_UFirst; indicates that we constructed remove_cvref_t<iter_reference_t<I>> from iter_reference_t<I>, which is not guaranteed to be well-formed by the function's constraints.
I believe we should do _Vty _Prev(*_UFirst); instead.

Contrived testcase: https://godbolt.org/z/Gdb8qffdM

@StephanTLavavej StephanTLavavej added bug Something isn't working ranges C++20/23 ranges labels Oct 18, 2023
@StephanTLavavej
Copy link
Member

Good catch - we talked about this at the weekly maintainer meeting and agree that this is a bug.

This also indicates that we have missing test coverage of ranges algorithms with this scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay! ranges C++20/23 ranges
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants