Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11646: Non-thread safe shared buffer returned from GetLogPrefix(). #11648

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ static void IConsoleWriteToLogFile(const std::string &string)
{
if (_iconsole_output_file != nullptr) {
/* if there is an console output file ... also print it there */
const char *header = GetLogPrefix();
if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
fwrite(string.c_str(), string.size(), 1, _iconsole_output_file) != 1 ||
fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
try {
fmt::print(_iconsole_output_file, "{}{}\n", GetLogPrefix(), string);
} catch (const std::system_error &) {
fclose(_iconsole_output_file);
_iconsole_output_file = nullptr;
IConsolePrint(CC_ERROR, "Cannot write to console log file; closing the log file.");
Expand Down
14 changes: 6 additions & 8 deletions src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,16 @@ std::string GetDebugString()

/**
* Get the prefix for logs; if show_date_in_logs is enabled it returns
* the date, otherwise it returns nothing.
* @return the prefix for logs (do not free), never nullptr
* the date, otherwise it returns an empty string.
* @return the prefix for logs.
*/
const char *GetLogPrefix()
std::string GetLogPrefix()
{
static std::string _log_prefix;
std::string log_prefix;
if (_settings_client.gui.show_date_in_logs) {
_log_prefix = fmt::format("[{:%Y-%m-%d %H:%M:%S}] ", fmt::localtime(time(nullptr)));
} else {
_log_prefix.clear();
log_prefix = fmt::format("[{:%Y-%m-%d %H:%M:%S}] ", fmt::localtime(time(nullptr)));
}
return _log_prefix.c_str();
return log_prefix;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ std::string GetDebugString();
void ShowInfoI(const std::string &str);
#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))

const char *GetLogPrefix();
std::string GetLogPrefix();

void DebugSendRemoteMessages();
void DebugReconsiderSendRemoteMessages();
Expand Down