From 25fc866a5896da549b629824198078b8f44921e8 Mon Sep 17 00:00:00 2001 From: Norbert Federa Date: Tue, 5 May 2015 13:55:48 +0200 Subject: [PATCH] Fix unchecked CreateThread calls and misc fixes --- channels/urbdrc/client/urbdrc_main.c | 5 +- .../Android/FreeRDPCore/jni/android_freerdp.c | 63 ++++++++++++++----- client/Mac/MRDPView.m | 47 ++++++++++---- client/Mac/mf_client.m | 8 +-- client/Sample/freerdp.c | 12 +++- client/Windows/wf_client.c | 9 ++- client/X11/xf_client.c | 27 +++++--- libfreerdp/codec/rfx.c | 5 +- libfreerdp/core/freerdp.c | 4 +- libfreerdp/core/listener.c | 10 ++- libfreerdp/core/message.c | 16 ++--- libfreerdp/core/update.c | 7 ++- libfreerdp/core/update.h | 2 +- server/Sample/sfreerdp.c | 14 ++--- server/Windows/wf_directsound.c | 8 ++- server/Windows/wf_interface.c | 12 ++-- server/Windows/wf_peer.c | 5 +- server/Windows/wf_wasapi.c | 8 ++- server/shadow/Mac/mac_shadow.c | 12 +++- server/shadow/Win/win_rdp.c | 8 ++- server/shadow/Win/win_shadow.c | 8 ++- server/shadow/X11/x11_shadow.c | 11 +++- server/shadow/shadow_client.c | 8 ++- server/shadow/shadow_server.c | 9 ++- .../error/test/TestErrorSetLastError.c | 13 ++-- winpr/libwinpr/pool/pool.c | 7 ++- winpr/libwinpr/pool/test/TestPoolThread.c | 7 ++- winpr/libwinpr/pool/test/TestPoolWork.c | 7 ++- winpr/libwinpr/sspi/test/TestSchannel.c | 7 ++- winpr/libwinpr/synch/test/TestSynchBarrier.c | 12 +++- winpr/libwinpr/synch/test/TestSynchCritical.c | 21 +++++-- winpr/libwinpr/utils/test/TestMessagePipe.c | 38 ++++++++--- winpr/libwinpr/utils/test/TestMessageQueue.c | 14 ++++- 33 files changed, 327 insertions(+), 117 deletions(-) diff --git a/channels/urbdrc/client/urbdrc_main.c b/channels/urbdrc/client/urbdrc_main.c index 38096b2cf..6db6edde7 100644 --- a/channels/urbdrc/client/urbdrc_main.c +++ b/channels/urbdrc/client/urbdrc_main.c @@ -472,7 +472,8 @@ static void* urbdrc_search_usb_device(void* arg) /* Get the file descriptor (fd) for the monitor. This fd will get passed to select() */ - mon_fd = CreateFileDescriptorEvent(NULL, TRUE, FALSE, udev_monitor_get_fd(mon)); + if (!(mon_fd = CreateFileDescriptorEvent(NULL, TRUE, FALSE, udev_monitor_get_fd(mon)))) + goto fail_create_monfd_event; while (1) { @@ -651,6 +652,8 @@ static void* urbdrc_search_usb_device(void* arg) } CloseHandle(mon_fd); + +fail_create_monfd_event: sem_post(&searchman->sem_term); return 0; diff --git a/client/Android/FreeRDPCore/jni/android_freerdp.c b/client/Android/FreeRDPCore/jni/android_freerdp.c index 81260a14e..82685257f 100644 --- a/client/Android/FreeRDPCore/jni/android_freerdp.c +++ b/client/Android/FreeRDPCore/jni/android_freerdp.c @@ -339,10 +339,17 @@ static void* jni_input_thread(void* arg) DEBUG_ANDROID("Start."); - queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE); - event[0] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, aCtx->event_queue->pipe_fd[0]); - event[1] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, aCtx->event_queue->pipe_fd[1]); - event[2] = freerdp_get_message_queue_event_handle(instance, FREERDP_INPUT_MESSAGE_QUEUE); + if (!(queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE))) + goto fail_get_message_queue; + + if (!(event[0] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, aCtx->event_queue->pipe_fd[0]))) + goto fail_create_event_0; + + if (!(event[1] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, aCtx->event_queue->pipe_fd[1]))) + goto fail_create_event_1; + + if (!(event[2] = freerdp_get_message_queue_event_handle(instance, FREERDP_INPUT_MESSAGE_QUEUE))) + goto fail_get_message_queue_event; do { @@ -364,8 +371,15 @@ static void* jni_input_thread(void* arg) while(1); DEBUG_ANDROID("Quit."); - + +fail_get_message_queue_event: + CloseHandle(event[1]); +fail_create_event_1: + CloseHandle(event[0]); +fail_create_event_0: MessageQueue_PostQuit(queue, 0); +fail_get_message_queue: + ExitThread(0); return NULL; } @@ -416,8 +430,8 @@ static int android_freerdp_run(freerdp* instance) const rdpSettings* settings = instance->context->settings; - HANDLE input_thread; - HANDLE channels_thread; + HANDLE input_thread = NULL; + HANDLE channels_thread = NULL; BOOL async_input = settings->AsyncInput; BOOL async_channels = settings->AsyncChannels; @@ -439,14 +453,22 @@ static int android_freerdp_run(freerdp* instance) if (async_input) { - input_thread = CreateThread(NULL, 0, - (LPTHREAD_START_ROUTINE) jni_input_thread, instance, 0, NULL); + if (!(input_thread = CreateThread(NULL, 0, + (LPTHREAD_START_ROUTINE) jni_input_thread, instance, 0, NULL))) + { + DEBUG_ANDROID("Failed to create async input thread\n"); + goto disconnect; + } } if (async_channels) { - channels_thread = CreateThread(NULL, 0, - (LPTHREAD_START_ROUTINE) jni_channels_thread, instance, 0, NULL); + if (!(channels_thread = CreateThread(NULL, 0, + (LPTHREAD_START_ROUTINE) jni_channels_thread, instance, 0, NULL))) + { + DEBUG_ANDROID("Failed to create async channels thread\n"); + goto disconnect; + } } ((androidContext*)instance->context)->is_connected = TRUE; @@ -568,6 +590,7 @@ static int android_freerdp_run(freerdp* instance) } } +disconnect: DEBUG_ANDROID("Prepare shutdown..."); // issue another OnDisconnecting here in case the disconnect was initiated by the server and not our client @@ -578,17 +601,20 @@ static int android_freerdp_run(freerdp* instance) DEBUG_ANDROID("Cleanup threads..."); - if (async_channels) + if (async_channels && channels_thread) { WaitForSingleObject(channels_thread, INFINITE); CloseHandle(channels_thread); } - if (async_input) + if (async_input && input_thread) { wMessageQueue* input_queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE); - MessageQueue_PostQuit(input_queue, 0); - WaitForSingleObject(input_thread, INFINITE); + if (input_queue) + { + MessageQueue_PostQuit(input_queue, 0); + WaitForSingleObject(input_thread, INFINITE); + } CloseHandle(input_thread); } @@ -670,8 +696,11 @@ JNIEXPORT jboolean JNICALL jni_freerdp_connect(JNIEnv *env, jclass cls, jint ins assert(inst); assert(ctx); - ctx->thread = CreateThread(NULL, 0, - (LPTHREAD_START_ROUTINE)android_thread_func, inst, 0, NULL); + if (!(ctx->thread = CreateThread(NULL, 0, + (LPTHREAD_START_ROUTINE)android_thread_func, inst, 0, NULL))) + { + return JNI_FALSE; + } return JNI_TRUE; } diff --git a/client/Mac/MRDPView.m b/client/Mac/MRDPView.m index 3f3f9ef0e..ce18f3c73 100644 --- a/client/Mac/MRDPView.m +++ b/client/Mac/MRDPView.m @@ -94,8 +94,12 @@ DWORD mac_client_thread(void* param); mfc->client_height = instance->settings->DesktopHeight; mfc->client_width = instance->settings->DesktopWidth; - mfc->thread = CreateThread(NULL, 0, mac_client_thread, (void*) context, 0, &mfc->mainThreadId); - + if (!(mfc->thread = CreateThread(NULL, 0, mac_client_thread, (void*) context, 0, &mfc->mainThreadId))) + { + WLog_ERR(TAG, "failed to create client thread"); + return -1; + } + return 0; } @@ -162,9 +166,9 @@ DWORD mac_client_thread(void* param) int status; HANDLE events[4]; HANDLE inputEvent; - HANDLE inputThread; + HANDLE inputThread = NULL; HANDLE updateEvent; - HANDLE updateThread; + HANDLE updateThread = NULL; HANDLE channelsEvent; DWORD nCount; @@ -190,7 +194,11 @@ DWORD mac_client_thread(void* param) if (settings->AsyncUpdate) { - updateThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) mac_client_update_thread, context, 0, NULL); + if (!(updateThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) mac_client_update_thread, context, 0, NULL))) + { + WLog_ERR(TAG, "failed to create async update thread"); + goto disconnect; + } } else { @@ -199,7 +207,11 @@ DWORD mac_client_thread(void* param) if (settings->AsyncInput) { - inputThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) mac_client_input_thread, context, 0, NULL); + if (!(inputThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) mac_client_input_thread, context, 0, NULL))) + { + WLog_ERR(TAG, "failed to create async input thread"); + goto disconnect; + } } else { @@ -214,7 +226,6 @@ DWORD mac_client_thread(void* param) if (WaitForSingleObject(mfc->stopEvent, 0) == WAIT_OBJECT_0) { - freerdp_disconnect(instance); break; } @@ -240,19 +251,29 @@ DWORD mac_client_thread(void* param) } } - if (settings->AsyncUpdate) +disconnect: + + freerdp_disconnect(instance); + + if (settings->AsyncUpdate && updateThread) { wMessageQueue* updateQueue = freerdp_get_message_queue(instance, FREERDP_UPDATE_MESSAGE_QUEUE); - MessageQueue_PostQuit(updateQueue, 0); - WaitForSingleObject(updateThread, INFINITE); + if (updateQueue) + { + MessageQueue_PostQuit(updateQueue, 0); + WaitForSingleObject(updateThread, INFINITE); + } CloseHandle(updateThread); } - if (settings->AsyncInput) + if (settings->AsyncInput && inputThread) { wMessageQueue* inputQueue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE); - MessageQueue_PostQuit(inputQueue, 0); - WaitForSingleObject(inputThread, INFINITE); + if (inputQueue) + { + MessageQueue_PostQuit(inputQueue, 0); + WaitForSingleObject(inputThread, INFINITE); + } CloseHandle(inputThread); } diff --git a/client/Mac/mf_client.m b/client/Mac/mf_client.m index 41a9364dd..18fe41c46 100644 --- a/client/Mac/mf_client.m +++ b/client/Mac/mf_client.m @@ -54,9 +54,7 @@ int mfreerdp_client_start(rdpContext* context) } view = (MRDPView*) mfc->view; - [view rdpStart:context]; - - return 0; + return [view rdpStart:context]; } int mfreerdp_client_stop(rdpContext* context) @@ -82,7 +80,7 @@ int mfreerdp_client_stop(rdpContext* context) return 0; } -int mfreerdp_client_new(freerdp* instance, rdpContext* context) +BOOL mfreerdp_client_new(freerdp* instance, rdpContext* context) { mfContext* mfc; rdpSettings* settings; @@ -103,7 +101,7 @@ int mfreerdp_client_new(freerdp* instance, rdpContext* context) settings->AsyncUpdate = TRUE; settings->AsyncInput = TRUE; - return 0; + return TRUE; } void mfreerdp_client_free(freerdp* instance, rdpContext* context) diff --git a/client/Sample/freerdp.c b/client/Sample/freerdp.c index e4614a249..70722f6f2 100644 --- a/client/Sample/freerdp.c +++ b/client/Sample/freerdp.c @@ -203,10 +203,16 @@ int main(int argc, char* argv[]) freerdp_client_load_addins(instance->context->channels, instance->settings); - thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) - tf_client_thread_proc, instance, 0, NULL); + if (!(thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) + tf_client_thread_proc, instance, 0, NULL))) + { + WLog_ERR(TAG, "Failed to create client thread"); + } + else + { + WaitForSingleObject(thread, INFINITE); + } - WaitForSingleObject(thread, INFINITE); freerdp_context_free(instance); freerdp_free(instance); diff --git a/client/Windows/wf_client.c b/client/Windows/wf_client.c index e639b31e4..f07e2abd5 100644 --- a/client/Windows/wf_client.c +++ b/client/Windows/wf_client.c @@ -670,9 +670,13 @@ DWORD WINAPI wf_client_thread(LPVOID lpParam) if (async_input) { - input_thread = CreateThread(NULL, 0, + if (!(input_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) wf_input_thread, - instance, 0, NULL); + instance, 0, NULL))) + { + WLog_ERR(TAG, "Failed to create async input thread."); + goto disconnect; + } } while (1) @@ -771,6 +775,7 @@ DWORD WINAPI wf_client_thread(LPVOID lpParam) CloseHandle(input_thread); } +disconnect: freerdp_disconnect(instance); WLog_DBG(TAG, "Main thread exited."); diff --git a/client/X11/xf_client.c b/client/X11/xf_client.c index 3209b3d05..8db672699 100644 --- a/client/X11/xf_client.c +++ b/client/X11/xf_client.c @@ -1417,10 +1417,9 @@ void* xf_client_thread(void* param) /* Connection succeeded. --authonly ? */ if (instance->settings->AuthenticationOnly || !status) { - freerdp_disconnect(instance); WLog_ERR(TAG, "Authentication only, exit status %d", !status); exit_code = XF_EXIT_CONN_FAILED; - ExitThread(exit_code); + goto disconnect; } channels = context->channels; @@ -1432,8 +1431,18 @@ void* xf_client_thread(void* param) } else { - inputThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_input_thread, instance, 0, NULL); - inputEvent = freerdp_get_message_queue_event_handle(instance, FREERDP_INPUT_MESSAGE_QUEUE); + if (!(inputEvent = freerdp_get_message_queue_event_handle(instance, FREERDP_INPUT_MESSAGE_QUEUE))) + { + WLog_ERR(TAG, "async input: failed to get input event handle"); + exit_code = XF_EXIT_UNKNOWN; + goto disconnect; + } + if (!(inputThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_input_thread, instance, 0, NULL))) + { + WLog_ERR(TAG, "async input: failed to create input thread"); + exit_code = XF_EXIT_UNKNOWN; + goto disconnect; + } } while (!xfc->disconnect && !freerdp_shall_disconnect(instance)) @@ -1512,8 +1521,8 @@ void* xf_client_thread(void* param) if (!exit_code) exit_code = freerdp_error_info(instance); +disconnect: freerdp_disconnect(instance); - ExitThread(exit_code); return NULL; } @@ -1623,9 +1632,13 @@ static int xfreerdp_client_start(rdpContext* context) xfc->disconnect = FALSE; - xfc->thread = CreateThread(NULL, 0, + if (!(xfc->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_client_thread, - context->instance, 0, NULL); + context->instance, 0, NULL))) + { + WLog_ERR(TAG, "failed to create client thread"); + return -1; + } return 0; } diff --git a/libfreerdp/codec/rfx.c b/libfreerdp/codec/rfx.c index 6f5a4651f..54e869977 100644 --- a/libfreerdp/codec/rfx.c +++ b/libfreerdp/codec/rfx.c @@ -316,7 +316,8 @@ RFX_CONTEXT* rfx_context_new(BOOL encoder) SetThreadpoolCallbackPool(&priv->ThreadPoolEnv, priv->ThreadPool); if (priv->MinThreadCount) - SetThreadpoolThreadMinimum(priv->ThreadPool, priv->MinThreadCount); + if (!SetThreadpoolThreadMinimum(priv->ThreadPool, priv->MinThreadCount)) + goto error_threadPool_minimum; if (priv->MaxThreadCount) SetThreadpoolThreadMaximum(priv->ThreadPool, priv->MaxThreadCount); @@ -339,6 +340,8 @@ RFX_CONTEXT* rfx_context_new(BOOL encoder) context->state = RFX_STATE_SEND_HEADERS; return context; +error_threadPool_minimum: + CloseThreadpool(priv->ThreadPool); error_threadPool: BufferPool_Free(priv->BufferPool); error_BufferPool: diff --git a/libfreerdp/core/freerdp.c b/libfreerdp/core/freerdp.c index 77ed203d6..44015c4f7 100644 --- a/libfreerdp/core/freerdp.c +++ b/libfreerdp/core/freerdp.c @@ -111,9 +111,7 @@ BOOL freerdp_connect(freerdp* instance) IFCALLRET(instance->PostConnect, status, instance); - update_post_connect(instance->update); - - if (!status) + if (!status || !update_post_connect(instance->update)) { WLog_ERR(TAG, "freerdp_post_connect failed"); diff --git a/libfreerdp/core/listener.c b/libfreerdp/core/listener.c index b649ac593..3b6a0da79 100644 --- a/libfreerdp/core/listener.c +++ b/libfreerdp/core/listener.c @@ -180,6 +180,7 @@ static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char* int sockfd; struct sockaddr_un addr; rdpListener* listener = (rdpListener*) instance->listener; + HANDLE hevent; sockfd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -213,8 +214,15 @@ static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char* return FALSE; } + if (!(hevent = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd))) + { + WLog_ERR(TAG, "failed to create sockfd event"); + closesocket((SOCKET) sockfd); + return FALSE; + } + listener->sockfds[listener->num_sockfds] = sockfd; - listener->events[listener->num_sockfds] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd); + listener->events[listener->num_sockfds] = hevent; listener->num_sockfds++; WLog_INFO(TAG, "Listening on socket %s.", addr.sun_path); return TRUE; diff --git a/libfreerdp/core/message.c b/libfreerdp/core/message.c index aa336ca6e..e0bab4047 100644 --- a/libfreerdp/core/message.c +++ b/libfreerdp/core/message.c @@ -2546,15 +2546,17 @@ static void *update_message_proxy_thread(void *arg) rdpUpdateProxy *update_message_proxy_new(rdpUpdate *update) { rdpUpdateProxy *message; - message = (rdpUpdateProxy *) malloc(sizeof(rdpUpdateProxy)); - if (message) + if (!(message = (rdpUpdateProxy *) calloc(1, sizeof(rdpUpdateProxy)))) + return NULL; + + message->update = update; + update_message_register_interface(message, update); + if (!(message->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) update_message_proxy_thread, update, 0, NULL))) { - ZeroMemory(message, sizeof(rdpUpdateProxy)); - - message->update = update; - update_message_register_interface(message, update); - message->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) update_message_proxy_thread, update, 0, NULL); + WLog_ERR(TAG, "Failed to create proxy thread"); + free(message); + return NULL; } return message; diff --git a/libfreerdp/core/update.c b/libfreerdp/core/update.c index 746c69db7..d25f94269 100644 --- a/libfreerdp/core/update.c +++ b/libfreerdp/core/update.c @@ -572,17 +572,20 @@ void update_reset_state(rdpUpdate* update) } } -void update_post_connect(rdpUpdate* update) +BOOL update_post_connect(rdpUpdate* update) { update->asynchronous = update->context->settings->AsyncUpdate; if (update->asynchronous) - update->proxy = update_message_proxy_new(update); + if (!(update->proxy = update_message_proxy_new(update))) + return FALSE; update->altsec->switch_surface.bitmapId = SCREEN_BITMAP_SURFACE; IFCALL(update->altsec->SwitchSurface, update->context, &(update->altsec->switch_surface)); update->initialState = FALSE; + + return TRUE; } void update_post_disconnect(rdpUpdate* update) diff --git a/libfreerdp/core/update.h b/libfreerdp/core/update.h index c0b266f64..2c363fe83 100644 --- a/libfreerdp/core/update.h +++ b/libfreerdp/core/update.h @@ -43,7 +43,7 @@ void update_free(rdpUpdate* update); void update_free_bitmap(BITMAP_UPDATE* bitmap_update); void update_reset_state(rdpUpdate* update); -void update_post_connect(rdpUpdate* update); +BOOL update_post_connect(rdpUpdate* update); void update_post_disconnect(rdpUpdate* update); BOOL update_read_bitmap_update(rdpUpdate* update, wStream* s, BITMAP_UPDATE* bitmapUpdate); diff --git a/server/Sample/sfreerdp.c b/server/Sample/sfreerdp.c index 42cf3b147..7d5355151 100644 --- a/server/Sample/sfreerdp.c +++ b/server/Sample/sfreerdp.c @@ -446,7 +446,8 @@ static void* tf_debug_channel_thread_func(void* arg) fd = *((void**) buffer); WTSFreeMemory(buffer); - context->event = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd); + if (!(context->event = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd))) + return NULL; } s = Stream_New(NULL, 4096); @@ -779,14 +780,13 @@ static void* test_peer_mainloop(void* arg) static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client) { - HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_peer_mainloop, (void*) client, 0, NULL); + HANDLE hThread; - if (hThread != NULL) { - CloseHandle(hThread); - return TRUE; - } + if (!(hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_peer_mainloop, (void*) client, 0, NULL))) + return FALSE; - return FALSE; + CloseHandle(hThread); + return TRUE; } static void test_server_mainloop(freerdp_listener* instance) diff --git a/server/Windows/wf_directsound.c b/server/Windows/wf_directsound.c index 649d9a8c0..14248393c 100644 --- a/server/Windows/wf_directsound.c +++ b/server/Windows/wf_directsound.c @@ -33,6 +33,7 @@ int wf_directsound_activate(RdpsndServerContext* context) { HRESULT hr; wfInfo* wfi; + HANDLE hThread; LPDIRECTSOUNDCAPTUREBUFFER pDSCB; @@ -77,7 +78,12 @@ int wf_directsound_activate(RdpsndServerContext* context) pDSCB->lpVtbl->Release(pDSCB); lastPos = 0; - CreateThread(NULL, 0, wf_rdpsnd_directsound_thread, latestPeer, 0, NULL); + if (!(hThread = CreateThread(NULL, 0, wf_rdpsnd_directsound_thread, latestPeer, 0, NULL))) + { + WLog_ERR(TAG, "Failed to create direct sound thread"); + return 1; + } + CloseHandle(hThread); return 0; } diff --git a/server/Windows/wf_interface.c b/server/Windows/wf_interface.c index 4b9b9b2b2..2a18c4aff 100644 --- a/server/Windows/wf_interface.c +++ b/server/Windows/wf_interface.c @@ -156,13 +156,13 @@ BOOL wfreerdp_server_start(wfServer* server) wf_settings_read_dword(HKEY_LOCAL_MACHINE, _T("Software\\FreeRDP\\Server"), _T("DefaultPort"), &server->port); - if (instance->Open(instance, NULL, (UINT16) server->port)) - { - server->thread = CreateThread(NULL, 0, wf_server_main_loop, (void*) instance, 0, NULL); - return TRUE; - } + if (!instance->Open(instance, NULL, (UINT16) server->port)) + return FALSE; - return FALSE; + if (!(server->thread = CreateThread(NULL, 0, wf_server_main_loop, (void*) instance, 0, NULL))) + return FALSE; + + return TRUE; } BOOL wfreerdp_server_stop(wfServer* server) diff --git a/server/Windows/wf_peer.c b/server/Windows/wf_peer.c index a1ef07cec..598080e68 100644 --- a/server/Windows/wf_peer.c +++ b/server/Windows/wf_peer.c @@ -151,9 +151,12 @@ void wf_peer_synchronize_event(rdpInput* input, UINT32 flags) BOOL wf_peer_accepted(freerdp_listener* instance, freerdp_peer* client) { - if (!CreateThread(NULL, 0, wf_peer_main_loop, client, 0, NULL)) + HANDLE hThread; + + if (!(hThread = CreateThread(NULL, 0, wf_peer_main_loop, client, 0, NULL))) return FALSE; + CloseHandle(hThread); return TRUE; } diff --git a/server/Windows/wf_wasapi.c b/server/Windows/wf_wasapi.c index 58275fd34..1a484a4f4 100644 --- a/server/Windows/wf_wasapi.c +++ b/server/Windows/wf_wasapi.c @@ -36,6 +36,7 @@ int wf_rdpsnd_set_latest_peer(wfPeerContext* peer) int wf_wasapi_activate(RdpsndServerContext* context) { wchar_t * pattern = L"Stereo Mix"; + HANDLE hThread; wf_wasapi_get_device_string(pattern, &devStr); @@ -46,7 +47,12 @@ int wf_wasapi_activate(RdpsndServerContext* context) } WLog_DBG(TAG, "RDPSND (WASAPI) Activated"); - CreateThread(NULL, 0, wf_rdpsnd_wasapi_thread, latestPeer, 0, NULL); + if (!(hThread = CreateThread(NULL, 0, wf_rdpsnd_wasapi_thread, latestPeer, 0, NULL))) + { + WLog_ERR(TAG, "CreateThread failed"); + return 1; + } + CloseHandle(hThread); return 0; } diff --git a/server/shadow/Mac/mac_shadow.c b/server/shadow/Mac/mac_shadow.c index 7efd78b30..95e99e124 100644 --- a/server/shadow/Mac/mac_shadow.c +++ b/server/shadow/Mac/mac_shadow.c @@ -23,6 +23,7 @@ #include #include +#include #include "../shadow_screen.h" #include "../shadow_client.h" @@ -33,6 +34,9 @@ #include "mac_shadow.h" +#define TAG SERVER_TAG("shadow.mac") + + static macShadowSubsystem* g_Subsystem = NULL; void mac_shadow_input_synchronize_event(macShadowSubsystem* subsystem, UINT32 flags) @@ -616,9 +620,13 @@ int mac_shadow_subsystem_start(macShadowSubsystem* subsystem) mac_shadow_capture_start(subsystem); - thread = CreateThread(NULL, 0, + if (!(thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) mac_shadow_subsystem_thread, - (void*) subsystem, 0, NULL); + (void*) subsystem, 0, NULL))) + { + WLog_ERR(TAG, "Failed to create thread"); + return -1; + } return 1; } diff --git a/server/shadow/Win/win_rdp.c b/server/shadow/Win/win_rdp.c index 7783a47d2..e0e672b44 100644 --- a/server/shadow/Win/win_rdp.c +++ b/server/shadow/Win/win_rdp.c @@ -269,9 +269,13 @@ int shw_freerdp_client_start(rdpContext* context) shw = (shwContext*) context; - shw->thread = CreateThread(NULL, 0, + if (!(shw->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) shw_client_thread, - instance, 0, NULL); + instance, 0, NULL))) + { + WLog_ERR(TAG, "Failed to create thread"); + return -1; + } return 0; } diff --git a/server/shadow/Win/win_shadow.c b/server/shadow/Win/win_shadow.c index b70b61f79..0d2cb212b 100644 --- a/server/shadow/Win/win_shadow.c +++ b/server/shadow/Win/win_shadow.c @@ -485,9 +485,13 @@ int win_shadow_subsystem_start(winShadowSubsystem* subsystem) if (!subsystem) return -1; - thread = CreateThread(NULL, 0, + if (!(thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) win_shadow_subsystem_thread, - (void*) subsystem, 0, NULL); + (void*) subsystem, 0, NULL))) + { + WLog_ERR(TAG, "Failed to create thread"); + return -1; + } return 1; } diff --git a/server/shadow/X11/x11_shadow.c b/server/shadow/X11/x11_shadow.c index 80ff07665..36950dce4 100644 --- a/server/shadow/X11/x11_shadow.c +++ b/server/shadow/X11/x11_shadow.c @@ -1236,7 +1236,8 @@ int x11_shadow_subsystem_init(x11ShadowSubsystem* subsystem) subsystem->use_xdamage = FALSE; } - subsystem->event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, subsystem->xfds); + if (!(subsystem->event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, subsystem->xfds))) + return -1; virtualScreen = &(subsystem->virtualScreen); @@ -1283,9 +1284,13 @@ int x11_shadow_subsystem_start(x11ShadowSubsystem* subsystem) if (!subsystem) return -1; - subsystem->thread = CreateThread(NULL, 0, + if (!(subsystem->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) x11_shadow_subsystem_thread, - (void*) subsystem, 0, NULL); + (void*) subsystem, 0, NULL))) + { + WLog_ERR(TAG, "Failed to create thread"); + return -1; + } return 1; } diff --git a/server/shadow/shadow_client.c b/server/shadow/shadow_client.c index 15ee9de46..b0f825d88 100644 --- a/server/shadow/shadow_client.c +++ b/server/shadow/shadow_client.c @@ -1087,8 +1087,12 @@ BOOL shadow_client_accepted(freerdp_listener* listener, freerdp_peer* peer) client = (rdpShadowClient*) peer->context; - client->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) - shadow_client_thread, client, 0, NULL); + if (!(client->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) + shadow_client_thread, client, 0, NULL))) + { + freerdp_peer_context_free(peer); + return FALSE; + } return TRUE; } diff --git a/server/shadow/shadow_server.c b/server/shadow/shadow_server.c index 6d8315d9b..7867a548e 100644 --- a/server/shadow/shadow_server.c +++ b/server/shadow/shadow_server.c @@ -376,10 +376,13 @@ int shadow_server_start(rdpShadowServer* server) else status = server->listener->OpenLocal(server->listener, server->ipcSocket); - if (status) + if (!status) + return -1; + + if (!(server->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) + shadow_server_thread, (void*) server, 0, NULL))) { - server->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) - shadow_server_thread, (void*) server, 0, NULL); + return -1; } return 0; diff --git a/winpr/libwinpr/error/test/TestErrorSetLastError.c b/winpr/libwinpr/error/test/TestErrorSetLastError.c index a447a0113..44c986a8f 100644 --- a/winpr/libwinpr/error/test/TestErrorSetLastError.c +++ b/winpr/libwinpr/error/test/TestErrorSetLastError.c @@ -60,6 +60,7 @@ int TestErrorSetLastError(int argc, char* argv[]) { DWORD error; HANDLE threads[4]; + int i; /* We must initialize WLog here. It will check for settings * in the environment and if the variables are not set, the last @@ -85,10 +86,14 @@ int TestErrorSetLastError(int argc, char* argv[]) } *pLoopCount = 0; - threads[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_error_thread, (void*) (size_t) 0, 0, NULL); - threads[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_error_thread, (void*) (size_t) 1, 0, NULL); - threads[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_error_thread, (void*) (size_t) 2, 0, NULL); - threads[3] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_error_thread, (void*) (size_t) 3, 0, NULL); + for (i = 0; i < 4; i++) + { + if (!(threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_error_thread, (void*) (size_t) 0, 0, NULL))) + { + printf("Failed to create thread #%d\n", i); + return -1; + } + } // let the threads run for at least 2 seconds Sleep(2000); diff --git a/winpr/libwinpr/pool/pool.c b/winpr/libwinpr/pool/pool.c index 978caaef0..a91b19f8f 100644 --- a/winpr/libwinpr/pool/pool.c +++ b/winpr/libwinpr/pool/pool.c @@ -252,9 +252,12 @@ BOOL SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic) while (ArrayList_Count(ptpp->Threads) < ptpp->Minimum) { - thread = CreateThread(NULL, 0, + if (!(thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) thread_pool_work_func, - (void*) ptpp, 0, NULL); + (void*) ptpp, 0, NULL))) + { + return FALSE; + } ArrayList_Add(ptpp->Threads, thread); } diff --git a/winpr/libwinpr/pool/test/TestPoolThread.c b/winpr/libwinpr/pool/test/TestPoolThread.c index a26562484..65d11f37e 100644 --- a/winpr/libwinpr/pool/test/TestPoolThread.c +++ b/winpr/libwinpr/pool/test/TestPoolThread.c @@ -24,7 +24,12 @@ int TestPoolThread(int argc, char* argv[]) return -1; } - SetThreadpoolThreadMinimum(pool, 8); /* default is 0 */ + if (!SetThreadpoolThreadMinimum(pool, 8)) /* default is 0 */ + { + printf("SetThreadpoolThreadMinimum failed\n"); + return -1; + } + SetThreadpoolThreadMaximum(pool, 64); /* default is 500 */ CloseThreadpool(pool); diff --git a/winpr/libwinpr/pool/test/TestPoolWork.c b/winpr/libwinpr/pool/test/TestPoolWork.c index 64680b727..08b4c9af1 100644 --- a/winpr/libwinpr/pool/test/TestPoolWork.c +++ b/winpr/libwinpr/pool/test/TestPoolWork.c @@ -64,7 +64,12 @@ int TestPoolWork(int argc, char* argv[]) return -1; } - SetThreadpoolThreadMinimum(pool, 4); + if (!SetThreadpoolThreadMinimum(pool, 4)) + { + printf("SetThreadpoolThreadMinimum failure\n"); + return -1; + } + SetThreadpoolThreadMaximum(pool, 8); InitializeThreadpoolEnvironment(&environment); diff --git a/winpr/libwinpr/sspi/test/TestSchannel.c b/winpr/libwinpr/sspi/test/TestSchannel.c index 162486024..61decbfc8 100644 --- a/winpr/libwinpr/sspi/test/TestSchannel.c +++ b/winpr/libwinpr/sspi/test/TestSchannel.c @@ -627,7 +627,12 @@ int TestSchannel(int argc, char* argv[]) return -1; } - thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) schannel_test_server_thread, NULL, 0, NULL); + if (!(thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) schannel_test_server_thread, NULL, 0, NULL))) + { + printf("Failed to create server thread\n"); + return -1; + } + table = InitSecurityInterface(); status = QuerySecurityPackageInfo(SCHANNEL_NAME, &pPackageInfo); diff --git a/winpr/libwinpr/synch/test/TestSynchBarrier.c b/winpr/libwinpr/synch/test/TestSynchBarrier.c index 9835ff500..9f804eea0 100644 --- a/winpr/libwinpr/synch/test/TestSynchBarrier.c +++ b/winpr/libwinpr/synch/test/TestSynchBarrier.c @@ -59,8 +59,16 @@ int TestSynchBarrier(int argc, char* argv[]) for (index = 0; index < 5; index++) { - threads[index] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) - test_synch_barrier_thread_func, NULL, 0, NULL); + if (!(threads[index] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) + test_synch_barrier_thread_func, NULL, 0, NULL))) + { + printf("%s: CreateThread failed for thread #%d. GetLastError() = 0x%08x\n", __FUNCTION__, index, GetLastError()); + while (index) + CloseHandle(threads[--index]); + DeleteCriticalSection(&g_Lock); + CloseHandle(g_Event); + return -1; + } } WaitForSingleObject(g_Event, INFINITE); diff --git a/winpr/libwinpr/synch/test/TestSynchCritical.c b/winpr/libwinpr/synch/test/TestSynchCritical.c index 715a5b1ea..426baf59d 100644 --- a/winpr/libwinpr/synch/test/TestSynchCritical.c +++ b/winpr/libwinpr/synch/test/TestSynchCritical.c @@ -217,8 +217,13 @@ static PVOID TestSynchCritical_Main(PVOID arg) /* the TestSynchCritical_Test1 threads shall run until bTest1Running is FALSE */ bTest1Running = TRUE; - for (i = 0; i < (int) dwThreadCount; i++) { - hThreads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) TestSynchCritical_Test1, &bTest1Running, 0, NULL); + for (i = 0; i < (int) dwThreadCount; i++) + { + if (!(hThreads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) TestSynchCritical_Test1, &bTest1Running, 0, NULL))) + { + printf("CriticalSection failure: Failed to create test_1 thread #%d\n", i); + goto fail; + } } /* let it run for TEST_SYNC_CRITICAL_TEST1_RUNTIME_MS ... */ @@ -265,7 +270,11 @@ static PVOID TestSynchCritical_Main(PVOID arg) goto fail; } /* This thread tries to call TryEnterCriticalSection which must fail */ - hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) TestSynchCritical_Test2, NULL, 0, NULL); + if (!(hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) TestSynchCritical_Test2, NULL, 0, NULL))) + { + printf("CriticalSection failure: Failed to create test_2 thread\n"); + goto fail; + } if (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0) { printf("CriticalSection failure: Failed to wait for thread\n"); @@ -300,7 +309,11 @@ int TestSynchCritical(int argc, char* argv[]) printf("Deadlock will be assumed after %u ms.\n", dwDeadLockDetectionTimeMs); - hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) TestSynchCritical_Main, &bThreadTerminated, 0, NULL); + if (!(hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) TestSynchCritical_Main, &bThreadTerminated, 0, NULL))) + { + printf("CriticalSection failure: Failed to create main thread\n"); + return -1; + } /** * We have to be able to detect dead locks in this test. diff --git a/winpr/libwinpr/utils/test/TestMessagePipe.c b/winpr/libwinpr/utils/test/TestMessagePipe.c index 6006e1b0d..215b7a577 100644 --- a/winpr/libwinpr/utils/test/TestMessagePipe.c +++ b/winpr/libwinpr/utils/test/TestMessagePipe.c @@ -65,17 +65,41 @@ static void* message_echo_pipe_server_thread(void* arg) int TestMessagePipe(int argc, char* argv[]) { - HANDLE ClientThread; - HANDLE ServerThread; - wMessagePipe* EchoPipe; + HANDLE ClientThread = NULL; + HANDLE ServerThread = NULL; + wMessagePipe* EchoPipe = NULL; + int ret = 1; - EchoPipe = MessagePipe_New(); + if (!(EchoPipe = MessagePipe_New())) + { + printf("failed to create message pipe\n"); + goto out; + } - ClientThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) message_echo_pipe_client_thread, (void*) EchoPipe, 0, NULL); - ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) message_echo_pipe_server_thread, (void*) EchoPipe, 0, NULL); + if (!(ClientThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) message_echo_pipe_client_thread, (void*) EchoPipe, 0, NULL))) + { + printf("failed to create client thread\n"); + goto out; + } + + if (!(ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) message_echo_pipe_server_thread, (void*) EchoPipe, 0, NULL))) + { + printf("failed to create server thread\n"); + goto out; + } WaitForSingleObject(ClientThread, INFINITE); WaitForSingleObject(ServerThread, INFINITE); - return 0; + ret = 0; + +out: + if (EchoPipe) + MessagePipe_Free(EchoPipe); + if (ClientThread) + CloseHandle(ClientThread); + if (ServerThread) + CloseHandle(ServerThread); + + return ret; } diff --git a/winpr/libwinpr/utils/test/TestMessageQueue.c b/winpr/libwinpr/utils/test/TestMessageQueue.c index 10a5942e6..37fb185eb 100644 --- a/winpr/libwinpr/utils/test/TestMessageQueue.c +++ b/winpr/libwinpr/utils/test/TestMessageQueue.c @@ -29,9 +29,18 @@ int TestMessageQueue(int argc, char* argv[]) HANDLE thread; wMessageQueue* queue; - queue = MessageQueue_New(NULL); + if (!(queue = MessageQueue_New(NULL))) + { + printf("failed to create message queue\n"); + return 1; + } - thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) message_queue_consumer_thread, (void*) queue, 0, NULL); + if (!(thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) message_queue_consumer_thread, (void*) queue, 0, NULL))) + { + printf("failed to create thread\n"); + MessageQueue_Free(queue); + return 1; + } MessageQueue_Post(queue, NULL, 123, NULL, NULL); MessageQueue_Post(queue, NULL, 456, NULL, NULL); @@ -41,6 +50,7 @@ int TestMessageQueue(int argc, char* argv[]) WaitForSingleObject(thread, INFINITE); MessageQueue_Free(queue); + CloseHandle(thread); return 0; }