Skip to content

Commit b62b437

Browse files
committed
Merge #13148: logging: Fix potential use-after-free in LogPrintStr(...)
0bd4cd3 logging: remove unused return value from LogPrintStr (practicalswift) 76f344d logging: Fix potential use-after-free in LogPrintStr(...) (practicalswift) Pull request description: Fix potential use-after-free in `LogPrintStr(...)`. `freopen(…)` frees `m_fileout`. Tree-SHA512: ceee1f659c10a21525aa648377afeea0a37016339f5269dea54850ba3b475aa316f4931081655717b65f981598fdc9d79a1e79e55f7084c242eeb7bf372bc4b6
2 parents 11adab3 + 0bd4cd3 commit b62b437

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/logging.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,38 +198,37 @@ std::string BCLog::Logger::LogTimestampStr(const std::string &str)
198198
return strStamped;
199199
}
200200

201-
int BCLog::Logger::LogPrintStr(const std::string &str)
201+
void BCLog::Logger::LogPrintStr(const std::string &str)
202202
{
203-
int ret = 0; // Returns total number of characters written
204-
205203
std::string strTimestamped = LogTimestampStr(str);
206204

207205
if (m_print_to_console) {
208206
// print to console
209-
ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
207+
fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
210208
fflush(stdout);
211209
}
212210
if (m_print_to_file) {
213211
std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
214212

215213
// buffer if we haven't opened the log yet
216214
if (m_fileout == nullptr) {
217-
ret = strTimestamped.length();
218215
m_msgs_before_open.push_back(strTimestamped);
219216
}
220217
else
221218
{
222219
// reopen the log file, if requested
223220
if (m_reopen_file) {
224221
m_reopen_file = false;
225-
if (fsbridge::freopen(m_file_path,"a",m_fileout) != nullptr)
226-
setbuf(m_fileout, nullptr); // unbuffered
222+
m_fileout = fsbridge::freopen(m_file_path, "a", m_fileout);
223+
if (!m_fileout) {
224+
return;
225+
}
226+
setbuf(m_fileout, nullptr); // unbuffered
227227
}
228228

229-
ret = FileWriteStr(strTimestamped, m_fileout);
229+
FileWriteStr(strTimestamped, m_fileout);
230230
}
231231
}
232-
return ret;
233232
}
234233

235234
void BCLog::Logger::ShrinkDebugFile()

src/logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace BCLog {
8686
std::atomic<bool> m_reopen_file{false};
8787

8888
/** Send a string to the log output */
89-
int LogPrintStr(const std::string &str);
89+
void LogPrintStr(const std::string &str);
9090

9191
/** Returns whether logs will be written to any output */
9292
bool Enabled() const { return m_print_to_console || m_print_to_file; }

0 commit comments

Comments
 (0)