refactor: Use constexpr in torcontrol where possible
What changed, and why it matters
This commit is a straightforward code cleanup in Bitcoin Core's Tor control module. It changes several constant variable declarations from 'static const' to 'constexpr' for integer and float values. This is a non-functional style/refactoring change with no security relevance.
No action required. This is a benign refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies src/torcontrol.cpp to replace six ‘static const’ declarations with ‘constexpr’ for TOR_COOKIE_SIZE, TOR_NONCE_SIZE, RECONNECT_TIMEOUT_START, RECONNECT_TIMEOUT_EXP, RECONNECT_TIMEOUT_MAX, and MAX_LINE_LENGTH. The values remain identical. Two std::string constants are intentionally left as ‘static const’ because std::string cannot be constexpr. This is a pure refactoring change with no behavioral, API, or security impact.
Changed components
src/torcontrol.cppInspect captured patch +6 / −6
diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp
index b948de3e..9abc1fc0 100644
--- a/src/torcontrol.cpp
+++ b/src/torcontrol.cpp
@@ -50,24 +50,24 @@ using util::ToString;
/** Default control ip and port */
const std::string DEFAULT_TOR_CONTROL = "127.0.0.1:" + ToString(DEFAULT_TOR_CONTROL_PORT);
/** Tor cookie size (from control-spec.txt) */
-static const int TOR_COOKIE_SIZE = 32;
+constexpr int TOR_COOKIE_SIZE = 32;
/** Size of client/server nonce for SAFECOOKIE */
-static const int TOR_NONCE_SIZE = 32;
+constexpr int TOR_NONCE_SIZE = 32;
/** For computing serverHash in SAFECOOKIE */
static const std::string TOR_SAFE_SERVERKEY = "Tor safe cookie authentication server-to-controller hash";
/** For computing clientHash in SAFECOOKIE */
static const std::string TOR_SAFE_CLIENTKEY = "Tor safe cookie authentication controller-to-server hash";
/** Exponential backoff configuration - initial timeout in seconds */
-static const float RECONNECT_TIMEOUT_START = 1.0;
+constexpr float RECONNECT_TIMEOUT_START = 1.0;
/** Exponential backoff configuration - growth factor */
-static const float RECONNECT_TIMEOUT_EXP = 1.5;
+constexpr float RECONNECT_TIMEOUT_EXP = 1.5;
/** Maximum reconnect timeout in seconds to prevent excessive delays */
-static const float RECONNECT_TIMEOUT_MAX = 600.0;
+constexpr float RECONNECT_TIMEOUT_MAX = 600.0;
/** Maximum length for lines received on TorControlConnection.
* tor-control-spec.txt mentions that there is explicitly no limit defined to line length,
* this is belt-and-suspenders sanity limit to prevent memory exhaustion.
*/
-static const int MAX_LINE_LENGTH = 100000;
+constexpr int MAX_LINE_LENGTH = 100000;
/****** Low-level TorControlConnection ********/
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.