public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r10-9572] libstdc++: Add assertions for preconditions in sampling distributions [PR 82584]
Date: Mon, 29 Mar 2021 20:01:32 +0000 (GMT)	[thread overview]
Message-ID: <20210329200132.D224B3851C3E@sourceware.org> (raw)

https://gcc.gnu.org/g:56b810af1bdf106b132dcf1f1a01573af6a3468b

commit r10-9572-g56b810af1bdf106b132dcf1f1a01573af6a3468b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Oct 8 15:14:58 2020 +0100

    libstdc++: Add assertions for preconditions in sampling distributions [PR 82584]
    
    These three distributions all require 0 < S where S is the sum of the
    weights. When the sum is zero there's an undefined FP division by zero.
    Add assertions to help users diagnose the problem.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/82584
            * include/bits/random.tcc
            (discrete_distribution::param_type::_M_initialize)
            (piecewise_constant_distribution::param_type::_M_initialize)
            (piecewise_linear_distribution::param_type::_M_initialize):
            Add assertions for positive sums..
            * testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error
            line.
    
    (cherry picked from commit b2a96bf9dce41ee777b1669e4a8b4c6df3ff3613)

Diff:
---
 libstdc++-v3/include/bits/random.tcc                     | 3 +++
 libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc
index f67b2fe48f4..a921b9bf815 100644
--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -2630,6 +2630,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       const double __sum = std::accumulate(_M_prob.begin(),
 					   _M_prob.end(), 0.0);
+      __glibcxx_assert(__sum > 0);
       // Now normalize the probabilites.
       __detail::__normalize(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
 			    __sum);
@@ -2794,6 +2795,7 @@ namespace __detail
 
       const double __sum = std::accumulate(_M_den.begin(),
 					   _M_den.end(), 0.0);
+      __glibcxx_assert(__sum > 0);
 
       __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
 			    __sum);
@@ -3016,6 +3018,7 @@ namespace __detail
 	  _M_cp.push_back(__sum);
 	  _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
 	}
+      __glibcxx_assert(__sum > 0);
 
       //  Now normalize the densities...
       __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
diff --git a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
index 139abbb3051..9cffc3d06f9 100644
--- a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
+++ b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
@@ -12,4 +12,4 @@ auto x = std::generate_canonical<std::size_t,
 
 // { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 167 }
 
-// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3281 }
+// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3284 }


                 reply	other threads:[~2021-03-29 20:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210329200132.D224B3851C3E@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).