Skip to content

Commit

Permalink
Guard std::numeric_limits::min and max for MSVC
Browse files Browse the repository at this point in the history
Also see GH #1214
  • Loading branch information
noloader committed Jun 23, 2023
1 parent a5f4738 commit cc92082
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
16 changes: 15 additions & 1 deletion TestScripts/cryptest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion chacha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions scrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cc92082

Please sign in to comment.