From c35d6ed17e47172a751e794c1d1056e70b6c5c12 Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Thu, 27 Nov 2025 17:44:52 +0100 Subject: [PATCH] fix(MongoDB): Fixes based on static analysis report. --- MongoDB/src/ReplicaSetConnection.cpp | 50 ++++++++++++++-------------- MongoDB/src/TopologyDescription.cpp | 7 ++-- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/MongoDB/src/ReplicaSetConnection.cpp b/MongoDB/src/ReplicaSetConnection.cpp index 26fa8bd19..977c5b266 100644 --- a/MongoDB/src/ReplicaSetConnection.cpp +++ b/MongoDB/src/ReplicaSetConnection.cpp @@ -23,20 +23,20 @@ namespace MongoDB { // MongoDB error codes that indicate retriable errors -namespace ErrorCodes +enum class ErrorCode { - const int NotMaster = 10107; - const int NotMasterNoSlaveOk = 13435; - const int NotMasterOrSecondary = 13436; - const int InterruptedAtShutdown = 11600; - const int InterruptedDueToReplStateChange = 11602; - const int PrimarySteppedDown = 189; - const int ShutdownInProgress = 91; - const int HostNotFound = 7; - const int HostUnreachable = 6; - const int NetworkTimeout = 89; - const int SocketException = 9001; -} + NotMaster = 10107, + NotMasterNoSlaveOk = 13435, + NotMasterOrSecondary = 13436, + InterruptedAtShutdown = 11600, + InterruptedDueToReplStateChange = 11602, + PrimarySteppedDown = 189, + ShutdownInProgress = 91, + HostNotFound = 7, + HostUnreachable = 6, + NetworkTimeout = 89, + SocketException = 9001 +}; ReplicaSetConnection::ReplicaSetConnection(ReplicaSet& replicaSet, const ReadPreference& readPref): @@ -266,21 +266,21 @@ bool ReplicaSetConnection::isRetriableMongoDBError(const OpMsgMessage& response) // Check for error code if (body.exists("code")) { - int code = body.get("code"); + ErrorCode code = static_cast(body.get("code")); switch (code) { - case ErrorCodes::NotMaster: - case ErrorCodes::NotMasterNoSlaveOk: - case ErrorCodes::NotMasterOrSecondary: - case ErrorCodes::InterruptedAtShutdown: - case ErrorCodes::InterruptedDueToReplStateChange: - case ErrorCodes::PrimarySteppedDown: - case ErrorCodes::ShutdownInProgress: - case ErrorCodes::HostNotFound: - case ErrorCodes::HostUnreachable: - case ErrorCodes::NetworkTimeout: - case ErrorCodes::SocketException: + case ErrorCode::NotMaster: + case ErrorCode::NotMasterNoSlaveOk: + case ErrorCode::NotMasterOrSecondary: + case ErrorCode::InterruptedAtShutdown: + case ErrorCode::InterruptedDueToReplStateChange: + case ErrorCode::PrimarySteppedDown: + case ErrorCode::ShutdownInProgress: + case ErrorCode::HostNotFound: + case ErrorCode::HostUnreachable: + case ErrorCode::NetworkTimeout: + case ErrorCode::SocketException: return true; default: return false; diff --git a/MongoDB/src/TopologyDescription.cpp b/MongoDB/src/TopologyDescription.cpp index bac45056d..b2c36589d 100644 --- a/MongoDB/src/TopologyDescription.cpp +++ b/MongoDB/src/TopologyDescription.cpp @@ -224,7 +224,7 @@ void TopologyDescription::addServer(const Net::SocketAddress& address) { Mutex::ScopedLock lock(_mutex); - auto [it, inserted] = _servers.try_emplace(address, address); + auto [_, inserted] = _servers.try_emplace(address, address); if (inserted) { updateTopologyType(); @@ -271,7 +271,6 @@ void TopologyDescription::updateTopologyType() int primaries = 0; int secondaries = 0; int mongosCount = 0; - int unknownCount = 0; int standaloneCount = 0; for (const auto& [address, server] : _servers) @@ -294,7 +293,7 @@ void TopologyDescription::updateTopologyType() standaloneCount++; break; case ServerDescription::Unknown: - unknownCount++; + // Unknown servers don't affect topology classification break; } } @@ -306,6 +305,8 @@ void TopologyDescription::updateTopologyType() } else if (standaloneCount > 0 && _servers.size() == 1) { + // Single standalone server - treat as Single topology + // Standalone servers behave like a single primary for read preferences _type = Single; } else if (primaries > 0)