Merge pull request #2594 from baruchsiach/fix-math-2
authorBernhard Miklautz <bmiklautz@users.noreply.github.com>
Fri, 8 May 2015 11:44:26 +0000 (13:44 +0200)
committerBernhard Miklautz <bmiklautz@users.noreply.github.com>
Fri, 8 May 2015 11:44:26 +0000 (13:44 +0200)
Don't disable C99 long double math on non Unix platforms

51 files changed:
channels/urbdrc/client/urbdrc_main.c
client/Android/FreeRDPCore/jni/android_freerdp.c
client/DirectFB/dfreerdp.c
client/Mac/MRDPView.m
client/Mac/mf_client.m
client/Sample/freerdp.c
client/Wayland/wlfreerdp.c
client/Windows/wf_client.c
client/X11/xf_client.c
include/freerdp/listener.h
libfreerdp/codec/rfx.c
libfreerdp/core/fastpath.c
libfreerdp/core/freerdp.c
libfreerdp/core/listener.c
libfreerdp/core/message.c
libfreerdp/core/rdp.c
libfreerdp/core/transport.c
libfreerdp/core/update.c
libfreerdp/core/update.h
libfreerdp/core/window.c
libfreerdp/crypto/certificate.c
server/Sample/sfreerdp.c
server/Windows/wf_directsound.c
server/Windows/wf_interface.c
server/Windows/wf_peer.c
server/Windows/wf_rdpsnd.c
server/Windows/wf_wasapi.c
server/shadow/Mac/mac_shadow.c
server/shadow/Win/win_rdp.c
server/shadow/Win/win_shadow.c
server/shadow/X11/x11_shadow.c
server/shadow/shadow_client.c
server/shadow/shadow_server.c
winpr/include/winpr/bitstream.h
winpr/include/winpr/wlog.h
winpr/libwinpr/error/test/TestErrorSetLastError.c
winpr/libwinpr/interlocked/interlocked.c
winpr/libwinpr/pipe/pipe.c
winpr/libwinpr/pool/pool.c
winpr/libwinpr/pool/test/TestPoolThread.c
winpr/libwinpr/pool/test/TestPoolWork.c
winpr/libwinpr/smartcard/smartcard_inspect.c
winpr/libwinpr/sspi/test/TestSchannel.c
winpr/libwinpr/synch/test/TestSynchBarrier.c
winpr/libwinpr/synch/test/TestSynchCritical.c
winpr/libwinpr/thread/test/TestThreadCreateProcess.c
winpr/libwinpr/utils/ssl.c
winpr/libwinpr/utils/test/TestMessagePipe.c
winpr/libwinpr/utils/test/TestMessageQueue.c
winpr/libwinpr/utils/wlog/BinaryAppender.c
winpr/libwinpr/utils/wlog/ConsoleAppender.c

index 38096b2..6db6edd 100644 (file)
@@ -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;
index 81260a1..8268525 100644 (file)
@@ -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;
 }
index f32f3ac..5e26078 100644 (file)
@@ -454,7 +454,11 @@ int main(int argc, char* argv[])
 
        setlocale(LC_ALL, "");
 
-       g_sem = CreateSemaphore(NULL, 0, 1, NULL);
+       if (!(g_sem = CreateSemaphore(NULL, 0, 1, NULL)))
+       {
+               WLog_ERR(TAG, "Failed to create semaphore");
+               exit(1);
+       }
 
        instance = freerdp_new();
        instance->PreConnect = df_pre_connect;
index 3f3f9ef..ce18f3c 100644 (file)
@@ -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);
                }
                
index 41a9364..18fe41c 100644 (file)
@@ -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)
index e4614a2..70722f6 100644 (file)
@@ -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);
 
index fff00a1..5d96f27 100644 (file)
@@ -118,7 +118,7 @@ static BOOL wl_post_connect(freerdp* instance)
        wlfWindow* window;
        wlfContext* context;
 
-       gdi_init(instance, CLRCONV_ALPHA | CLRCONV_INVERT | CLRBUF_32BPP, NULL);
+       gdi_init(instance, CLRCONV_ALPHA | CLRBUF_32BPP, NULL);
        gdi = instance->context->gdi;
        if (!gdi)
                return FALSE;
index e639b31..f07e2ab 100644 (file)
@@ -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.");
 
index 3209b3d..4338842 100644 (file)
@@ -589,23 +589,22 @@ BOOL xf_create_window(xfContext* xfc)
                XFreeModifiermap(xfc->modifierMap);
 
        xfc->modifierMap = XGetModifierMapping(xfc->display);
