From cc920825d2dab437c8d628f3cc909bcb694ba560 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 23 Jun 2023 15:07:13 -0400 Subject: [PATCH] Guard std::numeric_limits::min and max for MSVC Also see GH #1214 --- TestScripts/cryptest.sh | 16 +++++++++++++++- chacha.cpp | 2 +- scrypt.cpp | 10 +++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/TestScripts/cryptest.sh b/TestScripts/cryptest.sh index d76d5c244..21e3a15e1 100755 --- a/TestScripts/cryptest.sh +++ b/TestScripts/cryptest.sh @@ -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::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::max" | tee -a "$TEST_RESULTS" + fi + if [[ ("$FAILED" -eq 0) ]]; then echo "Verified std::min and std::max" | tee -a "$TEST_RESULTS" else diff --git a/chacha.cpp b/chacha.cpp index 5c50d52c1..d6c12ab9c 100644 --- a/chacha.cpp +++ b/chacha.cpp @@ -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::max()); + CRYPTOPP_ASSERT(iterationCount <= (std::numeric_limits::max)()); m_state[12] = (word32)iterationCount; // low word } diff --git a/scrypt.cpp b/scrypt.cpp index 37bc6b7a3..6b205c9b4 100644 --- a/scrypt.cpp +++ b/scrypt.cpp @@ -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::max() > std::numeric_limits::max()) + if ((std::numeric_limits::max)() > (std::numeric_limits::max)()) { const word64 maxLen = ((static_cast(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(std::numeric_limits::max())); - if (parallelization > static_cast(std::numeric_limits::max())) + CRYPTOPP_ASSERT(parallelization <= static_cast((std::numeric_limits::max)())); + if (parallelization > static_cast((std::numeric_limits::max)())) { std::ostringstream oss; oss << " parallelization " << parallelization << " is larger than "; - oss << std::numeric_limits::max(); + oss << (std::numeric_limits::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::max(); + maxParallel = (std::numeric_limits::max)(); #ifdef _OPENMP int threads = STDMIN(omp_get_max_threads(), maxParallel);