refactor: Use std::bind_front over std::bind
What changed, and why it matters
This commit is a straightforward code cleanup that replaces older-style std::bind calls with the newer C++20 std::bind_front. It does not change what the program does, only how the code is written. There is no security issue here.
No security action needed. This is a routine code-quality refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors callback binding in walletmodel.cpp, scheduler_tests.cpp, and torcontrol.cpp from std::bind with explicit placeholders to std::bind_front, which binds leading arguments more cleanly. The functional behavior, argument order, and object lifetimes remain unchanged. No security-sensitive logic is modified.
Changed components
src/qt/walletmodel.cppsrc/test/scheduler_tests.cppsrc/torcontrol.cppInspect captured patch +23 / −23
diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp
index 694fb535..578713c0 100644
--- a/src/qt/walletmodel.cpp
+++ b/src/qt/walletmodel.cpp
@@ -408,12 +408,12 @@ static void NotifyCanGetAddressesChanged(WalletModel* walletmodel)
void WalletModel::subscribeToCoreSignals()
{
// Connect signals to wallet
- m_handler_unload = m_wallet->handleUnload(std::bind(&NotifyUnload, this));
- m_handler_status_changed = m_wallet->handleStatusChanged(std::bind(&NotifyKeyStoreStatusChanged, this));
- m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
- m_handler_transaction_changed = m_wallet->handleTransactionChanged(std::bind(NotifyTransactionChanged, this, std::placeholders::_1, std::placeholders::_2));
- m_handler_show_progress = m_wallet->handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2));
- m_handler_can_get_addrs_changed = m_wallet->handleCanGetAddressesChanged(std::bind(NotifyCanGetAddressesChanged, this));
+ m_handler_unload = m_wallet->handleUnload(std::bind_front(&NotifyUnload, this));
+ m_handler_status_changed = m_wallet->handleStatusChanged(std::bind_front(&NotifyKeyStoreStatusChanged, this));
+ m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind_front(NotifyAddressBookChanged, this));
+ m_handler_transaction_changed = m_wallet->handleTransactionChanged(std::bind_front(NotifyTransactionChanged, this));
+ m_handler_show_progress = m_wallet->handleShowProgress(std::bind_front(ShowProgress, this));
+ m_handler_can_get_addrs_changed = m_wallet->handleCanGetAddressesChanged(std::bind_front(NotifyCanGetAddressesChanged, this));
}
void WalletModel::unsubscribeFromCoreSignals()
diff --git a/src/test/scheduler_tests.cpp b/src/test/scheduler_tests.cpp
index 1ec8e8da..c106eca6 100644
--- a/src/test/scheduler_tests.cpp
+++ b/src/test/scheduler_tests.cpp
@@ -23,7 +23,7 @@ static void microTask(CScheduler& s, std::mutex& mutex, int& counter, int delta,
}
auto noTime = std::chrono::steady_clock::time_point::min();
if (rescheduleTime != noTime) {
- CScheduler::Function f = std::bind(µTask, std::ref(s), std::ref(mutex), std::ref(counter), -delta + 1, noTime);
+ CScheduler::Function f = std::bind_front(µTask, std::ref(s), std::ref(mutex), std::ref(counter), -delta + 1, noTime);
s.schedule(f, rescheduleTime);
}
}
@@ -59,7 +59,7 @@ BOOST_AUTO_TEST_CASE(manythreads)
auto t = now + std::chrono::microseconds(randomMsec(rng));
auto tReschedule = now + std::chrono::microseconds(500 + randomMsec(rng));
int whichCounter = zeroToNine(rng);
- CScheduler::Function f = std::bind(µTask, std::ref(microTasks),
+ CScheduler::Function f = std::bind_front(µTask, std::ref(microTasks),
std::ref(counterMutex[whichCounter]), std::ref(counter[whichCounter]),
randomDelta(rng), tReschedule);
microTasks.schedule(f, t);
@@ -73,19 +73,19 @@ BOOST_AUTO_TEST_CASE(manythreads)
std::vector<std::thread> microThreads;
microThreads.reserve(10);
for (int i = 0; i < 5; i++)
- microThreads.emplace_back(std::bind(&CScheduler::serviceQueue, µTasks));
+ microThreads.emplace_back(std::bind_front(&CScheduler::serviceQueue, µTasks));
UninterruptibleSleep(std::chrono::microseconds{600});
now = std::chrono::steady_clock::now();
// More threads and more tasks:
for (int i = 0; i < 5; i++)
- microThreads.emplace_back(std::bind(&CScheduler::serviceQueue, µTasks));
+ microThreads.emplace_back(std::bind_front(&CScheduler::serviceQueue, µTasks));
for (int i = 0; i < 100; i++) {
auto t = now + std::chrono::microseconds(randomMsec(rng));
auto tReschedule = now + std::chrono::microseconds(500 + randomMsec(rng));
int whichCounter = zeroToNine(rng);
- CScheduler::Function f = std::bind(µTask, std::ref(microTasks),
+ CScheduler::Function f = std::bind_front(µTask, std::ref(microTasks),
std::ref(counterMutex[whichCounter]), std::ref(counter[whichCounter]),
randomDelta(rng), tReschedule);
microTasks.schedule(f, t);
diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp
index 50c3148e..d37fc2dd 100644
--- a/src/torcontrol.cpp
+++ b/src/torcontrol.cpp
@@ -332,8 +332,8 @@ TorController::TorController(struct event_base* _base, const std::string& tor_co
if (!reconnect_ev)
LogWarning("tor: Failed to create event for reconnection: out of memory?");
// Start connection attempts immediately
- if (!conn.Connect(m_tor_control_center, std::bind(&TorController::connected_cb, this, std::placeholders::_1),
- std::bind(&TorController::disconnected_cb, this, std::placeholders::_1) )) {
+ if (!conn.Connect(m_tor_control_center, std::bind_front(&TorController::connected_cb, this),
+ std::bind_front(&TorController::disconnected_cb, this) )) {
LogWarning("tor: Initiating connection to Tor control port %s failed", m_tor_control_center);
}
// Read service private key if cached
@@ -469,7 +469,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
// Now that we know Tor is running setup the proxy for onion addresses
// if -onion isn't set to something else.
if (gArgs.GetArg("-onion", "") == "") {
- _conn.Command("GETINFO net/listeners/socks", std::bind(&TorController::get_socks_cb, this, std::placeholders::_1, std::placeholders::_2));
+ _conn.Command("GETINFO net/listeners/socks", std::bind_front(&TorController::get_socks_cb, this));
}
// Finally - now create the service
@@ -479,7 +479,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
// Request onion service, redirect port.
// Note that the 'virtual' port is always the default port to avoid decloaking nodes using other ports.
_conn.Command(strprintf("ADD_ONION %s Port=%i,%s", private_key, Params().GetDefaultPort(), m_target.ToStringAddrPort()),
- std::bind(&TorController::add_onion_cb, this, std::placeholders::_1, std::placeholders::_2));
+ std::bind_front(&TorController::add_onion_cb, this));
} else {
LogWarning("tor: Authentication failed");
}
@@ -538,7 +538,7 @@ void TorController::authchallenge_cb(TorControlConnection& _conn, const TorContr
}
std::vector<uint8_t> computedClientHash = ComputeResponse(TOR_SAFE_CLIENTKEY, cookie, clientNonce, serverNonce);
- _conn.Command("AUTHENTICATE " + HexStr(computedClientHash), std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
+ _conn.Command("AUTHENTICATE " + HexStr(computedClientHash), std::bind_front(&TorController::auth_cb, this));
} else {
LogWarning("tor: Invalid reply to AUTHCHALLENGE");
}
@@ -589,23 +589,23 @@ void TorController::protocolinfo_cb(TorControlConnection& _conn, const TorContro
if (methods.contains("HASHEDPASSWORD")) {
LogDebug(BCLog::TOR, "Using HASHEDPASSWORD authentication\n");
ReplaceAll(torpassword, "\"", "\\\"");
- _conn.Command("AUTHENTICATE \"" + torpassword + "\"", std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
+ _conn.Command("AUTHENTICATE \"" + torpassword + "\"", std::bind_front(&TorController::auth_cb, this));
} else {
LogWarning("tor: Password provided with -torpassword, but HASHEDPASSWORD authentication is not available");
}
} else if (methods.contains("NULL")) {
LogDebug(BCLog::TOR, "Using NULL authentication\n");
- _conn.Command("AUTHENTICATE", std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
+ _conn.Command("AUTHENTICATE", std::bind_front(&TorController::auth_cb, this));
} else if (methods.contains("SAFECOOKIE")) {
// Cookie: hexdump -e '32/1 "%02x""\n"' ~/.tor/control_auth_cookie
LogDebug(BCLog::TOR, "Using SAFECOOKIE authentication, reading cookie authentication from %s\n", cookiefile);
std::pair<bool,std::string> status_cookie = ReadBinaryFile(fs::PathFromString(cookiefile), TOR_COOKIE_SIZE);
if (status_cookie.first && status_cookie.second.size() == TOR_COOKIE_SIZE) {
- // _conn.Command("AUTHENTICATE " + HexStr(status_cookie.second), std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
+ // _conn.Command("AUTHENTICATE " + HexStr(status_cookie.second), std::bind_front(&TorController::auth_cb, this));
cookie = std::vector<uint8_t>(status_cookie.second.begin(), status_cookie.second.end());
clientNonce = std::vector<uint8_t>(TOR_NONCE_SIZE, 0);
GetRandBytes(clientNonce);
- _conn.Command("AUTHCHALLENGE SAFECOOKIE " + HexStr(clientNonce), std::bind(&TorController::authchallenge_cb, this, std::placeholders::_1, std::placeholders::_2));
+ _conn.Command("AUTHCHALLENGE SAFECOOKIE " + HexStr(clientNonce), std::bind_front(&TorController::authchallenge_cb, this));
} else {
if (status_cookie.first) {
LogWarning("tor: Authentication cookie %s is not exactly %i bytes, as is required by the spec", cookiefile, TOR_COOKIE_SIZE);
@@ -627,7 +627,7 @@ void TorController::connected_cb(TorControlConnection& _conn)
{
reconnect_timeout = RECONNECT_TIMEOUT_START;
// First send a PROTOCOLINFO command to figure out what authentication is expected
- if (!_conn.Command("PROTOCOLINFO 1", std::bind(&TorController::protocolinfo_cb, this, std::placeholders::_1, std::placeholders::_2)))
+ if (!_conn.Command("PROTOCOLINFO 1", std::bind_front(&TorController::protocolinfo_cb, this)))
LogWarning("tor: Error sending initial protocolinfo command");
}
@@ -656,8 +656,8 @@ void TorController::Reconnect()
/* Try to reconnect and reestablish if we get booted - for example, Tor
* may be restarting.
*/
- if (!conn.Connect(m_tor_control_center, std::bind(&TorController::connected_cb, this, std::placeholders::_1),
- std::bind(&TorController::disconnected_cb, this, std::placeholders::_1) )) {
+ if (!conn.Connect(m_tor_control_center, std::bind_front(&TorController::connected_cb, this),
+ std::bind_front(&TorController::disconnected_cb, this) )) {
LogWarning("tor: Re-initiating connection to Tor control port %s failed", m_tor_control_center);
}
}
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.