-       assert(!xfc->gc);
-       xfc->gc = XCreateGC(xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
-       assert(!xfc->primary);
-       xfc->primary = XCreatePixmap(xfc->display, xfc->drawable, xfc->sessionWidth, xfc->sessionHeight, xfc->depth);
+       if (!xfc->gc);
+               xfc->gc = XCreateGC(xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
+       if (!xfc->primary);
+               xfc->primary = XCreatePixmap(xfc->display, xfc->drawable, xfc->sessionWidth, xfc->sessionHeight, xfc->depth);
        xfc->drawing = xfc->primary;
-       assert(!xfc->bitmap_mono);
-       xfc->bitmap_mono = XCreatePixmap(xfc->display, xfc->drawable, 8, 8, 1);
-       assert(!xfc->gc_mono);
-       xfc->gc_mono = XCreateGC(xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
+       if (!xfc->bitmap_mono);
+               xfc->bitmap_mono = XCreatePixmap(xfc->display, xfc->drawable, 8, 8, 1);
+       if (!xfc->gc_mono);
+               xfc->gc_mono = XCreateGC(xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
        XSetFunction(xfc->display, xfc->gc, GXcopy);
        XSetFillStyle(xfc->display, xfc->gc, FillSolid);
        XSetForeground(xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
        XFillRectangle(xfc->display, xfc->primary, xfc->gc, 0, 0, xfc->sessionWidth, xfc->sessionHeight);
        XFlush(xfc->display);
-       assert(!xfc->image);
-
-       xfc->image = XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
+       if (!xfc->image);
+               xfc->image = XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
                        (char*) xfc->primary_buffer, xfc->sessionWidth, xfc->sessionHeight, xfc->scanline_pad, 0);
 
        return TRUE;
@@ -1417,10 +1416,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 +1430,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 +1520,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 +1631,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;
 }
index e502a41..95a0379 100644 (file)
@@ -33,6 +33,7 @@ extern "C" {
 
 typedef BOOL (*psListenerOpen)(freerdp_listener* instance, const char* bind_address, UINT16 port);
 typedef BOOL (*psListenerOpenLocal)(freerdp_listener* instance, const char* path);
+typedef BOOL (*psListenerOpenFromSocket)(freerdp_listener* instance, int fd);
 typedef BOOL (*psListenerGetFileDescriptor)(freerdp_listener* instance, void** rfds, int* rcount);
 typedef DWORD (*psListenerGetEventHandles)(freerdp_listener* instance, HANDLE* events, DWORD nCount);
 typedef BOOL (*psListenerCheckFileDescriptor)(freerdp_listener* instance);
@@ -56,6 +57,7 @@ struct rdp_freerdp_listener
        psListenerClose Close;
 
        psPeerAccepted PeerAccepted;
+       psListenerOpenFromSocket OpenFromSocket;
 };
 
 FREERDP_API freerdp_listener* freerdp_listener_new(void);
index 6f5a465..54e8699 100644 (file)
@@ -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:
index 14ea6b8..9056393 100644 (file)
@@ -403,14 +403,14 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                if (fastpath->fragmentation != -1)
                {
                        WLog_ERR(TAG,  "Unexpected FASTPATH_FRAGMENT_SINGLE");
-                       return -1;
+                       goto out_fail;
                }
 
                totalSize = size;
                status = fastpath_recv_update(fastpath, updateCode, totalSize, cs);
 
                if (status < 0)
-                       return -1;
+                       goto out_fail;
        }
        else
        {
@@ -419,7 +419,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                        if (fastpath->fragmentation != -1)
                        {
                                WLog_ERR(TAG,  "Unexpected FASTPATH_FRAGMENT_FIRST");
-                               return -1;
+                               goto out_fail;
                        }
 
                        fastpath->fragmentation = FASTPATH_FRAGMENT_FIRST;
@@ -430,11 +430,11 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                        {
                                WLog_ERR(TAG,  "Total size (%d) exceeds MultifragMaxRequestSize (%d)",
                                                 totalSize, transport->settings->MultifragMaxRequestSize);
-                               return -1;
+                               goto out_fail;
                        }
 
                        if (!(fastpath->updateData = StreamPool_Take(transport->ReceivePool, size)))
-                               return -1;
+                               goto out_fail;
 
                        Stream_SetPosition(fastpath->updateData, 0);
 
@@ -446,7 +446,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                                        (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
                        {
                                WLog_ERR(TAG,  "Unexpected FASTPATH_FRAGMENT_NEXT");
-                               return -1;
+                               goto out_fail;
                        }
 
                        fastpath->fragmentation = FASTPATH_FRAGMENT_NEXT;
@@ -457,13 +457,13 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                        {
                                WLog_ERR(TAG,  "Total size (%d) exceeds MultifragMaxRequestSize (%d)",
                                                 totalSize, transport->settings->MultifragMaxRequestSize);
-                               return -1;
+                               goto out_fail;
                        }
 
                        if (!Stream_EnsureCapacity(fastpath->updateData, totalSize))
                        {
                                WLog_ERR(TAG,  "Couldn't re-allocate memory for stream");
-                               return -1;
+                               goto out_fail;
                        }
 
                        Stream_Copy(fastpath->updateData, cs, size);
@@ -474,7 +474,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                                        (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
                        {
                                WLog_ERR(TAG,  "Unexpected FASTPATH_FRAGMENT_LAST");
-                               return -1;
+                               goto out_fail;
                        }
 
                        fastpath->fragmentation = -1;
@@ -485,13 +485,13 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                        {
                                WLog_ERR(TAG,  "Total size (%d) exceeds MultifragMaxRequestSize (%d)",
                                                 totalSize, transport->settings->MultifragMaxRequestSize);
-                               return -1;
+                               goto out_fail;
                        }
 
                        if (!Stream_EnsureCapacity(fastpath->updateData, totalSize))
                        {
                                WLog_ERR(TAG,  "Couldn't re-allocate memory for stream");
-                               return -1;
+                               goto out_fail;
                        }
 
                        Stream_Copy(fastpath->updateData, cs, size);
@@ -504,7 +504,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                        Stream_Release(fastpath->updateData);
 
                        if (status < 0)
-                               return -1;
+                               goto out_fail;
                }
        }
 
@@ -514,6 +514,14 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
                Stream_Release(cs);
 
        return status;
+
+out_fail:
+
+    if (cs != s) {
+        Stream_Release(cs);
+    }
+
+    return -1;
 }
 
 int fastpath_recv_updates(rdpFastPath* fastpath, wStream* s)
index 77ed203..44015c4 100644 (file)
@@ -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");
 
index b649ac5..41cb64d 100644 (file)
@@ -115,6 +115,12 @@ static BOOL freerdp_listener_open(freerdp_listener* instance, const char* bind_a
                if ((ai->ai_family != AF_INET) && (ai->ai_family != AF_INET6))
                        continue;
 
+               if (listener->num_sockfds == MAX_LISTENER_HANDLES)
+               {
+                       WLog_ERR(TAG, "too many listening sockets");
+                       continue;
+               }
+
                sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
 
                if (sockfd == -1)
@@ -180,6 +186,13 @@ static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char*
        int sockfd;
        struct sockaddr_un addr;
        rdpListener* listener = (rdpListener*) instance->listener;
+       HANDLE hevent;
+
+       if (listener->num_sockfds == MAX_LISTENER_HANDLES)
+       {
+               WLog_ERR(TAG, "too many listening sockets");
+               return FALSE;
+       }
 
        sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
 
@@ -213,8 +226,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;
@@ -223,6 +243,36 @@ static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char*
 #endif
 }
 
+static BOOL freerdp_listener_open_from_socket(freerdp_listener* instance, int fd)
+{
+#ifndef _WIN32
+       rdpListener* listener = (rdpListener*) instance->listener;
+
+       if (listener->num_sockfds == MAX_LISTENER_HANDLES)
+       {
+               WLog_ERR(TAG, "too many listening sockets");
+               return FALSE;
+       }
+
+       if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
+               return FALSE;
+
+       listener->sockfds[listener->num_sockfds] = fd;
+       listener->events[listener->num_sockfds] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, fd);
+       if (!listener->events[listener->num_sockfds])
+               return FALSE;
+
+       listener->num_sockfds++;
+
+       WLog_INFO(TAG, "Listening on socket %d.", fd);
+       return TRUE;
+#else
+       return FALSE;
+#endif
+
+
+}
+
 static void freerdp_listener_close(freerdp_listener* instance)
 {
        int i;
@@ -367,6 +417,7 @@ freerdp_listener* freerdp_listener_new(void)
 
        instance->Open = freerdp_listener_open;
        instance->OpenLocal = freerdp_listener_open_local;
+       instance->OpenFromSocket = freerdp_listener_open_from_socket;
        instance->GetFileDescriptor = freerdp_listener_get_fds;
        instance->GetEventHandles = freerdp_listener_get_event_handles;
        instance->CheckFileDescriptor = freerdp_listener_check_fds;
index aa336ca..e0bab40 100644 (file)
@@ -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)
-       {
-               ZeroMemory(message, sizeof(rdpUpdateProxy));
+       if (!(message = (rdpUpdateProxy *) calloc(1, sizeof(rdpUpdateProxy))))
+               return NULL;
 
-               message->update = update;
-               update_message_register_interface(message, update);
-               message->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) update_message_proxy_thread, update, 0, 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)))
+       {
+               WLog_ERR(TAG, "Failed to create proxy thread");
+               free(message);
+               return NULL;
        }
 
        return message;
