From: Patrick Palka <ppalka@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: Patrick Palka <ppalka@redhat.com>,
gcc Patches <gcc-patches@gcc.gnu.org>,
libstdc++ <libstdc++@gcc.gnu.org>
Subject: Re: [PATCH] libstdc++: Avoid double-deref of __first in ranges::minmax [PR104858]
Date: Fri, 15 Apr 2022 14:42:54 -0400 (EDT) [thread overview]
Message-ID: <f2035d00-c36a-60eb-595e-2f8477664ff9@idea> (raw)
In-Reply-To: <CACb0b4=j4D1357P8jh5OLnHH2yhE5vXJwhvV_R4vhbtXYRRYYw@mail.gmail.com>
On Thu, 14 Apr 2022, Jonathan Wakely wrote:
> On Thu, 14 Apr 2022 at 16:21, Patrick Palka via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > Tested on x86_64-pc-linux-gnu, does this look OK for trunk and 11/10
> > once the branch is unfrozen?
> >
> > PR libstdc++/104858
> >
> > libstdc++-v3/ChangeLog:
> >
> > * include/bits/ranges_algo.h (__minmax_fn): Avoid dereferencing
> > __first twice at the start.
> > * testsuite/25_algorithms/minmax/constrained.cc (test06): New test.
> > ---
> > libstdc++-v3/include/bits/ranges_algo.h | 2 +-
> > .../25_algorithms/minmax/constrained.cc | 23 +++++++++++++++++++
> > 2 files changed, 24 insertions(+), 1 deletion(-)
> >
> > diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
> > index 62dc605080a..3d30fb1428c 100644
> > --- a/libstdc++-v3/include/bits/ranges_algo.h
> > +++ b/libstdc++-v3/include/bits/ranges_algo.h
> > @@ -3084,7 +3084,7 @@ namespace ranges
> > auto __last = ranges::end(__r);
> > __glibcxx_assert(__first != __last);
> > auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
> > - minmax_result<range_value_t<_Range>> __result = {*__first, *__first};
> > + minmax_result<range_value_t<_Range>> __result = {*__first, __result.min};
>
> Clever ... I'm surprised this even works. I would have expected it to
> evaluate both initializers before actually initializing the members.
> TIL.
Indeed, it seems to do the right thing, practically speaking at least :)
FWIW the alternative approach
- minmax_result<range_value_t<_Range>> __result = {*__first, *__first};
+ minmax_result<range_value_t<_Range>> __result;
+ __result.max = __result.min = *__first;
wouldn't be right because the value type is not necessarily default
constructible. I beefed up the new testcase to verify we don't demand
default constructibility here.
prev parent reply other threads:[~2022-04-15 18:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-14 15:21 Patrick Palka
2022-04-14 15:27 ` Jonathan Wakely
2022-04-15 18:42 ` Patrick Palka [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f2035d00-c36a-60eb-595e-2f8477664ff9@idea \
--to=ppalka@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jwakely@redhat.com \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).