1
0
mirror of https://github.com/moparisthebest/curl synced 2025-02-28 09:21:50 -05:00

asyn-thread: Fixed cleanup after OOM

destroy_async_data() assumes that if the flag "done" is not set yet, the
thread itself will clean up once the request is complete.  But if an
error (generally OOM) occurs before the thread even has a chance to
start, it will never get a chance to clean up and memory will be leaked.
By clearing "done" only just before starting the thread, the correct
cleanup sequence will happen in all cases.
This commit is contained in:
Dan Fandrich 2017-08-29 00:30:30 +02:00
parent edcb1ef60f
commit df29455dbc

View File

@ -210,6 +210,10 @@ int init_thread_sync_data(struct thread_data * td,
tsd->td = td; tsd->td = td;
tsd->port = port; tsd->port = port;
/* Treat the request as done until the thread actually starts so any early
* cleanup gets done properly.
*/
tsd->done = 1;
#ifdef HAVE_GETADDRINFO #ifdef HAVE_GETADDRINFO
DEBUGASSERT(hints); DEBUGASSERT(hints);
tsd->hints = *hints; tsd->hints = *hints;
@ -403,6 +407,9 @@ static bool init_resolve_thread(struct connectdata *conn,
if(!conn->async.hostname) if(!conn->async.hostname)
goto err_exit; goto err_exit;
/* The thread will set this to 1 when complete. */
td->tsd.done = 0;
#ifdef HAVE_GETADDRINFO #ifdef HAVE_GETADDRINFO
td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd); td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
#else #else
@ -410,6 +417,8 @@ static bool init_resolve_thread(struct connectdata *conn,
#endif #endif
if(!td->thread_hnd) { if(!td->thread_hnd) {
/* The thread never started, so mark it as done here for proper cleanup. */
td->tsd.done = 1;
err = errno; err = errno;
goto err_exit; goto err_exit;
} }