From 368553479c74478d15204383289d1291ad097909 Mon Sep 17 00:00:00 2001 From: Ted Tuckman Date: Fri, 7 Sep 2018 15:42:20 -0400 Subject: SERVER-36926 Remove possibility of passing INT_MIN to abs() in ClusterCursorManager (cherry picked from commit 0e5b2946e1750fd4a5ac30cdc6d67d6fda94d378) --- src/mongo/s/query/cluster_cursor_manager.cpp | 9 +++++++-- 1 file 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 5c267bde0a1..3524e8b1e31 100644 --- a/src/mongo/s/query/cluster_cursor_manager.cpp +++ b/src/mongo/s/query/cluster_cursor_manager.cpp @@ -249,8 +249,13 @@ StatusWith 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(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::min()); + containerPrefix = static_cast(std::abs(randomNumber)); } while (_cursorIdPrefixToNamespaceMap.count(containerPrefix) > 0); _cursorIdPrefixToNamespaceMap[containerPrefix] = nss; -- cgit v1.2.1