index 6fcff23..2ddde95 100644 (file)
@@ -815,72 +815,72 @@ int rdp_recv_data_pdu(rdpRdp* rdp, wStream* s)
        {
                case DATA_PDU_TYPE_UPDATE:
                        if (!update_recv(rdp->update, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_CONTROL:
                        if (!rdp_recv_server_control_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_POINTER:
                        if (!update_recv_pointer(rdp->update, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_SYNCHRONIZE:
                        if (!rdp_recv_synchronize_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_PLAY_SOUND:
                        if (!update_recv_play_sound(rdp->update, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_SHUTDOWN_DENIED:
                        if (!rdp_recv_server_shutdown_denied_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_SAVE_SESSION_INFO:
                        if (!rdp_recv_save_session_info(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_FONT_MAP:
                        if (!rdp_recv_font_map_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS:
                        if (!rdp_recv_server_set_keyboard_indicators_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS:
                        if (!rdp_recv_server_set_keyboard_ime_status_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_SET_ERROR_INFO:
                        if (!rdp_recv_set_error_info_data_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_ARC_STATUS:
                        if (!rdp_recv_server_auto_reconnect_status_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_STATUS_INFO:
                        if (!rdp_recv_server_status_info_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                case DATA_PDU_TYPE_MONITOR_LAYOUT:
                        if (!rdp_recv_monitor_layout_pdu(rdp, cs))
-                               return -1;
+                               goto out_fail;
                        break;
 
                default:
@@ -891,6 +891,11 @@ int rdp_recv_data_pdu(rdpRdp* rdp, wStream* s)
                Stream_Release(cs);
 
        return 0;
+
+out_fail:
+    if (cs != s)
+        Stream_Release(cs);
+    return -1;
 }
 
 int rdp_recv_message_channel_pdu(rdpRdp* rdp, wStream* s)
index d345052..d2d0d18 100644 (file)
@@ -109,18 +109,15 @@ BOOL transport_connect_tls(rdpTransport* transport)
        rdpContext* context = transport->context;
        rdpSettings* settings = transport->settings;
 
+       if (!(tls = tls_new(settings)))
+               return FALSE;
+
+       transport->tls = tls;
+
        if (transport->GatewayEnabled)
-       {
-               tls = transport->tls = tls_new(settings);
                transport->layer = TRANSPORT_LAYER_TSG_TLS;
-       }
        else
-       {
-               tls = transport->tls = tls_new(settings);
                transport->layer = TRANSPORT_LAYER_TLS;
-       }
-
-       transport->tls = tls;
 
        tls->hostname = settings->ServerHostname;
        tls->port = settings->ServerPort;
index 746c69d..d25f942 100644 (file)
@@ -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)
index c0b266f..2c363fe 100644 (file)
@@ -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);
index 701fc82..c5d2481 100644 (file)
@@ -43,6 +43,14 @@ static BOOL rail_read_unicode_string(wStream* s, RAIL_UNICODE_STRING* unicode_st
        if (Stream_GetRemainingLength(s) < new_len)
                return FALSE;
 
+       if (!new_len)
+       {
+               free(unicode_string->string);
+               unicode_string->string = NULL;
+               unicode_string->length = 0;
+               return TRUE;
+       }
+
        new_str = (BYTE*) realloc(unicode_string->string, new_len);
        if (!new_str)
        {
index 945021d..fbe804d 100644 (file)
@@ -42,66 +42,75 @@ static const char certificate_known_hosts_file[] = "known_hosts";
 
 #define TAG FREERDP_TAG("crypto")
 
-int certificate_store_init(rdpCertificateStore* certificate_store)
+BOOL certificate_store_init(rdpCertificateStore* certificate_store)
 {
-       char* server_path;
+       char* server_path = NULL;
        rdpSettings* settings;
 
        settings = certificate_store->settings;
 
        if (!PathFileExistsA(settings->ConfigPath))
        {
-               CreateDirectoryA(settings->ConfigPath, 0);
+               if (!CreateDirectoryA(settings->ConfigPath, 0))
+               {
+                       WLog_ERR(TAG,  "error creating directory '%s'", settings->ConfigPath);
+                       goto fail;
+               }
                WLog_INFO(TAG,  "creating directory %s", settings->ConfigPath);
        }
 
-       certificate_store->path = GetCombinedPath(settings->ConfigPath, (char*) certificate_store_dir);
-
-       if (!certificate_store->path)
-               return -1;
+       if (!(certificate_store->path = GetCombinedPath(settings->ConfigPath, (char*) certificate_store_dir)))
+               goto fail;
 
        if (!PathFileExistsA(certificate_store->path))
        {
-               CreateDirectoryA(certificate_store->path, 0);
-               WLog_INFO(TAG,  "creating directory %s", certificate_store->path);
+               if (!CreateDirectoryA(certificate_store->path, 0))
+               {
+                       WLog_ERR(TAG,  "error creating directory [%s]", certificate_store->path);
+                       goto fail;
+               }
+               WLog_INFO(TAG,  "creating directory [%s]", certificate_store->path);
        }
 
-       server_path = GetCombinedPath(settings->ConfigPath, (char*) certificate_server_dir);
-
-       if (!server_path)
-               return -1;
+       if (!(server_path = GetCombinedPath(settings->ConfigPath, (char*) certificate_server_dir)))
+               goto fail;
 
        if (!PathFileExistsA(server_path))
        {
-               CreateDirectoryA(server_path, 0);
-               WLog_INFO(TAG,  "creating directory %s", server_path);
+               if (!CreateDirectoryA(server_path, 0))
+               {
+                       WLog_ERR(TAG,  "error creating directory [%s]", server_path);
+                       goto fail;
+               }
+               WLog_INFO(TAG,  "created directory [%s]", server_path);
        }
 
-       free(server_path);
-
-       certificate_store->file = GetCombinedPath(settings->ConfigPath, (char*) certificate_known_hosts_file);
-
-       if (!certificate_store->file)
-               return -1;
+       if (!(certificate_store->file = GetCombinedPath(settings->ConfigPath, (char*) certificate_known_hosts_file)))
+               goto fail;
 
-       if (PathFileExistsA(certificate_store->file) == FALSE)
-       {
+       if (!PathFileExistsA(certificate_store->file))
                certificate_store->fp = fopen((char*) certificate_store->file, "w+");
-
-               if (!certificate_store->fp)
-               {
-                       WLog_ERR(TAG,  "certificate_store_open: error opening [%s] for writing", certificate_store->file);
-                       return -1;
-               }
-
-               fflush(certificate_store->fp);
-       }
        else
-       {
                certificate_store->fp = fopen((char*) certificate_store->file, "r+");
+
+       if (!certificate_store->fp)
+       {
+               WLog_ERR(TAG,  "error opening [%s]", certificate_store->file);
+               goto fail;
        }
 
-       return 1;
+       free(server_path);
+
+       return TRUE;
+
+fail:
+       WLog_ERR(TAG,  "certificate store initialization failed");
+       free(server_path);
+       free(certificate_store->path);
+       free(certificate_store->file);
+       certificate_store->path = NULL;
+       certificate_store->file = NULL;
+       return FALSE;
 }
 
 int certificate_data_match(rdpCertificateStore* certificate_store, rdpCertificateData* certificate_data)
@@ -285,7 +294,11 @@ rdpCertificateStore* certificate_store_new(rdpSettings* settings)
 
        certificate_store->settings = settings;
 
-       certificate_store_init(certificate_store);
+       if (!certificate_store_init(certificate_store))
+       {
+               free(certificate_store);
+               return NULL;
+       }
 
        return certificate_store;
 }
index 42cf3b1..7d53551 100644 (file)
@@ -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)
index 649d9a8..1424839 100644 (file)
@@ -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;
 }
index 4b9b9b2..2a18c4a 100644 (file)
@@ -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)
index a1ef07c..598080e 100644 (file)
@@ -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;
 }
 
index d9d7f46..3fdffe1 100644 (file)
@@ -142,11 +142,14 @@ int wf_rdpsnd_unlock()
 
 BOOL wf_peer_rdpsnd_init(wfPeerContext* context)
 {
-       wfInfo* wfi;
+       wfInfo* wfi = wf_info_get_instance();
        
-       wfi = wf_info_get_instance();
+       if (!wfi)
+               return FALSE;
+
+       if (!(wfi->snd_mutex = CreateMutex(NULL, FALSE, NULL)))
+               return FALSE;
 
-       wfi->snd_mutex = CreateMutex(NULL, FALSE, NULL);
        context->rdpsnd = rdpsnd_server_context_new(context->vcm);
        context->rdpsnd->data = context;
 
index 58275fd..1a484a4 100644 (file)
@@ -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;
 }
index 7efd78b..95e99e1 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <freerdp/codec/color.h>
 #include <freerdp/codec/region.h>
+#include <freerdp/log.h>
 
 #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;
 }
index 7783a47..e0e672b 100644 (file)
@@ -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;
 }
index b70b61f..0d2cb21 100644 (file)
@@ -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;
 }
index 80ff076..36950dc 100644 (file)
@@ -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;
 }
index 15ee9de..b0f825d 100644 (file)
@@ -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;
 }
index 6d8315d..11b6090 100644 (file)
@@ -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;
@@ -431,16 +434,26 @@ int shadow_server_init_config_path(rdpShadowServer* server)
 
                if (userLibraryPath)
                {
-                       if (!PathFileExistsA(userLibraryPath))
-                               CreateDirectoryA(userLibraryPath, 0);
+                       if (!PathFileExistsA(userLibraryPath) &&
+                               !CreateDirectoryA(userLibraryPath, 0))
+                       {
+                               WLog_ERR(TAG, "Failed to create directory '%s'", userLibraryPath);
+                               free(userLibraryPath);
+                               return -1;
+                       }
 
                        userApplicationSupportPath = GetCombinedPath(userLibraryPath, "Application Support");
 
                        if (userApplicationSupportPath)
                        {
-                               if (!PathFileExistsA(userApplicationSupportPath))
-                                       CreateDirectoryA(userApplicationSupportPath, 0);
-
+                               if (!PathFileExistsA(userApplicationSupportPath) &&
+                                       !CreateDirectoryA(userApplicationSupportPath, 0))
+                               {
+                                       WLog_ERR(TAG, "Failed to create directory '%s'", userApplicationSupportPath);
+                                       free(userLibraryPath);
+                                       free(userApplicationSupportPath);
+                                       return -1;
+                               }
                                server->ConfigPath = GetCombinedPath(userApplicationSupportPath, "freerdp");
                        }
 
@@ -458,11 +471,14 @@ int shadow_server_init_config_path(rdpShadowServer* server)
 
                if (configHome)
                {
-                       if (!PathFileExistsA(configHome))
-                               CreateDirectoryA(configHome, 0);
-
+                       if (!PathFileExistsA(configHome) &&
+                               !CreateDirectoryA(configHome, 0))
+                       {
+                               WLog_ERR(TAG, "Failed to create directory '%s'", configHome);
+                               free(configHome);
+                               return -1;
+                       }
                        server->ConfigPath = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, "freerdp");
-
                        free(configHome);
                }
        }
@@ -489,16 +505,23 @@ int shadow_server_init_certificate(rdpShadowServer* server)
 
        int makecert_argc = (sizeof(makecert_argv) / sizeof(char*));
 
-       if (!PathFileExistsA(server->ConfigPath))
-               CreateDirectoryA(server->ConfigPath, 0);
-
-       filepath = GetCombinedPath(server->ConfigPath, "shadow");
+       if (!PathFileExistsA(server->ConfigPath) &&
+               !CreateDirectoryA(server->ConfigPath, 0))
+       {
+               WLog_ERR(TAG, "Failed to create directory '%s'", server->ConfigPath);
+               return -1;
+       }
 
-       if (!filepath)
+       if (!(filepath = GetCombinedPath(server->ConfigPath, "shadow")))
                return -1;
 
-       if (!PathFileExistsA(filepath))
-               CreateDirectoryA(filepath, 0);
+       if (!PathFileExistsA(filepath) &&
+               !CreateDirectoryA(filepath, 0))
+       {
+               WLog_ERR(TAG, "Failed to create directory '%s'", filepath);
+               free(filepath);
+               return -1;
+       }
 
        server->CertificateFile = GetCombinedPath(filepath, "shadow.crt");
        server->PrivateKeyFile = GetCombinedPath(filepath, "shadow.key");
index 21d3a90..fca98e4 100644 (file)
@@ -49,37 +49,37 @@ extern "C" {
 
 #define BitStream_Prefetch(_bs) do { \
                (_bs->prefetch) = 0; \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 4)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 4) < (_bs->capacity)) \
                        (_bs->prefetch) |= (*(_bs->pointer + 4) << 24); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 5)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 5) < (_bs->capacity)) \
                        (_bs->prefetch) |= (*(_bs->pointer + 5) << 16); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 6)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 6) < (_bs->capacity)) \
                        (_bs->prefetch) |= (*(_bs->pointer + 6) << 8); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 7)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 7) < (_bs->capacity)) \
                        (_bs->prefetch) |= (*(_bs->pointer + 7) << 0); \
        } while(0)
 
 #define BitStream_Fetch(_bs) do { \
                (_bs->accumulator) = 0; \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 0)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 0) < (_bs->capacity)) \
                        (_bs->accumulator) |= (*(_bs->pointer + 0) << 24); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 1)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 1) < (_bs->capacity)) \
                        (_bs->accumulator) |= (*(_bs->pointer + 1) << 16); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 2)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 2) < (_bs->capacity)) \
                        (_bs->accumulator) |= (*(_bs->pointer + 2) << 8); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) <(_bs->capacity + 3)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 3) <(_bs->capacity)) \
                        (_bs->accumulator) |= (*(_bs->pointer + 3) << 0); \
                BitStream_Prefetch(_bs); \
        } while(0)
 
 #define BitStream_Flush(_bs) do { \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 0)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 0) < (_bs->capacity)) \
                        *(_bs->pointer + 0) = (_bs->accumulator >> 24); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 1)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 1) < (_bs->capacity)) \
                        *(_bs->pointer + 1) = (_bs->accumulator >> 16); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 2)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 2) < (_bs->capacity)) \
                        *(_bs->pointer + 2) = (_bs->accumulator >> 8); \
