Skip to content

Commit d203d68

Browse files
authored
CCDB: ensure the right number of bytes is deleted for libuv handlers (#13272)
* CCDB: cast to the concrete pointer type before deleting a uv_handle_t* libuv handlers use c-style polymorphism, so deleting uv_handle_t* pointing to a derived class will in fact delete wrong number of bytes. As ugly as it is, it seems we have to check for all the possible types here. * CCDB: use malloc/free for uv_handle_t to release the correct size on heap While the previous approach was probably also OK, it's indeed more concise with C... Asan output for testCcdbApiDownloader is now perfectly clean.
1 parent fcff0dd commit d203d68

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

CCDB/src/CCDBDownloader.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ CCDBDownloader::CCDBDownloader(uv_loop_t* uv_loop)
8080
}
8181

8282
// Preparing timer to be used by curl
83-
mTimeoutTimer = new uv_timer_t();
83+
mTimeoutTimer = (uv_timer_t*)malloc(sizeof(*mTimeoutTimer));
8484
mTimeoutTimer->data = this;
8585
uvErrorCheck(uv_timer_init(mUVLoop, mTimeoutTimer));
8686
mHandleMap[(uv_handle_t*)mTimeoutTimer] = true;
@@ -136,7 +136,7 @@ void closeHandles(uv_handle_t* handle, void* arg)
136136
void onUVClose(uv_handle_t* handle)
137137
{
138138
if (handle != nullptr) {
139-
delete handle;
139+
free(handle);
140140
}
141141
}
142142

@@ -174,7 +174,7 @@ curl_socket_t opensocketCallback(void* clientp, curlsocktype purpose, struct cur
174174
}
175175

176176
if (CD->mExternalLoop) {
177-
CD->mSocketTimerMap[sock] = new uv_timer_t();
177+
CD->mSocketTimerMap[sock] = (uv_timer_t*)malloc(sizeof(*CD->mSocketTimerMap[sock]));
178178
uvErrorCheck(uv_timer_init(CD->mUVLoop, CD->mSocketTimerMap[sock]));
179179
CD->mHandleMap[(uv_handle_t*)CD->mSocketTimerMap[sock]] = true;
180180

@@ -323,7 +323,7 @@ CCDBDownloader::curl_context_t* CCDBDownloader::createCurlContext(curl_socket_t
323323
context = (curl_context_t*)malloc(sizeof(*context));
324324
context->CD = this;
325325
context->sockfd = sockfd;
326-
context->poll_handle = new uv_poll_t();
326+
context->poll_handle = (uv_poll_t*)malloc(sizeof(*context->poll_handle));
327327

328328
uvErrorCheck(uv_poll_init_socket(mUVLoop, context->poll_handle, sockfd));
329329
mHandleMap[(uv_handle_t*)(context->poll_handle)] = true;
@@ -335,7 +335,7 @@ CCDBDownloader::curl_context_t* CCDBDownloader::createCurlContext(curl_socket_t
335335
void CCDBDownloader::curlCloseCB(uv_handle_t* handle)
336336
{
337337
auto* context = (curl_context_t*)handle->data;
338-
delete context->poll_handle;
338+
free(context->poll_handle);
339339
free(context);
340340
}
341341

CCDB/test/testCcdbApiDownloader.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ BOOST_AUTO_TEST_CASE(test_with_break)
302302
void onUVClose(uv_handle_t* handle)
303303
{
304304
if (handle != nullptr) {
305-
delete handle;
305+
free(handle);
306306
}
307307
}
308308

@@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(external_loop_test)
325325
uv_loop_init(uvLoop);
326326

327327
// Prepare test timer. It will be used to check whether the downloader affects external handles.
328-
auto testTimer = new uv_timer_t();
328+
auto testTimer = (uv_timer_t*)malloc(sizeof(uv_timer_t));
329329
uv_timer_init(uvLoop, testTimer);
330330
uv_timer_start(testTimer, testTimerCB, 10, 10);
331331

@@ -358,7 +358,7 @@ BOOST_AUTO_TEST_CASE(external_loop_test)
358358
// The reason for that are the uv_poll handles attached to the curl multi handle.
359359
// The multi handle must be cleaned (via destuctor) before poll handles attached to them are removed (via walking and closing).
360360
delete downloader;
361-
while (uv_loop_alive(uvLoop) && uv_loop_close(uvLoop) == UV_EBUSY) {
361+
while (uv_loop_alive(uvLoop) || uv_loop_close(uvLoop) == UV_EBUSY) {
362362
uv_walk(uvLoop, closeAllHandles, nullptr);
363363
uv_run(uvLoop, UV_RUN_ONCE);
364364
}

0 commit comments

Comments
 (0)