From patchwork Tue Mar 19 16:00:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1913699 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=UXXbgdBD; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tzc1n21xNz1yWs for ; Wed, 20 Mar 2024 03:02:37 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5136E3858289 for ; Tue, 19 Mar 2024 16:02:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id E84B73858283 for ; Tue, 19 Mar 2024 16:01:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E84B73858283 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E84B73858283 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710864091; cv=none; b=ckUlZyTKXY44//YDWL6L0PfvU8SQ8oiTqYrfRudwgYkYDNxPMg2GIIebVPQW9/YDvEDVF2TV9r2poe+J2nNhrvo//idXa++Xy6khSLESMp8XKIHUDQK2MjCeH7urzdlx67I0AwLMQvo4r63ztQoi9qLNbhtk4biKZsza2nBBH6E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710864091; c=relaxed/simple; bh=S9X7PwZdRWbp5FfjhGh7rk5k3EwlNxgPQokcywaqsZc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=A4Ii+f1+vpNv38nM1jk2ljYyBsGc/pcBjk629s84RJxXPwLFRcrweUtU62ey4MTEZ14qR55tpQ7/JQVOIBBkgDRP6Z8MtDSMDzh1DJsD22kk9TbAoN/ak2+QIWtyAmyl6Zj4/a2bBCjRiTCJ3SqnpYHCF0Dj6L6W2h2ljNfFKfs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710864089; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qx6jzOg2cxJWYprYcXEGlkqao+7HU/sQ9hZna19awc4=; b=UXXbgdBDjU8HxgPArq+k4O6P9Qe6zQnnhgD1qKEUIO6bHsYOkSp+4nQsmqvq4lIsH6+5W7 jRH5LWwg4KV2XfTioeOX0ltGZ4TvnvZpk5y8drDI2KH46M8TtyMostkIAwFmefs1TeeUu6 z5h9iLMVaCK31XDu9SZmoQD2+gJcLF4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-310-1pdIKlhuOwaJ7kXy2Kd1xg-1; Tue, 19 Mar 2024 12:01:26 -0400 X-MC-Unique: 1pdIKlhuOwaJ7kXy2Kd1xg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 20D243800E8B; Tue, 19 Mar 2024 16:01:26 +0000 (UTC) Received: from localhost (unknown [10.42.28.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id E280317A91; Tue, 19 Mar 2024 16:01:25 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix infinite loop in std::binomial_distribution [PR114359] Date: Tue, 19 Mar 2024 16:00:46 +0000 Message-ID: <20240319160121.3408014-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. Not a regression, but worth backporting. -- >8 -- The multiplication (4 * _M_t * __1p) can wraparound to zero if _M_t is unsigned and 4 * _M_t wraps to zero. The third operand has type double, so do the second multiplication first, so that we aren't multiplying integers. libstdc++-v3/ChangeLog: PR libstdc++/114359 * include/bits/random.tcc (binomial_distribution::param_type): Ensure arithmetic is done as type double. * testsuite/26_numerics/random/binomial_distribution/114359.cc: New test. --- libstdc++-v3/include/bits/random.tcc | 2 +- .../random/binomial_distribution/114359.cc | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/26_numerics/random/binomial_distribution/114359.cc diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index ade416390b3..8216883c448 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -1503,7 +1503,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // sqrt(pi / 2) const double __spi_2 = 1.2533141373155002512078826424055226L; _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np)); - _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p)); + _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * (_M_t * __1p))); _M_c = 2 * _M_d1 / __np; _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2; const double __a12 = _M_a1 + _M_s2 * __spi_2; diff --git a/libstdc++-v3/testsuite/26_numerics/random/binomial_distribution/114359.cc b/libstdc++-v3/testsuite/26_numerics/random/binomial_distribution/114359.cc new file mode 100644 index 00000000000..c1e4c380bf9 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/binomial_distribution/114359.cc @@ -0,0 +1,12 @@ +// { dg-do run { target c++11 } } + +// Bug 114359 - std::binomial_distribution hangs in infinite loop + +#include + +int main() +{ + std::default_random_engine g{}; + std::binomial_distribution b(1U << 30); + b(g); // hangs forever +}