Guard std::numeric_limits::min and max for MSVC

Also see GH #1214
This commit is contained in:
Jeffrey Walton 2023-06-23 15:07:13 -04:00
parent a5f47381b5
commit cc920825d2
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 21 additions and 7 deletions

View File

@ -1267,7 +1267,7 @@ if true; then
echo "Testing: C++ std::min and std::max" | tee -a "$TEST_RESULTS"
echo
TEST_LIST+=("C++ std::min and std::max")
TEST_LIST+=("C++ std::min, std::max")
FAILED=0
# If this fires, then use the library's STDMIN(a,b) or (std::min)(a, b);
@ -1284,6 +1284,20 @@ if true; then
echo "FAILED: found std::max" | tee -a "$TEST_RESULTS"
fi
# If this fires, then use the library's STDMIN(a,b) or (std::min)(a, b);
COUNT=$(cat ./*.h ./*.cpp | "${GREP}" -v '//' | "${GREP}" -c -E 'std::numeric_limits<.*>::min[[:space:]]*\(')
if [[ "$COUNT" -ne 0 ]]; then
FAILED=1
echo "FAILED: found std::numeric_limits<T>::min" | tee -a "$TEST_RESULTS"
fi
# If this fires, then use the library's STDMAX(a,b) or (std::max)(a, b);
COUNT=$(cat ./*.h ./*.cpp | "${GREP}" -v '//' | "${GREP}" -c -E 'std::numeric_limits<.*>::max[[:space:]]*\(')
if [[ "$COUNT" -ne 0 ]]; then
FAILED=1
echo "FAILED: found std::numeric_limits<T>::max" | tee -a "$TEST_RESULTS"
fi
if [[ ("$FAILED" -eq 0) ]]; then
echo "Verified std::min and std::max" | tee -a "$TEST_RESULTS"
else

View File

@ -442,7 +442,7 @@ void ChaChaTLS_Policy::SeekToIteration(lword iterationCount)
// large then we can wrap and process more data as long as
// data processed in the security context does not exceed
// 2^32 blocks or approximately 256 GB of data.
CRYPTOPP_ASSERT(iterationCount <= std::numeric_limits<word32>::max());
CRYPTOPP_ASSERT(iterationCount <= (std::numeric_limits<word32>::max)());
m_state[12] = (word32)iterationCount; // low word
}

View File

@ -200,7 +200,7 @@ void Scrypt::ValidateParameters(size_t derivedLen, word64 cost, word64 blockSize
throw InvalidArgument("Scrypt: parallelization cannot be 0");
// Optimizer should remove this on 32-bit platforms
if (std::numeric_limits<size_t>::max() > std::numeric_limits<word32>::max())
if ((std::numeric_limits<size_t>::max)() > (std::numeric_limits<word32>::max)())
{
const word64 maxLen = ((static_cast<word64>(1) << 32) - 1) * 32;
if (derivedLen > maxLen) {
@ -211,12 +211,12 @@ void Scrypt::ValidateParameters(size_t derivedLen, word64 cost, word64 blockSize
}
// https://github.com/weidai11/cryptopp/issues/787
CRYPTOPP_ASSERT(parallelization <= static_cast<word64>(std::numeric_limits<int>::max()));
if (parallelization > static_cast<word64>(std::numeric_limits<int>::max()))
CRYPTOPP_ASSERT(parallelization <= static_cast<word64>((std::numeric_limits<int>::max)()));
if (parallelization > static_cast<word64>((std::numeric_limits<int>::max)()))
{
std::ostringstream oss;
oss << " parallelization " << parallelization << " is larger than ";
oss << std::numeric_limits<int>::max();
oss << (std::numeric_limits<int>::max)();
throw InvalidArgument("Scrypt: " + oss.str());
}
@ -297,7 +297,7 @@ size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen, const byte*secret, siz
// Visual Studio and OpenMP 2.0 fixup. We must use int, not size_t.
int maxParallel=0;
if (!SafeConvert(parallel, maxParallel))
maxParallel = std::numeric_limits<int>::max();
maxParallel = (std::numeric_limits<int>::max)();
#ifdef _OPENMP
int threads = STDMIN(omp_get_max_threads(), maxParallel);