From 0f85baabe18f97b53e7addcdbffe2f3e635dfd63 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 15 May 2019 13:43:53 +0300 Subject: [PATCH] MXS-2491 Remove global destructor inter-dependency log.cc:this_unit.slogger depends upon logger.cc:this_unit.ident. Depending on which one is destructed first, there will be a crash or then not. It seems that the destruction order when returning from main() is not necessarily the same as that when calling exit() in main, as this bug is triggered with compilers versions and not with other. --- maxutils/maxbase/src/logger.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/maxutils/maxbase/src/logger.cc b/maxutils/maxbase/src/logger.cc index f76bc8154..8dd519257 100644 --- a/maxutils/maxbase/src/logger.cc +++ b/maxutils/maxbase/src/logger.cc @@ -70,17 +70,20 @@ bool should_log_error() struct this_unit { - std::string ident; + static const int MAX_IDENT_LEN = 256; + + // Don't change to std::string. Order of destruction issue with logger.cc:this_unit. + char ident[MAX_IDENT_LEN + 1]; } this_unit; std::string get_ident() { - if (this_unit.ident.empty()) + if (!this_unit.ident[0]) { #ifdef __GNUC__ - this_unit.ident = program_invocation_short_name; + return program_invocation_short_name; #else - this_unit.ident = "The Program"; + return "The Program"; #endif } @@ -98,7 +101,15 @@ namespace maxbase // static void Logger::set_ident(const std::string& ident) { - this_unit.ident = ident; + int len = ident.length(); + + if (len > this_unit.MAX_IDENT_LEN) + { + len = this_unit.MAX_IDENT_LEN; + } + + this_unit.ident[len] = 0; + memcpy(this_unit.ident, ident.c_str(), len); } std::unique_ptr FileLogger::create(const std::string& filename)