-               if (((UINT32) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 3)) \
+               if (((UINT32) (_bs->pointer - _bs->buffer) + 3) < (_bs->capacity)) \
                        *(_bs->pointer + 3) = (_bs->accumulator >> 0); \
        } while(0)
 
index bd55ffb..aea7a0d 100644 (file)
@@ -146,9 +146,10 @@ struct _wLogAppender
        WLOG_APPENDER_COMMON();
 };
 
-#define WLOG_CONSOLE_STDOUT    1
-#define WLOG_CONSOLE_STDERR    2
-#define WLOG_CONSOLE_DEBUG     3
+#define WLOG_CONSOLE_DEFAULT   0
+#define WLOG_CONSOLE_STDOUT            1
+#define WLOG_CONSOLE_STDERR            2
+#define WLOG_CONSOLE_DEBUG             4
 
 struct _wLogConsoleAppender
 {
index a447a01..44c986a 100644 (file)
@@ -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);
index 6490bb6..b5d7eef 100644 (file)
@@ -253,31 +253,36 @@ PVOID InterlockedCompareExchangePointer(PVOID volatile *Destination, PVOID Excha
 
 static volatile HANDLE mutex = NULL;
 
-int static_mutex_lock(volatile HANDLE* static_mutex)
+BOOL static_mutex_lock(volatile HANDLE* static_mutex)
 {
        if (*static_mutex == NULL)
        {
-               HANDLE handle = CreateMutex(NULL, FALSE, NULL);
+               HANDLE handle;
+
+               if (!(handle = CreateMutex(NULL, FALSE, NULL)))
+                       return FALSE;
 
                if (InterlockedCompareExchangePointer((PVOID*) static_mutex, (PVOID) handle, NULL) != NULL)
                        CloseHandle(handle);
        }
 
-       return (WaitForSingleObject(*static_mutex, INFINITE) == WAIT_FAILED);
+       return (WaitForSingleObject(*static_mutex, INFINITE) == WAIT_OBJECT_0);
 }
 
 LONGLONG InterlockedCompareExchange64(LONGLONG volatile *Destination, LONGLONG Exchange, LONGLONG Comperand)
 {
        LONGLONG previousValue = 0;
-
-       static_mutex_lock(&mutex);
+       BOOL locked = static_mutex_lock(&mutex);
 
        previousValue = *Destination;
 
        if (*Destination == Comperand)
                *Destination = Exchange;
 
-       ReleaseMutex(mutex);
+       if (locked)
+               ReleaseMutex(mutex);
+       else
+               fprintf(stderr, "WARNING: InterlockedCompareExchange64 operation might have failed\n");
 
        return previousValue;
 }
index f3dce5d..98ab3aa 100644 (file)
@@ -584,7 +584,11 @@ HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD
 
                if (!PathFileExistsA(lpPipePath))
                {
-                       CreateDirectoryA(lpPipePath, 0);
+                       if (!CreateDirectoryA(lpPipePath, 0))
+                       {
+                               free(lpPipePath);
+                               goto out;
+                       }
                        UnixChangeFileMode(lpPipePath, 0xFFFF);
                }
 
index 978caae..a91b19f 100644 (file)
@@ -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);
        }
