From d4c7c4009da14c84576d43ab4a1241967cfa5ffc Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Fri, 24 May 2024 11:55:53 -0400 Subject: [PATCH] init: error out if -maxconnections is negative --- src/init.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 39abea3895c..3c4b72d3e4f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -994,12 +994,14 @@ bool AppInitParameterInteraction(const ArgsManager& args) int nBind = std::max(nUserBind, size_t(1)); // Maximum number of connections with other nodes, this accounts for all types of outbounds and inbounds except for manual int user_max_connection = args.GetIntArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS); - nMaxConnections = std::max(user_max_connection, 0); + if (user_max_connection < 0) { + return InitError(Untranslated("-maxconnections must be greater or equal than zero")); + } // Reserve enough FDs to account for the bare minimum, plus any manual connections, plus the bound interfaces int min_required_fds = MIN_CORE_FDS + MAX_ADDNODE_CONNECTIONS + nBind; // Try raising the FD limit to what we need (available_fds may be smaller than the requested amount if this fails) - available_fds = RaiseFileDescriptorLimit(nMaxConnections + min_required_fds); + available_fds = RaiseFileDescriptorLimit(user_max_connection + min_required_fds); // If we are using select instead of poll, our actual limit may be even smaller #ifndef USE_POLL available_fds = std::min(FD_SETSIZE, available_fds); @@ -1008,7 +1010,7 @@ bool AppInitParameterInteraction(const ArgsManager& args) return InitError(strprintf(_("Not enough file descriptors available. %d available, %d required."), available_fds, min_required_fds)); // Trim requested connection counts, to fit into system limitations - nMaxConnections = std::min(available_fds - min_required_fds, nMaxConnections); + nMaxConnections = std::min(available_fds - min_required_fds, user_max_connection); if (nMaxConnections < user_max_connection) InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), user_max_connection, nMaxConnections));