summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Morrow <acm@10gen.com>2013-08-19 16:57:23 -0400
committerAndrew Morrow <acm@10gen.com>2013-08-20 11:48:46 -0400
commit4a6f0941c13ac6a197bb787e238ddbc12120b641 (patch)
tree0f517f8a4cab699a3f1a1d63cb199b32aa41fa21
parent95f78edec079e130c58bed831cfe341f45250784 (diff)
downloadmongo-4a6f0941c13ac6a197bb787e238ddbc12120b641.tar.gz
SERVER-10490 SERVER-375 Add static assertions for overflow safe algos
-rw-r--r--src/mongo/util/safe_num.cpp7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/mongo/util/safe_num.cpp b/src/mongo/util/safe_num.cpp
index 4b26ca4f3e4..90578896ee3 100644
--- a/src/mongo/util/safe_num.cpp
+++ b/src/mongo/util/safe_num.cpp
@@ -14,6 +14,7 @@
*/
#include <sstream>
+#include <boost/static_assert.hpp>
#include "mongo/pch.h" // for malloc/realloc/INFINITY pulled from bson
@@ -163,6 +164,9 @@ namespace mongo {
// integers. Then, if we fall within the allowable range of int, we downcast,
// otherwise, we retain the 64-bit result.
+ // This algorithm is only correct if sizeof(long long) > sizeof(int)
+ BOOST_STATIC_ASSERT(sizeof(long long) > sizeof(int));
+
const long long int result =
static_cast<long long int>(lInt32) +
static_cast<long long int>(rInt32);
@@ -204,6 +208,9 @@ namespace mongo {
// integers. Then, if we fall within the allowable range of int, we downcast,
// otherwise, we retain the 64-bit result.
+ // This algorithm is only correct if sizeof(long long) >= (2 * sizeof(int))
+ BOOST_STATIC_ASSERT(sizeof(long long) >= (2 * sizeof(int)));
+
const long long int result =
static_cast<long long int>(lInt32) *
static_cast<long long int>(rInt32);