From 88f94ed358e3489fdad0275b2ddb89872fcddd1a Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 19 Jul 2015 14:35:30 -0400 Subject: [PATCH] Cleared behavior that relied upon undefined behavior --- misc.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/misc.h b/misc.h index a6a8dea1..df04ce6d 100644 --- a/misc.h +++ b/misc.h @@ -4,6 +4,7 @@ #include "cryptlib.h" #include "smartptr.h" #include // for memcpy and memmove +#include // for numeric_limits #ifdef _MSC_VER #if _MSC_VER >= 1400 @@ -392,8 +393,8 @@ inline T1 RoundDownToMultipleOf(const T1 &n, const T2 &m) template inline T1 RoundUpToMultipleOf(const T1 &n, const T2 &m) { - // TODO: undefined behavior here... - if (n+m-1 < n) + const size_t limit = std::numeric_limits::max(); + if (n > limit-m) throw InvalidArgument("RoundUpToMultipleOf: integer overflow"); return RoundDownToMultipleOf(n+m-1, m); }