summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Tuckman <ted.tuckman@mongodb.com>2018-09-07 15:42:20 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-04-06 14:11:31 +0000
commitfc3f626537400cf3b6dd89daf9ee8f50c45e03cb (patch)
tree95baabb10061e9a3a68b4c26fc50f66522d4aca2
parent0395f6fae741624906529958d091c8762e72b594 (diff)
downloadmongo-fc3f626537400cf3b6dd89daf9ee8f50c45e03cb.tar.gz
SERVER-36926 Remove possibility of passing INT_MIN to abs() in ClusterCursorManager
(cherry picked from commit 0e5b2946e1750fd4a5ac30cdc6d67d6fda94d378) (cherry picked from commit 368553479c74478d15204383289d1291ad097909)
-rw-r--r--src/mongo/s/query/cluster_cursor_manager.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/mongo/s/query/cluster_cursor_manager.cpp b/src/mongo/s/query/cluster_cursor_manager.cpp
index 6ed2bc89e40..657e21850ec 100644
--- a/src/mongo/s/query/cluster_cursor_manager.cpp
+++ b/src/mongo/s/query/cluster_cursor_manager.cpp
@@ -245,8 +245,13 @@ StatusWith<CursorId> ClusterCursorManager::registerCursor(
do {
// The server has always generated positive values for CursorId (which is a signed
// type), so we use std::abs() here on the prefix for consistency with this historical
- // behavior.
- containerPrefix = static_cast<uint32_t>(std::abs(_pseudoRandom.nextInt32()));
+ // behavior. If the random number generated is INT_MIN, calling std::abs on it is
+ // undefined behavior on 2's complement systems so we need to generate a new number.
+ int32_t randomNumber = 0;
+ do {
+ randomNumber = _pseudoRandom.nextInt32();
+ } while (randomNumber == std::numeric_limits<int32_t>::min());
+ containerPrefix = static_cast<uint32_t>(std::abs(randomNumber));
} while (_cursorIdPrefixToNamespaceMap.count(containerPrefix) > 0);
_cursorIdPrefixToNamespaceMap[containerPrefix] = nss;