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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 58FC13858D35 for ; Mon, 21 Sep 2020 12:53:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 58FC13858D35 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-594-y-KbJrdUMz2wbiCJXbjGRA-1; Mon, 21 Sep 2020 08:47:01 -0400 X-MC-Unique: y-KbJrdUMz2wbiCJXbjGRA-1 Received: by mail-qv1-f70.google.com with SMTP id w2so9030331qvr.19 for ; Mon, 21 Sep 2020 05:47:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=N+LnF8wa+ml+xQ/zpEHCYQSqZMThmkYQQgx4kkTQzvo=; b=dLODtej8+rApECQvBverA3X+1DieNNND9it/5lnD+7xd0qB9K5XJedyTpbIfcH8mSR zypbn0o10N8jZF8yfdcE+2pPl6xYbYQIYVkRfsaiBDOO+/gDLB0qIy/NQxhpn+x/jo8v G19Ocw8gwaXV6HCIXnB+MLJVKPGo3btyQ28V9Dba69HVk+jEAlNaahQNrBgkxTFkXpBV WxJLRyV7ZJyaw9uHt9VUME1glMjO+B/PNcp9xPzcHDe8qNf3bQr7ApkQrwdMYfxwhU1p C5PAZA3vpWf7XKTGPHmBl5sURrCsuDFLUzmHrLbFrR/z6ccxmDRDOGTt5sHnFg5aIZPe /hCg== X-Gm-Message-State: AOAM530Jq0WEeSlHvabCXFQs6KRVZ7++s14A5FZPMBXq0V9zW3gDHhpD y2rNp31JQgPiXiM9IkaRh7e7pJz+tMo69Z+wNvtiX3G3RTLjinwvMs9hZm7lxAsJRnAwXEkc/9s UKAaJAA/55CnE7uI= X-Received: by 2002:ae9:c30d:: with SMTP id n13mr37989765qkg.138.1600692421453; Mon, 21 Sep 2020 05:47:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwsvQe+nhsNDkwtZi+31hvjPduNA/Yc5aCVwYHSwm+wWZG20i4Tt1xEG5qoBrt7eQBkk5RrMQ== X-Received: by 2002:ae9:c30d:: with SMTP id n13mr37989742qkg.138.1600692421162; Mon, 21 Sep 2020 05:47:01 -0700 (PDT) Received: from [192.168.1.130] (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id c70sm8774538qkg.4.2020.09.21.05.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Sep 2020 05:47:00 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Mon, 21 Sep 2020 08:46:59 -0400 (EDT) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH] libstdc++: Fix division by zero in std::sample In-Reply-To: <20200919010804.3509832-2-ppalka@redhat.com> Message-ID: <77195593-ebb-c4a5-9f1-c6e04b8d227a@idea> References: <20200919010804.3509832-1-ppalka@redhat.com> <20200919010804.3509832-2-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-16.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Mon, 21 Sep 2020 12:53:15 -0000 On Fri, 18 Sep 2020, Patrick Palka wrote: > This fixes a division by zero in the selection-sampling std::__search Whoops, this line say std::__sample, not std::__search. > overload when the input range is empty (and hence __unsampled_sz is 0). > > Tested on x86_64-pc-linux-gnu. > > libstdc++-v3/ChangeLog: > > * include/bits/stl_algo.h (__sample): Exit early when the > input range is empty. > * testsuite/25_algorithms/sample/3.cc: New test. > --- > libstdc++-v3/include/bits/stl_algo.h | 3 ++ > .../testsuite/25_algorithms/sample/3.cc | 50 +++++++++++++++++++ > 2 files changed, 53 insertions(+) > create mode 100644 libstdc++-v3/testsuite/25_algorithms/sample/3.cc > > diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h > index a0b96c61798..2478b5857c1 100644 > --- a/libstdc++-v3/include/bits/stl_algo.h > +++ b/libstdc++-v3/include/bits/stl_algo.h > @@ -5775,6 +5775,9 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO > using _Gen = remove_reference_t<_UniformRandomBitGenerator>; > using __uc_type = common_type_t; > > + if (__first == __last) > + return __out; > + > __distrib_type __d{}; > _Size __unsampled_sz = std::distance(__first, __last); > __n = std::min(__n, __unsampled_sz); > diff --git a/libstdc++-v3/testsuite/25_algorithms/sample/3.cc b/libstdc++-v3/testsuite/25_algorithms/sample/3.cc > new file mode 100644 > index 00000000000..e89c40e27ee > --- /dev/null > +++ b/libstdc++-v3/testsuite/25_algorithms/sample/3.cc > @@ -0,0 +1,50 @@ > +// Copyright (C) 2020 Free Software Foundation, Inc. > +// > +// This file is part of the GNU ISO C++ Library. This library is free > +// software; you can redistribute it and/or modify it under the > +// terms of the GNU General Public License as published by the > +// Free Software Foundation; either version 3, or (at your option) > +// any later version. > + > +// This library is distributed in the hope that it will be useful, > +// but WITHOUT ANY WARRANTY; without even the implied warranty of > +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > +// GNU General Public License for more details. > + > +// You should have received a copy of the GNU General Public License along > +// with this library; see the file COPYING3. If not see > +// . > + > +// { dg-options "-std=gnu++17" } > +// { dg-do run { target c++17 } } > +// { dg-require-cstdint "" } > + > +#include > +#include > +#include > +#include > + > +std::mt19937 rng; > + > +using std::sample; > +using __gnu_test::test_container; > +using __gnu_test::output_iterator_wrapper; > +using __gnu_test::forward_iterator_wrapper; > + > +void > +test01() > +{ > + const int in = 0; > + test_container pop(&in, &in); > + int out; > + test_container samp(&out, &out + 1); > + > + auto it = sample(pop.begin(), pop.end(), samp.begin(), 1, rng); > + VERIFY( it.ptr == &out ); > +} > + > +int > +main() > +{ > + test01(); > +} > -- > 2.28.0.497.g54e85e7af1 > >