From patchwork Sat Sep 19 01:08:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1367435 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Lqqbl3e9; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BtXdY3TsKz9sSC for ; Sat, 19 Sep 2020 11:08:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8CD5F39730E6; Sat, 19 Sep 2020 01:08:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8CD5F39730E6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1600477700; bh=aDSWU/5q/2ANshNyJyGw4LfyUPUUHehkv2oELnKQVFU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Lqqbl3e97Xc8BxGIp1B243HEzto8uaARSm6EoTKVQC7fpfuXGXrQJZnGYiCf0jg8w xE20ldV4M4jFFd5rp6xcON6OdDbe9gxlUkQ7a8BTh2zIjSP39uV0QRkFXN33SajGU5 RLx1Lr0V89EbnOUF8GApS0itFQBLabl9GIwiLev0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id BA26639730DF for ; Sat, 19 Sep 2020 01:08:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BA26639730DF Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-22-WCZBm5kPPeKbT0gTy369zw-1; Fri, 18 Sep 2020 21:08:14 -0400 X-MC-Unique: WCZBm5kPPeKbT0gTy369zw-1 Received: by mail-qt1-f199.google.com with SMTP id y53so6783556qth.2 for ; Fri, 18 Sep 2020 18:08:14 -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:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aDSWU/5q/2ANshNyJyGw4LfyUPUUHehkv2oELnKQVFU=; b=S9ehhPctxUAITs6f5xzF3X4U7IcNJ0QAGi3/hjEDeXjQ+idqhoYJPAFzJlW8CL9J/4 wE8h4Zy7lF6Fh21BvV8HnNXiUGy4E9qRnfDghqyLJw6JROkAsiAQH51gePNojXE51MvG UPn+d+nW9ayKLodesYBHKm3X6wYiabcH4MKhSf155b75vwZnaSDO7MgqMPs9Nte4BN+v LD2guoXe5rsPmavrvn59MnjizNTQUxgQZQpwEYCmsDIzlaNbU9XoHte31VjyAxYeVJXb kQgLfBdGXaPgYnyWW39NjcmWPtW0B36fZiyhB9u6P0FmvBic89UjdxbjPFl8Yt4HcRq6 +foQ== X-Gm-Message-State: AOAM5336/pLY/6x0FEmeqMIMirpzxZCsCKXiwcpPL/XqLiq3xxqcQFgx nTgX+BG6v9aLoJCGQvoC0LoBUP6zjRABaoAID7ePFJgy1BcvgmTFTaTmoH8hoD23NvfUpnyqsG4 oBYtEDuW4pggvu82ovA== X-Received: by 2002:a37:4711:: with SMTP id u17mr33808430qka.54.1600477693958; Fri, 18 Sep 2020 18:08:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGqm1EI82PbSwSCHLBA7dcC0a+e1dLonclxtnJfeCLZTWDja5F2RfWOh456HStoZOTvZKovw== X-Received: by 2002:a37:4711:: with SMTP id u17mr33808422qka.54.1600477693763; Fri, 18 Sep 2020 18:08:13 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id o188sm3314141qke.83.2020.09.18.18.08.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Sep 2020 18:08:12 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Fix division by zero in std::sample Date: Fri, 18 Sep 2020 21:08:04 -0400 Message-Id: <20200919010804.3509832-2-ppalka@redhat.com> X-Mailer: git-send-email 2.28.0.497.g54e85e7af1 In-Reply-To: <20200919010804.3509832-1-ppalka@redhat.com> References: <20200919010804.3509832-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-17.2 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_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Cc: libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This fixes a division by zero in the selection-sampling 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(); +}