From e3f98b554a1beae996ac456f2b7d86a842f364b7 Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Tue, 10 Nov 2015 09:27:15 +0100 Subject: [PATCH] GH #1022: clean-up setThreadName --- Foundation/src/Thread_POSIX.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Foundation/src/Thread_POSIX.cpp b/Foundation/src/Thread_POSIX.cpp index a3eac3b36..f3ca5f8d7 100644 --- a/Foundation/src/Thread_POSIX.cpp +++ b/Foundation/src/Thread_POSIX.cpp @@ -65,14 +65,12 @@ void setThreadName(pthread_t thread, const std::string& threadName) #if (POCO_OS == POCO_OS_MAC_OS_X) pthread_setname_np(threadName.c_str()); // __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2) #else - if (pthread_setname_np(thread, threadName.c_str())) + if (pthread_setname_np(thread, threadName.c_str()) != 0 && errno == ERANGE && threadName.size() > 15) { - char truncName[16] = {0}; - std::size_t suffixIndex = threadName.length() - 7; - std::memcpy(truncName, &threadName[0], 7); - truncName[7] = '~'; - memcpy(&truncName[8], &threadName[suffixIndex], 7); - pthread_setname_np(thread, truncName); + std::string truncName(threadName, 0, 7); + truncName.append("~"); + truncName.append(threadName, threadName.size() - 7, 7); + pthread_setname_np(thread, truncName.c_str()); } #endif }