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 EB9893857C50 for ; Thu, 14 Apr 2022 15:21:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EB9893857C50 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-301-TNW3SOuSOVq7NKO-ENsjEw-1; Thu, 14 Apr 2022 11:21:14 -0400 X-MC-Unique: TNW3SOuSOVq7NKO-ENsjEw-1 Received: by mail-qk1-f198.google.com with SMTP id bi19-20020a05620a319300b0069c16295aabso3493435qkb.1 for ; Thu, 14 Apr 2022 08:21:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=f/tsTZIj+0wUL646fdlYB1iuD5AaeSg+RZo81Ip9Q8Q=; b=l+ZXaiY18f+lw+D0YdcG8VtwnlbiJNJD5ADJ7+w1GLt6FMLKwmZeinWStbNbakeUVd fl1zHnXseIHPqBaOCXguFO2JAYlrrsM+L93QZ3HjkaVtmZSvdC6GvFAIBM/NlkJ1e9Pm aCV6qckNJ7mxGksBhjTZXn52vkGSnSis6iRbF14kmw3DBl3i2qgO7N5q7ZJoNO2w1eSg bJWNMZCEzmClIrK18Xi5Ov91Qh0h4jm681Q6kIthFPi704muIPo8XBW3cTZpNLxU3oiF FIjSprNrhU3PTk6+ktLAPB2XjXeMVcFteXuvn7/cFuTTUYFL8ZFQ3PCFIZZwci/U6Lgg XOVw== X-Gm-Message-State: AOAM530QDYd55OprZiZ4VcGczt5zniUMMq+xyIFpE7qPvhAiyG0jn056 HiAqZ86EIZdkLsJ/ooUP54mvhK77CUWt9PijzLXvYkBrFTaKGRYWdutkWeiN4xsAR+iqkptzlar 3eILvwj86O9gbsw0= X-Received: by 2002:a37:ac12:0:b0:69c:e9e:8c02 with SMTP id e18-20020a37ac12000000b0069c0e9e8c02mr2243949qkm.603.1649949674040; Thu, 14 Apr 2022 08:21:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8/ZgvfN3Zu3YinHYSACDwaBRnvjSlQ9Sje6EUFnNDWnk56KhV+YnnFRNjV/dFAwAnjKNj+A== X-Received: by 2002:a37:ac12:0:b0:69c:e9e:8c02 with SMTP id e18-20020a37ac12000000b0069c0e9e8c02mr2243936qkm.603.1649949673823; Thu, 14 Apr 2022 08:21:13 -0700 (PDT) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id p16-20020a05622a13d000b002e227782e9asm1261679qtk.14.2022.04.14.08.21.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Apr 2022 08:21:13 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: Avoid double-deref of __first in ranges::minmax [PR104858] Date: Thu, 14 Apr 2022 11:21:11 -0400 Message-Id: <20220414152111.229967-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.0.rc2.10.g1ac7422e39 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.3 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=ham 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:21:17 -0000 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}; 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