Skip to content

Commit 9ddb626

Browse files
authored
Fix the heap-use-after-free risk caused by direct deconstruction when it is not used after initialization. (#274)
Fix the heap-use-after-free risk caused by direct deconstruction when it is not used after initialization. (#274)
1 parent 900b937 commit 9ddb626

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/MQClientFactory.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ MQClientFactory::~MQClientFactory() {
6262
m_topicRouteTable.clear();
6363
m_brokerAddrTable.clear();
6464
m_topicPublishInfoTable.clear();
65-
66-
m_pClientAPIImpl = NULL;
6765
}
6866

6967
void MQClientFactory::start() {
@@ -287,26 +285,36 @@ void MQClientFactory::shutdown() {
287285
return;
288286

289287
switch (m_serviceState) {
288+
case CREATE_JUST:
290289
case RUNNING: {
291290
if (m_consumer_async_service_thread) {
292291
m_consumer_async_ioService.stop();
293292
m_consumer_async_service_thread->interrupt();
294293
m_consumer_async_service_thread->join();
294+
m_consumer_async_service_thread.reset();
295+
}
296+
297+
if (m_async_service_thread) {
298+
m_async_ioService.stop();
299+
m_async_service_thread->interrupt();
300+
m_async_service_thread->join();
301+
m_async_service_thread.reset();
295302
}
296-
m_async_ioService.stop();
297-
m_async_service_thread->interrupt();
298-
m_async_service_thread->join();
299-
m_pClientAPIImpl->stopAllTcpTransportThread(); // Note: stop all
300-
// TcpTransport Threads
301-
// and release all
302-
// responseFuture
303-
// conditions
303+
304+
if (m_pClientAPIImpl) {
305+
m_pClientAPIImpl->stopAllTcpTransportThread(); // Note: stop all
306+
// TcpTransport Threads
307+
// and release all
308+
// responseFuture
309+
// conditions
310+
m_pClientAPIImpl.reset();
311+
}
312+
304313
m_serviceState = SHUTDOWN_ALREADY;
305314
LOG_INFO("MQClientFactory:%s shutdown", m_clientId.c_str());
306315
break;
307316
}
308317
case SHUTDOWN_ALREADY:
309-
case CREATE_JUST:
310318
break;
311319
default:
312320
break;

test/src/MQClientManagerTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ TEST(MQClientManagerTest, getClientFactory) {
3636
MQClientFactory* factory = MQClientManager::getInstance()->getMQClientFactory(clientId, 1, 1000, 3000, unitName);
3737
MQClientFactory* factory2 = MQClientManager::getInstance()->getMQClientFactory(clientId, 1, 1000, 3000, unitName);
3838
EXPECT_EQ(factory, factory2);
39+
factory->shutdown();
3940

4041
MQClientManager::getInstance()->removeClientFactory(clientId);
4142
}

0 commit comments

Comments
 (0)