Skip to content

Commit a7c36aa

Browse files
committed
exmdb_client: fix crash when replacing old connections
==774==ERROR: AddressSanitizer: global-buffer-overflow; READ of size 1 f0 _M_reset /usr/include/c++/15/optional:337 f4 ~remote_svr lib/exmdb_client.cpp:82 f9 erase /usr/include/c++/15/bits/list.tcc:153 f10 exmdb_client_get_connection lib/exmdb_client.cpp:475 Fix these issues: * The inside of the loop referenced `i`, which is not right; the iterator is `j`. * `mdcl_server_list.erase(j.base())`: the iterator was not secured against removal of the element it points to. * The loop ran in the wrong direction (from the end - where `i` was just added previously). Fixes: gromox-3.4-160-g242b86036
1 parent bc506b1 commit a7c36aa

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lib/exmdb_client.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -464,16 +464,22 @@ static remote_conn_ref exmdb_client_get_connection(const char *dir)
464464
i->conn_list.pop_front();
465465
}
466466
if (g_exmdbcl_active_handles >= mdcl_conn_max) {
467-
/*
468-
* Try closing an older connection. But do not touch async
469-
* notifier threads, they are kind of separate anyway.
470-
*/
471-
for (auto j = mdcl_server_list.rbegin(); j != mdcl_server_list.rend(); ++j) {
472-
if (i->conn_list.size() > 0)
473-
i->conn_list.pop_back();
474-
if (i->conn_list.empty() && !i->m_agent.has_value())
475-
mdcl_server_list.erase(j.base());
476-
break;
467+
/* Try closing one older connection. */
468+
for (auto j = mdcl_server_list.begin(); j != i; ) {
469+
bool do_pop = j->conn_list.size() > 0;
470+
if (do_pop)
471+
j->conn_list.pop_back();
472+
/* Do not touch async notifier threads, they are kind of separate anyway. */
473+
auto clean = j->conn_list.empty() && !j->m_agent.has_value();
474+
if (do_pop) {
475+
if (clean)
476+
mdcl_server_list.erase(j);
477+
break;
478+
}
479+
if (clean)
480+
j = mdcl_server_list.erase(j);
481+
else
482+
++j;
477483
}
478484
}
479485
if (g_exmdbcl_active_handles >= mdcl_conn_max) {

0 commit comments

Comments
 (0)