index a265624..65d11f3 100644 (file)
@@ -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);
index 64680b7..08b4c9a 100644 (file)
@@ -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);
index ada50b4..192a48a 100644 (file)
@@ -1275,15 +1275,17 @@ void Inspect_InitLog()
        if (g_Log)
                return;
 
-       g_Log = WLog_Get("WinSCard");
+       if (!PathFileExistsA(filepath))
+               if (!CreateDirectoryA(filepath, NULL))
+                       return;
+
+       if (!(g_Log = WLog_Get("WinSCard")))
+               return;
 
        WLog_SetLogLevel(g_Log, WLOG_DEBUG);
        WLog_SetLogAppenderType(g_Log, WLOG_APPENDER_FILE);
        appender = (wLogFileAppender*) WLog_GetLogAppender(g_Log);
 
-       if (!PathFileExistsA(filepath))
-               CreateDirectoryA(filepath, NULL);
-
        WLog_FileAppender_SetOutputFileName(g_Log, appender, "WinSCard.txt");
        WLog_FileAppender_SetOutputFilePath(g_Log, appender, filepath);
 
index 1624860..61decbf 100644 (file)
@@ -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);
 
index 9835ff5..9f804ee 100644 (file)
@@ -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);
index 715a5b1..426baf5 100644 (file)
@@ -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.
index fb30f61..0ccc405 100644 (file)
@@ -55,6 +55,12 @@ int TestThreadCreateProcess(int argc, char* argv[])
                        &StartupInfo,
                        &ProcessInformation);
 
