Cleared behavior that relied upon undefined behavior

pull/35/head
Jeffrey Walton 2015-07-19 14:35:30 -04:00
parent a56f07709e
commit 88f94ed358
1 changed files with 3 additions and 2 deletions

5
misc.h
View File

@ -4,6 +4,7 @@
#include "cryptlib.h"
#include "smartptr.h"
#include <string.h> // for memcpy and memmove
#include <limits> // for numeric_limits
#ifdef _MSC_VER
#if _MSC_VER >= 1400
@ -392,8 +393,8 @@ inline T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
template <class T1, class T2>
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<T1>::max();
if (n > limit-m)
throw InvalidArgument("RoundUpToMultipleOf: integer overflow");
return RoundDownToMultipleOf(n+m-1, m);
}