Secure __next_prime from overflowing
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@117650 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
4e59948249
commit
e87ad178cc
24
src/hash.cpp
24
src/hash.cpp
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include "__hash_table"
|
#include "__hash_table"
|
||||||
#include "algorithm"
|
#include "algorithm"
|
||||||
|
#include "stdexcept"
|
||||||
|
|
||||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||||
|
|
||||||
@ -143,6 +144,26 @@ const unsigned indices[] =
|
|||||||
// are fewer potential primes to search, and fewer potential primes to divide
|
// are fewer potential primes to search, and fewer potential primes to divide
|
||||||
// against.
|
// against.
|
||||||
|
|
||||||
|
inline _LIBCPP_INLINE_VISIBILITY
|
||||||
|
void
|
||||||
|
__check_for_overflow(size_t N, integral_constant<size_t, 32>)
|
||||||
|
{
|
||||||
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
||||||
|
if (N > 0xFFFFFFFB)
|
||||||
|
throw overflow_error("__next_prime overflow");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
inline _LIBCPP_INLINE_VISIBILITY
|
||||||
|
void
|
||||||
|
__check_for_overflow(size_t N, integral_constant<size_t, 64>)
|
||||||
|
{
|
||||||
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
||||||
|
if (N > 0xFFFFFFFFFFFFFFC5ull)
|
||||||
|
throw overflow_error("__next_prime overflow");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
__next_prime(size_t n)
|
__next_prime(size_t n)
|
||||||
{
|
{
|
||||||
@ -152,6 +173,9 @@ __next_prime(size_t n)
|
|||||||
if (n <= small_primes[N-1])
|
if (n <= small_primes[N-1])
|
||||||
return *std::lower_bound(small_primes, small_primes + N, n);
|
return *std::lower_bound(small_primes, small_primes + N, n);
|
||||||
// Else n > largest small_primes
|
// Else n > largest small_primes
|
||||||
|
// Check for overflow
|
||||||
|
__check_for_overflow(n, integral_constant<size_t,
|
||||||
|
sizeof(n) * __CHAR_BIT__>());
|
||||||
// Start searching list of potential primes: L * k0 + indices[in]
|
// Start searching list of potential primes: L * k0 + indices[in]
|
||||||
const size_t M = sizeof(indices) / sizeof(indices[0]);
|
const size_t M = sizeof(indices) / sizeof(indices[0]);
|
||||||
// Select first potential prime >= n
|
// Select first potential prime >= n
|
||||||
|
Loading…
x
Reference in New Issue
Block a user