+       if (!status)
+       {
+               printf("CreateProcess failed. error=%d\n", GetLastError());
+               return 1;
+       }
+
        FreeEnvironmentStrings(lpszEnvironmentBlock);
 
        WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
index 44a0397..9e63d3a 100644 (file)
@@ -60,12 +60,15 @@ static void _winpr_openssl_locking(int mode, int type, const char* file, int lin
 
 static struct CRYPTO_dynlock_value* _winpr_openssl_dynlock_create(const char* file, int line)
 {
-       struct CRYPTO_dynlock_value* dynlock = (struct CRYPTO_dynlock_value*)
-                                                                                  malloc(sizeof(struct CRYPTO_dynlock_value));
+       struct CRYPTO_dynlock_value* dynlock;
 
-       if (dynlock)
+       if (!(dynlock = (struct CRYPTO_dynlock_value*) malloc(sizeof(struct CRYPTO_dynlock_value))))
+               return NULL;
+
+       if (!(dynlock->mutex = CreateMutex(NULL, FALSE, NULL)))
        {
-               dynlock->mutex = CreateMutex(NULL, FALSE, NULL);
+               free(dynlock);
+               return NULL;
        }
 
        return dynlock;
index 6006e1b..215b7a5 100644 (file)
@@ -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;
 }
index 10a5942..37fb185 100644 (file)
@@ -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;
 }
