From 8a4183640d7bec065fdadeaea64da18fee773034 Mon Sep 17 00:00:00 2001 From: kcgen Date: Fri, 3 Dec 2021 18:20:50 -0800 Subject: [PATCH 1/6] Use cross-platform string operations in vtextprintf --- loguru.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index a0f1241..ec455b0 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -418,18 +418,27 @@ namespace loguru LOGURU_PRINTF_LIKE(1, 0) static Text vtextprintf(const char* format, va_list vlist) { -#ifdef _WIN32 - int bytes_needed = _vscprintf(format, vlist); + // Determine the required length of the string + int bytes_needed = 0; + + // Calling vsnprintf is assumed to 'consume' the vlist, therefore we need a copy + // of the args to avoid corrupting the stack in the second call to vsnprintf. + va_list argcopy; + va_copy(argcopy, vlist); + bytes_needed = vsnprintf(NULL, 0, format, argcopy); + va_end(argcopy); CHECK_F(bytes_needed >= 0, "Bad string format: '%s'", format); - char* buff = (char*)malloc(bytes_needed+1); - vsnprintf(buff, bytes_needed+1, format, vlist); - return Text(buff); -#else - char* buff = nullptr; - int result = vasprintf(&buff, format, vlist); - CHECK_F(result >= 0, "Bad string format: '" LOGURU_FMT(s) "'", format); + + // Allocate the string's buffer + ++bytes_needed; // Add space for null terminator + auto buff = static_cast(malloc(bytes_needed)); + CHECK_F(buff != nullptr, "Out of memory"); + + // Construct the string and check the result + const auto written = vsnprintf(buff, bytes_needed, format, vlist); + CHECK_F(written <= bytes_needed, "Bad string format: '%s'", format); + return Text(buff); -#endif } Text textprintf(const char* format, ...) From bbd4ec0a0187b98438d29baea23a0472a1ca6817 Mon Sep 17 00:00:00 2001 From: kcgen Date: Fri, 3 Dec 2021 18:21:25 -0800 Subject: [PATCH 2/6] Fix an unused parameter warning --- loguru.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/loguru.cpp b/loguru.cpp index ec455b0..f06dca8 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -585,6 +585,7 @@ namespace loguru return Text(STRDUP(buff)); #else // Not thread-safe. + (void)buff; // unused parameter return Text(STRDUP(strerror(errno))); #endif } From 90161281e13ba8963761fa7e7aa3a070dc53a149 Mon Sep 17 00:00:00 2001 From: kcgen Date: Fri, 3 Dec 2021 18:25:28 -0800 Subject: [PATCH 3/6] Exclude unknown platforms from thread naming --- loguru.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index f06dca8..13f6454 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -634,6 +634,9 @@ namespace loguru pthread_set_name_np(this_thread, main_thread_name); #elif defined(__linux__) || defined(__sun) pthread_setname_np(this_thread, main_thread_name); + #else + // platforms that we don't know how to set the name on + (void)this_thread; // unused #endif } #endif // LOGURU_PTHREADS @@ -1049,6 +1052,9 @@ namespace loguru pthread_set_name_np(pthread_self(), name); #elif defined(__linux__) || defined(__sun) pthread_setname_np(pthread_self(), name); + #else + // Platforms that may not support setting a thread name + (void)name; // unused #endif #elif LOGURU_WINTHREADS // Store thread name in a thread-local storage: @@ -1076,7 +1082,12 @@ namespace loguru // Ask the OS about the thread name. // This is what we *want* to do on all platforms, but // only some platforms support it (currently). - pthread_getname_np(pthread_self(), buffer, length); + #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__) || defined(__sun) + pthread_getname_np(pthread_self(), buffer, length); + #else + // Other platforms that don't support thread names + buffer[0] = 0; + #endif #elif LOGURU_WINTHREADS snprintf(buffer, static_cast(length), "%s", thread_name_buffer()); #else From 8a27e5047f1557ffbac03521fc173785b0eec00c Mon Sep 17 00:00:00 2001 From: kcgen Date: Fri, 3 Dec 2021 18:25:50 -0800 Subject: [PATCH 4/6] Reinterpret the thread pointer to a standard ptr integer type Fixes an invalid conversion error on 32-bit platforms that don't support assigning pointers directly to integers. --- loguru.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 13f6454..f2acc6f 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1108,7 +1108,7 @@ namespace loguru long thread_id; (void)thr_self(&thread_id); #elif LOGURU_PTHREADS - uint64_t thread_id = pthread_self(); + const auto thread_id = reinterpret_cast(pthread_self()); #else // This ID does not correllate to anything we can get from the OS, // so this is the worst way to get the ID. From f00e3ca5623a4dc303752c1c232b425382ea9144 Mon Sep 17 00:00:00 2001 From: Nikola Zivkovic Date: Fri, 3 Dec 2021 15:51:55 +0100 Subject: [PATCH 5/6] Fix a compilation warning - directive output may be truncated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a simple solution to a compilation warning that doesn't require any special changes to be made to already existing enums, types or logic. loguru/loguru.cpp: In function ‘void loguru::print_preamble(char*, size_t, loguru::Verbosity, const char*, unsigned int)’: loguru/loguru.cpp:1208:50: warning: ‘% 4d’ directive output may be truncated writing between 4 and 11 bytes into a region of size 5 [-Wformat-truncation=] 1208 | snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity); | ^~~~ loguru/loguru.cpp:1208:49: note: directive argument in the range [1, 2147483647] 1208 | snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity); | ^~~~~~ In file included from /usr/include/stdio.h:867, from /usr/include/c++/9/cstdio:42, from /usr/include/c++/9/ext/string_conversions.h:43, from /usr/include/c++/9/bits/basic_string.h:6493, from /usr/include/c++/9/string:55, from /usr/include/c++/9/stdexcept:39, from /usr/include/c++/9/array:39, from /usr/include/c++/9/tuple:39, from /usr/include/c++/9/functional:54, from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13, from /usr/include/c++/9/algorithm:71, from loguru/loguru.cpp:36: /usr/include/x86_64-linux-gnu/bits/stdio2.h:67:35: note: ‘__builtin___snprintf_chk’ output between 5 and 12 bytes into a destination of size 5 67 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 68 | __bos (__s), __fmt, __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- loguru.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index f2acc6f..c86b54e 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1332,7 +1332,7 @@ namespace loguru if (custom_level_name) { snprintf(level_buff, sizeof(level_buff) - 1, "%s", custom_level_name); } else { - snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity); + snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", (int8_t)verbosity); } size_t pos = 0; From e486de857a0e0309608e66a6af490ebaa12ac02e Mon Sep 17 00:00:00 2001 From: kcgen Date: Sun, 26 Dec 2021 08:06:29 -0800 Subject: [PATCH 6/6] Handle platform-specific pthread_self() return types --- loguru.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index c86b54e..cf52fb5 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -102,6 +102,9 @@ #endif // LOGURU_STACKTRACES #if LOGURU_PTHREADS + // for std::is_pointer<> to differentiate pthread_self() type variations + #include + #include #if defined(__FreeBSD__) #include @@ -1108,7 +1111,15 @@ namespace loguru long thread_id; (void)thr_self(&thread_id); #elif LOGURU_PTHREADS - const auto thread_id = reinterpret_cast(pthread_self()); + const auto native_id = pthread_self(); + // Warning, even within POSIX, return types and sizes vary: + // - Haku GCC returns a pthread_t* + // - ARM32 GCC returns an unsigned long long int + // So we bake the variations down to a common integer: + const auto pthread_self_is_pointer = std::is_pointer::value; + const auto thread_id = pthread_self_is_pointer + ? reinterpret_cast((void*)native_id) + : static_cast(native_id); #else // This ID does not correllate to anything we can get from the OS, // so this is the worst way to get the ID.