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.133.124]) by sourceware.org (Postfix) with ESMTPS id 59A3E3858430 for ; Tue, 19 Mar 2024 16:01:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 59A3E3858430 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 59A3E3858430 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=1710864089; cv=none; b=WIKMxYEx8rYdjxErFoTskldrF7hq7TdBaL7+DKengf3pTV4ZV9lA81jyl/YJhbdCxRfn2xXObQpv/Sw1S2fza794RSDDUB9oVC6gse/GQXLhWlHo+MtjU0Unsgv4W6kk/pO1LIJfmP2C7IQ0bSR0Fp/52VpakV3dyYzza8IbYYU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710864089; c=relaxed/simple; bh=S9X7PwZdRWbp5FfjhGh7rk5k3EwlNxgPQokcywaqsZc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QqBDuuBzA7D8xkIh5GFmT5TyuNpWA8Y1unU48tmETEPGDZ/NB68Lw4R+/r0rTsMGGUrzPjO7bROMddg8yFdgdV21TAztHpEpbRU4NsRIW4xUoz2fURn7BNbwaBU2HoeYJ7isnx/UTDkW0698nt9GIUYu3HnlvxIwgml0vSMDEp4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710864088; 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=EdaCstapl9O+VSbEi2o/l33pra2eF/7yyd358IgXdYjRQZAHnb0HFRB7XuHbM6FrmfzyA9 d1Drfh7w3CRC/GIZAnt6zMuN5BvVFZxOEIbEDgixUjThxGDC5S7Wsc7I4ATfW181359dnp jbXVJN5r0EeruetI3cRHG8XurmjktAI= 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 +} -- 2.44.0