index 208891e..e37a3db 100644 (file)
@@ -96,7 +96,8 @@ int WLog_BinaryAppender_Open(wLog* log, wLogBinaryAppender* appender)
 
        if (!PathFileExistsA(appender->FilePath))
        {
-               CreateDirectoryA(appender->FilePath, 0);
+               if (!CreateDirectoryA(appender->FilePath, 0))
+                       return -1;
                UnixChangeFileMode(appender->FilePath, 0xFFFF);
        }
 
index 5de8e15..67ecdcb 100644 (file)
@@ -47,14 +47,14 @@ void WLog_ConsoleAppender_SetOutputStream(wLog* log, wLogConsoleAppender* append
                return;
 
        if (outputStream < 0)
-               outputStream = WLOG_CONSOLE_STDOUT;
+               outputStream = WLOG_CONSOLE_DEFAULT;
 
        if (outputStream == WLOG_CONSOLE_STDOUT)
                appender->outputStream = WLOG_CONSOLE_STDOUT;
        else if (outputStream == WLOG_CONSOLE_STDERR)
                appender->outputStream = WLOG_CONSOLE_STDERR;
        else
-               appender->outputStream = WLOG_CONSOLE_STDOUT;
+               appender->outputStream = WLOG_CONSOLE_DEFAULT;
 }
 
 int WLog_ConsoleAppender_Open(wLog* log, wLogConsoleAppender* appender)
