Skip to content

Commit cc92082

Browse files
committed
Guard std::numeric_limits::min and max for MSVC
Also see GH weidai11#1214
1 parent a5f4738 commit cc92082

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

TestScripts/cryptest.sh

+15-1
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ if true; then
12671267
echo "Testing: C++ std::min and std::max" | tee -a "$TEST_RESULTS"
12681268
echo
12691269

1270-
TEST_LIST+=("C++ std::min and std::max")
1270+
TEST_LIST+=("C++ std::min, std::max")
12711271
FAILED=0
12721272

12731273
# If this fires, then use the library's STDMIN(a,b) or (std::min)(a, b);
@@ -1284,6 +1284,20 @@ if true; then
12841284
echo "FAILED: found std::max" | tee -a "$TEST_RESULTS"
12851285
fi
12861286

1287+
# If this fires, then use the library's STDMIN(a,b) or (std::min)(a, b);
1288+
COUNT=$(cat ./*.h ./*.cpp | "${GREP}" -v '//' | "${GREP}" -c -E 'std::numeric_limits<.*>::min[[:space:]]*\(')
1289+
if [[ "$COUNT" -ne 0 ]]; then
1290+
FAILED=1
1291+
echo "FAILED: found std::numeric_limits<T>::min" | tee -a "$TEST_RESULTS"
1292+
fi
1293+
1294+
# If this fires, then use the library's STDMAX(a,b) or (std::max)(a, b);
1295+
COUNT=$(cat ./*.h ./*.cpp | "${GREP}" -v '//' | "${GREP}" -c -E 'std::numeric_limits<.*>::max[[:space:]]*\(')
1296+
if [[ "$COUNT" -ne 0 ]]; then
1297+
FAILED=1
1298+
echo "FAILED: found std::numeric_limits<T>::max" | tee -a "$TEST_RESULTS"
1299+
fi
1300+
12871301
if [[ ("$FAILED" -eq 0) ]]; then
12881302
echo "Verified std::min and std::max" | tee -a "$TEST_RESULTS"
12891303
else

chacha.cpp

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

scrypt.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void Scrypt::ValidateParameters(size_t derivedLen, word64 cost, word64 blockSize
200200
throw InvalidArgument("Scrypt: parallelization cannot be 0");
201201

202202
// Optimizer should remove this on 32-bit platforms
203-
if (std::numeric_limits<size_t>::max() > std::numeric_limits<word32>::max())
203+
if ((std::numeric_limits<size_t>::max)() > (std::numeric_limits<word32>::max)())
204204
{
205205
const word64 maxLen = ((static_cast<word64>(1) << 32) - 1) * 32;
206206
if (derivedLen > maxLen) {
@@ -211,12 +211,12 @@ void Scrypt::ValidateParameters(size_t derivedLen, word64 cost, word64 blockSize
211211
}
212212

213213
// https://github.com/weidai11/cryptopp/issues/787
214-
CRYPTOPP_ASSERT(parallelization <= static_cast<word64>(std::numeric_limits<int>::max()));
215-
if (parallelization > static_cast<word64>(std::numeric_limits<int>::max()))
214+
CRYPTOPP_ASSERT(parallelization <= static_cast<word64>((std::numeric_limits<int>::max)()));
215+
if (parallelization > static_cast<word64>((std::numeric_limits<int>::max)()))
216216
{
217217
std::ostringstream oss;
218218
oss << " parallelization " << parallelization << " is larger than ";
219-
oss << std::numeric_limits<int>::max();
219+
oss << (std::numeric_limits<int>::max)();
220220
throw InvalidArgument("Scrypt: " + oss.str());
221221
}
222222

@@ -297,7 +297,7 @@ size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen, const byte*secret, siz
297297
// Visual Studio and OpenMP 2.0 fixup. We must use int, not size_t.
298298
int maxParallel=0;
299299
if (!SafeConvert(parallel, maxParallel))
300-
maxParallel = std::numeric_limits<int>::max();
300+
maxParallel = (std::numeric_limits<int>::max)();
301301

302302
#ifdef _OPENMP
303303
int threads = STDMIN(omp_get_max_threads(), maxParallel);

0 commit comments

Comments
 (0)