summaryrefslogtreecommitdiffstats
path: root/libcxx/include/algorithm
diff options
context:
space:
mode:
authorMarshall Clow <mclow.lists@gmail.com>2018-10-08 20:20:34 +0000
committerMarshall Clow <mclow.lists@gmail.com>2018-10-08 20:20:34 +0000
commit2dd5335204da085718e926d839f3200dc5227fc9 (patch)
tree3a60541f65532e8ad7094912247e87943a8fb7b9 /libcxx/include/algorithm
parentff9f02580dcc33bb2d1e313800c4fafd4b5e2654 (diff)
downloadbcm5719-llvm-2dd5335204da085718e926d839f3200dc5227fc9.tar.gz
bcm5719-llvm-2dd5335204da085718e926d839f3200dc5227fc9.zip
Do the math in uniform_int_distribution::operator() as unsigned to prevent UB when overflowing. Also add a UBSAN notification that we're ffine with unsigned overflow. This fixes PR#32617. Thanks to Vincent & Christoph for their help with this issue.
llvm-svn: 343996
Diffstat (limited to 'libcxx/include/algorithm')
-rw-r--r--libcxx/include/algorithm5
1 files changed, 3 insertions, 2 deletions
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index c5c17296e68..9ce6aa011db 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -2899,10 +2899,11 @@ template<class _IntType>
template<class _URNG>
typename uniform_int_distribution<_IntType>::result_type
uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
+_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
{
typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
uint32_t, uint64_t>::type _UIntType;
- const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
+ const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
if (_Rp == 1)
return __p.a();
const size_t _Dt = numeric_limits<_UIntType>::digits;
@@ -2989,7 +2990,7 @@ random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
{
difference_type __i = __rand(__d);
if (__i != difference_type(0))
- swap(*__first, *(__first + __i));
+ swap(*__first, *(__first + __i));
}
}
}
OpenPOWER on IntegriCloud