From 9a05fa756390bd3bcddc19d2ee242078dcde5938 Mon Sep 17 00:00:00 2001 From: amg077-motorolasolutions-com <45261601+amg077-motorolasolutions-com@users.noreply.github.com> Date: Mon, 26 Nov 2018 09:59:06 +0100 Subject: [PATCH] Update PlatformThread.cpp Added handling thread creation failure when _beginthreadex() returns (uintptr_t )-1L. See MSDN: https://msdn.microsoft.com/en-us/library/kdzttdcb.aspx for details. Added errno and _doserrno to exception message. Reason: PlatformThread::createNewThread() is a public method. Developer who uses this method expects, that it is enough to catch an exception and next apply usual null pointer checking. Value -1L is different than NULL so it would cause memory access violation on de-reference. --- .../concurrent/windows/PlatformThread.cpp | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp b/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp index 9b7077342..117eb312d 100644 --- a/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp +++ b/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp @@ -352,15 +352,27 @@ void PlatformThread::createNewThread(decaf_thread_t* handle, threadMainMethod th int priority, long long stackSize, long long* threadId) { unsigned int osThreadId = 0; - - *handle = (HANDLE)_beginthreadex( - NULL, (DWORD)0, threadMain, threadArg, 0, &osThreadId ); - - if (*handle == 0) { - throw RuntimeException( - __FILE__, __LINE__, "Failed to create new Thread." ); + uintptr_t result = _beginthreadex(NULL, (DWORD)0, threadMain, threadArg, 0, &osThreadId); + char errorMessage[256]; + + *handle = nullptr; + + if (((uintptr_t )-1L) == result) + { + sprintf_s(errorMessage, sizeof(errorMessage), "Failed to create new Thread. Result: -1. Error code: %d. DOS Error code: %d.", errno, _doserrno); + + throw RuntimeException(__FILE__, __LINE__, errorMessage); } - + + *handle = (decaf_thread_t)result; + + if (nullptr == *handle) + { + sprintf_s(errorMessage, sizeof(errorMessage), "Failed to create new Thread. Result: NULL. Error code: %d. DOS Error code: %d.", errno, _doserrno); + + throw RuntimeException(__FILE__, __LINE__, errorMessage); + } + *threadId = (long long)osThreadId; }