From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5D54F3856DCD for ; Thu, 14 Apr 2022 15:27:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5D54F3856DCD Received: from mail-yw1-f197.google.com (mail-yw1-f197.google.com [209.85.128.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-221-glPCyJf7M8KMZZwr_2pXyg-1; Thu, 14 Apr 2022 11:27:19 -0400 X-MC-Unique: glPCyJf7M8KMZZwr_2pXyg-1 Received: by mail-yw1-f197.google.com with SMTP id 00721157ae682-2d11b6259adso45290797b3.19 for ; Thu, 14 Apr 2022 08:27:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VOsiA0fBb50YpRnDAAwdXVzK8L9g0DHSa+HZjsMOWW8=; b=ghnPwNtP9ksBhHNc62qM1Du4K78h6VXYMqs5YQrhKmbTAYbSedf0Qe+Hu0SboBuwgt TevAHCl4G8VZDbDTPr/xb9yAeJTRSsnhspdftlldF24gWXhHm4HgG34s9f2mkVMjaJTz ZBbadtLQbo9hbOcfFirgME7PlHwXO1c+DXqk39yGRzBg3nKGB7KxOICwHdT81WqxS1U8 c1L/wk9NMP+2MOi0/bZN+cWkZEQ1irV8gXZ4kxw4hBvHrX2Y03xWxhhf1oNk0F0nvPGz C5TAnFNESWesNzB3hBXn7JoTn9rt0+EPDaerEPjJfihRWUdMZbW7NIA8pfvJtqKMl1XY XUUA== X-Gm-Message-State: AOAM530ezcQ6omGU/hae/dW40UjsSjc4Nekj21RCaMRpkEb64uj9doEP +Dwf0FZJbUqVn7uiX/gTbLI6pcAcz70Lcx/ttPMix7kNE2qvjaIfH8WwrQIk4BQcPZt854m6dY9 wMXWBQcs3acTFWtX1Lj99RjSNEN5yHqk= X-Received: by 2002:a0d:e2ce:0:b0:2eb:f8cb:fae6 with SMTP id l197-20020a0de2ce000000b002ebf8cbfae6mr2352649ywe.206.1649950039408; Thu, 14 Apr 2022 08:27:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwF5yMxuPVohBJhloLTrGge2dcBO4j9Jdl1IzpY3jCxNNKVv9wymdjfwMFTrnDKihPH2TCKcZr2gNctlqiRe84= X-Received: by 2002:a0d:e2ce:0:b0:2eb:f8cb:fae6 with SMTP id l197-20020a0de2ce000000b002ebf8cbfae6mr2352641ywe.206.1649950039186; Thu, 14 Apr 2022 08:27:19 -0700 (PDT) MIME-Version: 1.0 References: <20220414152111.229967-1-ppalka@redhat.com> In-Reply-To: <20220414152111.229967-1-ppalka@redhat.com> From: Jonathan Wakely Date: Thu, 14 Apr 2022 16:27:08 +0100 Message-ID: Subject: Re: [PATCH] libstdc++: Avoid double-deref of __first in ranges::minmax [PR104858] To: Patrick Palka Cc: gcc Patches , "libstdc++" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Apr 2022 15:27:22 -0000 On Thu, 14 Apr 2022 at 16:21, Patrick Palka via Libstdc++ 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> __result = {*__first, *__first}; > + minmax_result> __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. OK for trunk now, and branches once thawed. > if (++__first == __last) > return __result; > else > diff --git a/libstdc++-v3/testsuite/25_algorithms/minmax/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/minmax/constrained.cc > index 90882afb6d0..306c495babe 100644 > --- a/libstdc++-v3/testsuite/25_algorithms/minmax/constrained.cc > +++ b/libstdc++-v3/testsuite/25_algorithms/minmax/constrained.cc > @@ -129,6 +129,28 @@ test05() > VERIFY( result.min == "a"s && result.max == "c"s ); > } > > +struct A { > + A() = default; > + A(const A&) = default; > + A(A&&) { ++move_count; } > + A& operator=(const A&) = default; > + A& operator=(A&&) = default; > + friend auto operator<=>(const A&, const A&) = default; > + static inline int move_count = 0; > +}; > + > +void > +test06() > +{ > + // PR libstdc++/104858 > + // Verify ranges::minmax doesn't dereference the iterator for the first > + // element in the range twice. > + A a; > + ranges::subrange r = {std::move_iterator(&a), std::move_sentinel(&a + 1)}; > + ranges::minmax(r); > + VERIFY( A::move_count == 1 ); > +} > + > int > main() > { > @@ -137,4 +159,5 @@ main() > test03(); > test04(); > test05(); > + test06(); > } > -- > 2.36.0.rc2.10.g1ac7422e39 >