[gcc r9-9369] libstdc++: Add assertions for preconditions in sampling distributions [PR 82584]

Jonathan Wakely redi@gcc.gnu.org
Tue Apr 20 18:52:18 GMT 2021


https://gcc.gnu.org/g:31a5cc60428b968c9ff3f505fcd1a5ef5c0083bb

commit r9-9369-g31a5cc60428b968c9ff3f505fcd1a5ef5c0083bb
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 9e0f1322f33..0a299baedc5 100644
--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -2661,6 +2661,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);
@@ -2827,6 +2828,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);
@@ -3051,6 +3053,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 b360203adf4..599d53665f3 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 *-*-* } 156 }
 
-// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3318 }
+// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3321 }


More information about the Libstdc++-cvs mailing list