diff options
Diffstat (limited to 'libcxx/include/__random/negative_binomial_distribution.h')
| -rw-r--r-- | libcxx/include/__random/negative_binomial_distribution.h | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/libcxx/include/__random/negative_binomial_distribution.h b/libcxx/include/__random/negative_binomial_distribution.h index 72ce88ea74ba..90d3f0178914 100644 --- a/libcxx/include/__random/negative_binomial_distribution.h +++ b/libcxx/include/__random/negative_binomial_distribution.h @@ -29,7 +29,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD template<class _IntType = int> class _LIBCPP_TEMPLATE_VIS negative_binomial_distribution { - static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be an integer type larger than char"); + static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type"); public: // types typedef _IntType result_type; @@ -121,7 +121,9 @@ negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_ static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); result_type __k = __pr.k(); double __p = __pr.p(); - if (__k <= 21 * __p) + // When the number of bits in _IntType is small, we are too likely to + // overflow __f below to use this technique. + if (__k <= 21 * __p && sizeof(_IntType) > 1) { bernoulli_distribution __gen(__p); result_type __f = 0; @@ -133,6 +135,8 @@ negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_ else ++__f; } + _LIBCPP_ASSERT(__f >= 0, "std::negative_binomial_distribution should never produce negative values. " + "This is almost certainly a signed integer overflow issue on __f."); return __f; } return poisson_distribution<result_type>(gamma_distribution<double> |