@@ -123,9 +123,31 @@ int WLog_ConsoleAppender_WriteMessage(wLog* log, wLogConsoleAppender* appender,
                __android_log_print(level, log->Name, "%s%s", message->PrefixString, message->TextString);
 
 #else
-       fp = (appender->outputStream == WLOG_CONSOLE_STDERR) ? stderr : stdout;
+       switch(appender->outputStream)
+       {
+               case WLOG_CONSOLE_STDOUT:
+                       fp = stdout;
+                       break;
+               case WLOG_CONSOLE_STDERR:
+                       fp = stderr;
+                       break;
+               default:
+                       switch(message->Level)
+                       {
+                               case WLOG_TRACE:
+                               case WLOG_DEBUG:
+                               case WLOG_INFO:
+                                       fp = stdout;
+                                       break;
+                               default:
+                                       fp = stderr;
+                                       break;
+                       }
+                       break;
+       }
 
-       fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);
+       if (message->Level != WLOG_OFF)
+               fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);
 #endif
        return 1;
 }
@@ -211,11 +233,11 @@ wLogConsoleAppender* WLog_ConsoleAppender_New(wLog* log)
        ConsoleAppender->WritePacketMessage =
                        (WLOG_APPENDER_WRITE_PACKET_MESSAGE_FN) WLog_ConsoleAppender_WritePacketMessage;
 
-       ConsoleAppender->outputStream = WLOG_CONSOLE_STDOUT;
+       ConsoleAppender->outputStream = WLOG_CONSOLE_DEFAULT;
 
 #ifdef _WIN32
-    if (IsDebuggerPresent())
-        ConsoleAppender->outputStream = WLOG_CONSOLE_DEBUG;
+       if (IsDebuggerPresent())
+               ConsoleAppender->outputStream = WLOG_CONSOLE_DEBUG;
 #endif
 
        return ConsoleAppender;