From f227bbdf47401eab919593d96bef72af52bfd11f Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:13:39 -0700 Subject: [PATCH 01/20] Squash uninitialized member warnings --- loguru.cpp | 2 +- loguru.hpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index dd7ee08..0ffe92f 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1629,7 +1629,7 @@ namespace loguru struct StringStream { - std::string str; + std::string str{}; }; // Use this in your EcPrinter implementations. diff --git a/loguru.hpp b/loguru.hpp index bfdce06..522f9e5 100644 --- a/loguru.hpp +++ b/loguru.hpp @@ -677,8 +677,8 @@ namespace loguru Verbosity _verbosity; const char* _file; // Set to null if we are disabled due to verbosity unsigned _line; - bool _indent_stderr; // Did we? - long long _start_time_ns; + bool _indent_stderr = false; // Did we? + long long _start_time_ns = 0; char _name[LOGURU_SCOPE_TEXT_SIZE]; }; @@ -834,7 +834,7 @@ namespace loguru const char* _file; unsigned _line; const char* _descr; - EcEntryBase* _previous; + EcEntryBase* _previous = nullptr; }; template From 734938881fedbb0dba5b3df0115024e95d53bbbf Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:29:03 -0700 Subject: [PATCH 02/20] Always initialize LogScopeRAII members Fixes PVS Studio issue: V730 Not all members of a class are initialized inside the constructor. Consider inspecting: _verbosity, _line, _name. --- loguru.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/loguru.hpp b/loguru.hpp index 522f9e5..0e1418e 100644 --- a/loguru.hpp +++ b/loguru.hpp @@ -674,12 +674,12 @@ namespace loguru LogScopeRAII& operator=(const LogScopeRAII&) = delete; void operator=(LogScopeRAII&&) = delete; - Verbosity _verbosity; - const char* _file; // Set to null if we are disabled due to verbosity - unsigned _line; + Verbosity _verbosity = Verbosity_INVALID; + const char* _file = nullptr; // Set to null if we are disabled due to verbosity + unsigned _line = 0; bool _indent_stderr = false; // Did we? long long _start_time_ns = 0; - char _name[LOGURU_SCOPE_TEXT_SIZE]; + char _name[LOGURU_SCOPE_TEXT_SIZE] = {}; }; // Marked as 'noreturn' for the benefit of the static analyzer and optimizer. From 0537dce75829ecad2b9f96a1c2ac2028b2733876 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:32:42 -0700 Subject: [PATCH 03/20] Check the duplicated file_path before using it Fixes PVS Studio flagged issue: V769 The 'file_path' pointer in the 'file_path + 1' expression could be nullptr. In such case, resulting value will be senseless and it should not be used. --- loguru.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/loguru.cpp b/loguru.cpp index 0ffe92f..2708b80 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -747,6 +747,7 @@ namespace loguru { CHECK_F(file_path_const && *file_path_const); char* file_path = STRDUP(file_path_const); + CHECK_F(file_path != nullptr, "Failed to allocate memory"); for (char* p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) { *p = '\0'; From fa3b667c85afcd5dae7d530a32708292692ef94c Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:37:19 -0700 Subject: [PATCH 04/20] Drop always-false test of info.dli_sname in stack trace The check to see if info.dli_sname is 0 will always be false because of the prior if-statement requiring it to be non-zero: "if (dladdr(callstack[i], &info) && info.dli_sname)" ... Fixes PVS Studio flagged issue: V547 Expression 'info.dli_sname == 0' is always false. --- loguru.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index 2708b80..eb8748e 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1196,8 +1196,7 @@ namespace loguru } snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n", i - skip, int(2 + sizeof(void*) * 2), callstack[i], - status == 0 ? demangled : - info.dli_sname == 0 ? symbols[i] : info.dli_sname, + status == 0 ? demangled : info.dli_sname, static_cast(callstack[i]) - static_cast(info.dli_saddr)); free(demangled); } else { From 79a64440834ef282a1475be079bcfca7bd128c9a Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:39:34 -0700 Subject: [PATCH 05/20] Use string's default constructor instead of copy-constructor ("") A very minor issue flagged by PVS Studio: V815 Decreased performance. Consider replacing the expression 'std::string("")' with 'std::string()'. --- loguru.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index eb8748e..0415358 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1163,7 +1163,7 @@ namespace loguru try { std::regex std_allocator_re(R"(,\s*std::allocator<[^<>]+>)"); - output = std::regex_replace(output, std_allocator_re, std::string("")); + output = std::regex_replace(output, std_allocator_re, std::string()); std::regex template_spaces_re(R"(<\s*([^<> ]+)\s*>)"); output = std::regex_replace(output, template_spaces_re, std::string("<$1>")); From b0344fdaf5037514dff3c077a12e2e14aacb3928 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:42:45 -0700 Subject: [PATCH 06/20] Drop always-positive logic in print_preamble_header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pos < out_buff_size is always true, because the prior if-condition: if (out_buff_size == 0) { return; } already guarantees that out_buff_size (an unsigned type) is already non-zero, and long pos = 0, therefore out_buff_size will always be greater than 0. Fixes two PVS Studio issues: V560 A part of conditional expression is always true: pos < out_buff_size. ↑ V560 A part of conditional expression is always true: pos < out_buff_size. --- loguru.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index 0415358..4e5c3e7 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1245,7 +1245,7 @@ namespace loguru if (out_buff_size == 0) { return; } out_buff[0] = '\0'; long pos = 0; - if (g_preamble_date && pos < out_buff_size) { + if (g_preamble_date) { pos += snprintf(out_buff + pos, out_buff_size - pos, "date "); } if (g_preamble_time && pos < out_buff_size) { @@ -1298,7 +1298,7 @@ namespace loguru long pos = 0; - if (g_preamble_date && pos < out_buff_size) { + if (g_preamble_date) { pos += snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ", 1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday); } From 1c8c0b77d292284e4f9b2f14273e49d3b3e8322b Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:46:01 -0700 Subject: [PATCH 07/20] Check the with_newline pointer after allocating Fixes a PVS Studio issue: V522 There might be dereferencing of a potential null pointer 'with_newline'. Check lines: 1810, 1809. --- loguru.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/loguru.cpp b/loguru.cpp index 4e5c3e7..49a13fe 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1807,6 +1807,7 @@ namespace loguru Text parent_ec = get_error_context_for(ec_handle); size_t buffer_size = strlen(parent_ec.c_str()) + 2; char* with_newline = reinterpret_cast(malloc(buffer_size)); + CHECK_F(with_newline != nullptr, "Failed to allocate memory for error context."); with_newline[0] = '\n'; #ifdef _WIN32 strncpy_s(with_newline + 1, buffer_size, parent_ec.c_str(), buffer_size - 2); From c20b1d8d6e142af8c686fa480dae835e1b97342e Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 22:51:39 -0700 Subject: [PATCH 08/20] Use simpler string-clear syntax Fixes two PVS Studio minor issues: V815 Decreased performance. Consider replacing the expression 's_arguments = ""' with 's_arguments.clear()'. V805 Decreased performance. It is inefficient to identify an empty string by using 'strlen(str) != 0' construct. A more efficient way is to check: str[0] != '\0'. --- loguru.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index 49a13fe..8a917db 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -599,7 +599,7 @@ namespace loguru LOG_F(WARNING, "Failed to get current working directory: " LOGURU_FMT(s) "", error_text.c_str()); } - s_arguments = ""; + s_arguments.clear(); for (int i = 0; i < argc; ++i) { escape(s_arguments, argv[i]); if (i + 1 < argc) { @@ -645,7 +645,7 @@ namespace loguru fflush(stderr); } VLOG_F(g_internal_verbosity, "arguments: " LOGURU_FMT(s) "", s_arguments.c_str()); - if (strlen(s_current_dir) != 0) + if (s_current_dir[0] != '\0') { VLOG_F(g_internal_verbosity, "Current dir: " LOGURU_FMT(s) "", s_current_dir); } @@ -817,7 +817,7 @@ namespace loguru if (!s_arguments.empty()) { fprintf(file, "arguments: %s\n", s_arguments.c_str()); } - if (strlen(s_current_dir) != 0) { + if (s_current_dir[0] != '\0') { fprintf(file, "Current dir: %s\n", s_current_dir); } fprintf(file, "File verbosity level: %d\n", verbosity); From 349786916e4a80b4c6079954e95bdbabb3b525a1 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 22:52:53 -0700 Subject: [PATCH 09/20] Created stack_cleanup object in-place in container Fixes a PVS Studio issues: V823 Decreased performance. Object may be created in-place in the 's_user_stack_cleanups' container. Consider replacing methods: 'push_back' -> 'emplace_back'. --- loguru.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 8a917db..6ea1f77 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -898,7 +898,7 @@ namespace loguru return; } - s_user_stack_cleanups.push_back(StringPair(find_this, replace_with_this)); + s_user_stack_cleanups.emplace_back(StringPair(find_this, replace_with_this)); } static void on_callback_change() From 2fc5dda8722c86136dd15e1a87e724ce4a0c48f0 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 23:02:38 -0700 Subject: [PATCH 10/20] Avoid incrementing the position to clarify it's unused Fixes two issues flagged by Clang's static analyzer. --- loguru.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index 6ea1f77..35021fc 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1264,7 +1264,7 @@ namespace loguru pos += snprintf(out_buff + pos, out_buff_size - pos, " v"); } if (g_preamble_pipe && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "| "); + /* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| "); } } @@ -1325,7 +1325,7 @@ namespace loguru level_buff); } if (g_preamble_pipe && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "| "); + /* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| "); } } From abd794b7e7ead9abd9224dfe47d32305c839afb5 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Tue, 24 Aug 2021 12:29:31 -0700 Subject: [PATCH 11/20] Virtualize EcEntryBase's destructor so it's called by derived classes Fixes an error flagged by LGTM: Derived classes from a base class without a virtual destructor will only call the destructor of the type of the pointer being deleted and not the base-class. This can cause a defect if the pointer type is a base type while the object instance is a derived type. --- loguru.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.hpp b/loguru.hpp index 0e1418e..743d5de 100644 --- a/loguru.hpp +++ b/loguru.hpp @@ -820,7 +820,7 @@ namespace loguru { public: EcEntryBase(const char* file, unsigned line, const char* descr); - ~EcEntryBase(); + virtual ~EcEntryBase(); EcEntryBase(const EcEntryBase&) = delete; EcEntryBase(EcEntryBase&&) = delete; EcEntryBase& operator=(const EcEntryBase&) = delete; From 927e642479c6aca80fc394f10c3fc527e8247e69 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Tue, 24 Aug 2021 12:41:07 -0700 Subject: [PATCH 12/20] Explicitly ignore sigaction's return value --- loguru.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 35021fc..87141a4 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1858,7 +1858,10 @@ namespace loguru memset(&sig_action, 0, sizeof(sig_action)); sigemptyset(&sig_action.sa_mask); sig_action.sa_handler = SIG_DFL; - sigaction(signal_number, &sig_action, NULL); + + // Note: Explicitly ignore sigaction's return value. + // It's only used when setting up the signal handlers. + (void) sigaction(signal_number, &sig_action, NULL); kill(getpid(), signal_number); } From aca302dea0a8fb7da9002e7118ea59ba352daa7a Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Thu, 16 Sep 2021 07:03:01 -0700 Subject: [PATCH 13/20] Handle unsigned versus signed char comparisons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes these warnings: ../../submodules/loguru/loguru.cpp:559:15: warning: comparison is always true due to limited range of data type [-Wtype-limits] else if (0 <= c && c < 0x20) { // ASCI control character: ~~^~~~ ../../submodules/loguru/loguru.cpp: In function ‘loguru::Text loguru::ec_to_text(char)’: ../../submodules/loguru/loguru.cpp:1776:14: warning: comparison is always true due to limited range of data type [-Wtype-limits] else if (0 <= c && c < 0x20) { ~~^~~~ --- loguru.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 87141a4..d966267 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -556,7 +557,11 @@ namespace loguru else if (c == '\'') { out += "\\\'"; } else if (c == '\"') { out += "\\\""; } else if (c == ' ') { out += "\\ "; } - else if (0 <= c && c < 0x20) { // ASCI control character: +#if (CHAR_MIN < 0) // char is signed + else if (0 <= c && c < 0x20) { // ASCI control character +#else // char is unsigned + else if (c < 0x20) { // ASCI control character +#endif // else if (c < 0x20 || c != (c & 127)) { // ASCII control character or UTF-8: out += "\\x"; write_hex_byte(out, static_cast(c)); @@ -1773,7 +1778,11 @@ namespace loguru else if (c == '\n') { str += "\\n"; } else if (c == '\r') { str += "\\r"; } else if (c == '\t') { str += "\\t"; } +#if (CHAR_MIN < 0) // char is signed else if (0 <= c && c < 0x20) { +#else // char is unsigned + else if (c < 0x20) { +#endif str += "\\u"; write_hex_16(static_cast(c)); } else { str += c; } From 86bbfd922013fec5422c28a2508a7a19237d9e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20P=2E=20Tjern=C3=B8?= Date: Sun, 29 Aug 2021 06:08:35 -0700 Subject: [PATCH 14/20] Fix clang on windows warnings (#180) * Fix warning suppression on Windows clang When building loguru under Windows clang, _MSC_VER is defined, but it uses clang-style warning suppression flags. Fix that by checking for __GNUC__ and __clang__ first, then checking for _MSC_VER. Also fix the lack of a `#pragma GCC diagnostic pop` for people who include loguru.cpp. * Fix unsigned/signed comparison `snprintf` can return a negative value to signify that the buffer is not large enough to contain the bytes you attempted to write, so check for a positive value before advancing `pos` -- otherwise a later `snprintf` might overwrite bytes we've already written to the buffer. --- loguru.cpp | 102 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 34 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index d966267..f69dd61 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1,4 +1,4 @@ -#ifndef _WIN32 +#if defined(__GNUC__) || defined(__clang__) // Disable all warnings from gcc/clang: #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" @@ -11,16 +11,13 @@ #pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" #pragma GCC diagnostic ignored "-Wmissing-prototypes" #pragma GCC diagnostic ignored "-Wpadded" -#pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wunknown-pragmas" #pragma GCC diagnostic ignored "-Wunused-macros" #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" -#else -#ifdef _MSC_VER +#elif defined(_MSC_VER) #pragma warning(push) -#pragma warning(disable:4018) -#endif // _MSC_VER +#pragma warning(disable:4365) // conversion from 'X' to 'Y', signed/unsigned mismatch #endif #include "loguru.hpp" @@ -1249,27 +1246,45 @@ namespace loguru { if (out_buff_size == 0) { return; } out_buff[0] = '\0'; - long pos = 0; - if (g_preamble_date) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "date "); + size_t pos = 0; + if (g_preamble_date && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "date "); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_time && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "time "); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "time "); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_uptime && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) "); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) "); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_thread && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id"); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id"); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_file && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file"); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file"); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_verbose && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, " v"); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, " v"); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_pipe && pos < out_buff_size) { - /* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| "); + /* pos += */ (void)snprintf(out_buff + pos, out_buff_size - pos, "| "); } } @@ -1301,36 +1316,54 @@ namespace loguru snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity); } - long pos = 0; + size_t pos = 0; - if (g_preamble_date) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ", - 1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday); + if (g_preamble_date && pos < out_buff_size) { + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ", + 1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_time && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ", - time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ", + time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_uptime && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ", - uptime_sec); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ", + uptime_sec); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_thread && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", - LOGURU_THREADNAME_WIDTH, thread_name); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", + LOGURU_THREADNAME_WIDTH, thread_name); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_file && pos < out_buff_size) { char shortened_filename[LOGURU_FILENAME_WIDTH + 1]; snprintf(shortened_filename, LOGURU_FILENAME_WIDTH + 1, "%s", file); - pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ", - LOGURU_FILENAME_WIDTH, shortened_filename, line); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ", + LOGURU_FILENAME_WIDTH, shortened_filename, line); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_verbose && pos < out_buff_size) { - pos += snprintf(out_buff + pos, out_buff_size - pos, "%4s", - level_buff); + int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%4s", + level_buff); + if (bytes > 0) { + pos += bytes; + } } if (g_preamble_pipe && pos < out_buff_size) { - /* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| "); + /* pos += */ (void)snprintf(out_buff + pos, out_buff_size - pos, "| "); } } @@ -1969,10 +2002,11 @@ namespace loguru #endif // _WIN32 -#ifdef _WIN32 -#ifdef _MSC_VER + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#elif defined(_MSC_VER) #pragma warning(pop) -#endif // _MSC_VER -#endif // _WIN32 +#endif #endif // LOGURU_IMPLEMENTATION From 8b31eecf1b6dce2f52bb478d100b97372b85d1b0 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Sun, 22 Aug 2021 23:02:38 -0700 Subject: [PATCH 15/20] Avoid incrementing the position to clarify it's unused Fixes two issues flagged by Clang's static analyzer. --- loguru.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index f69dd61..8ded665 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1284,7 +1284,14 @@ namespace loguru } } if (g_preamble_pipe && pos < out_buff_size) { - /* pos += */ (void)snprintf(out_buff + pos, out_buff_size - pos, "| "); + (void)snprintf(out_buff + pos, out_buff_size - pos, "| "); + + // Because this is the last if-statement, we avoid incrementing the + // position to clarify it's unused. If more cases are added, then: + // int bytes = snprintf(...) + // if (bytes > 0) { + // pos += bytes; + // } } } @@ -1363,7 +1370,13 @@ namespace loguru } } if (g_preamble_pipe && pos < out_buff_size) { - /* pos += */ (void)snprintf(out_buff + pos, out_buff_size - pos, "| "); + (void)snprintf(out_buff + pos, out_buff_size - pos, "| "); + + // Avoid incrementing the position to clarify it's unused + // int bytes = snprintf(...) + // if (bytes > 0) { + // pos += bytes; + // } } } From 0155fe9552c470754e1c1919d3cf7f7df90a21ff Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Thu, 16 Sep 2021 08:34:02 -0700 Subject: [PATCH 16/20] Assert two always-true conditions --- loguru.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index 8ded665..c629b89 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1247,7 +1247,8 @@ namespace loguru if (out_buff_size == 0) { return; } out_buff[0] = '\0'; size_t pos = 0; - if (g_preamble_date && pos < out_buff_size) { + assert(pos < out_buff_size); + if (g_preamble_date) { int bytes = snprintf(out_buff + pos, out_buff_size - pos, "date "); if (bytes > 0) { pos += bytes; @@ -1324,8 +1325,8 @@ namespace loguru } size_t pos = 0; - - if (g_preamble_date && pos < out_buff_size) { + assert(pos < out_buff_size); + if (g_preamble_date) { int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ", 1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday); if (bytes > 0) { From a9f232d9c5efa4e73ce0f270e18f492f3043f211 Mon Sep 17 00:00:00 2001 From: kcgen <1557255+kcgen@users.noreply.github.com> Date: Thu, 16 Sep 2021 09:25:33 -0700 Subject: [PATCH 17/20] Include assert header --- loguru.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/loguru.cpp b/loguru.cpp index 23651ca..9032245 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include #include From 29992d9966375bb0d2fcb566fcec5b73ce448074 Mon Sep 17 00:00:00 2001 From: kcgen Date: Thu, 21 Oct 2021 07:30:53 -0700 Subject: [PATCH 18/20] Limit verbosity number when populating the warning level string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes CI failure: /usr/include/x86_64-linux-gnu/bits/stdio2.h:64:35: note: ‘__builtin___snprintf_chk’ output between 5 and 11 bytes into a destination of size 5 64 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 65 | __bos (__s), __fmt, __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1plus: all warnings being treated as errors CMakeFiles/loguru_test.dir/build.make:75: recipe for target 'CMakeFiles/loguru_test.dir/loguru_test.cpp.o' failed make[2]: *** [CMakeFiles/loguru_test.dir/loguru_test.cpp.o] Error 1 CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/loguru_test.dir/all' failed make[1]: *** [CMakeFiles/loguru_test.dir/all] Error 2 Makefile:100: recipe for target 'all' failed make: *** [all] Error 2 CMake Error at test/appveyor.cmake:22 (_message): --- loguru.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index 9032245..5ad6007 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1312,12 +1312,12 @@ namespace loguru file = filename(file); } - char level_buff[6]; + char level_buff[7]; const char* custom_level_name = get_verbosity_name(verbosity); 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", static_cast(verbosity) % 9999); } size_t pos = 0; From 52f2fcd7913148b9b313d116049b0e2dd6307d70 Mon Sep 17 00:00:00 2001 From: kcgen Date: Sun, 12 Dec 2021 10:47:45 -0800 Subject: [PATCH 19/20] Check for NOMINMAX before defining it MSYS2's own headers define NOMINMAX in: /msys64/mingw64/include/c++/11.2.0/x86_64-w64-mingw32/bits/os_defines.h This causes a redefinition warning when building Loguru: ../src/libs/loguru/loguru.cpp:131: warning: "NOMINMAX" redefined 131 | #define NOMINMAX | In file included from D:/a/_temp/msys64/mingw64/include/c++/11.2.0/x86_64-w64-mingw32/bits/c++config.h:586, from D:/a/_temp/msys64/mingw64/include/c++/11.2.0/utility:68, from D:/a/_temp/msys64/mingw64/include/c++/11.2.0/algorithm:60, from ../src/libs/loguru/loguru.cpp:33: 45 | #define NOMINMAX 1 --- loguru.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 5ad6007..e4f0f9f 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -128,7 +128,9 @@ #define _WIN32_WINNT 0x0502 #endif #define WIN32_LEAN_AND_MEAN - #define NOMINMAX + #ifndef NOMINMAX + #define NOMINMAX + #endif #include #endif From 74efd68751d494a5d1e0f65a3a1416c05a9ea617 Mon Sep 17 00:00:00 2001 From: kcgen Date: Sun, 12 Dec 2021 10:57:08 -0800 Subject: [PATCH 20/20] Drop unecessary declarations on the thread_name buffer --- loguru.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index e4f0f9f..30072a0 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1029,7 +1029,7 @@ namespace loguru // Where we store the custom thread name set by `set_thread_name` char* thread_name_buffer() { - __declspec( thread ) static char thread_name[LOGURU_THREADNAME_WIDTH + 1] = {0}; + static char thread_name[LOGURU_THREADNAME_WIDTH + 1] = {0}; return &thread_name[0]; } #endif // LOGURU_WINTHREADS