Auto Roll Logger to add some extra checking to avoid segfault. (#5623)

Summary:
AutoRollLogger sets GetStatus() to be non-OK if the log file fails to be created and logger_ is set to null. It is left to the caller to check the status before calling function to this class. There is no harm to create another null checking to logger_ before we using it, so that in case users mis-use the logger, they don't get a segfault.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5623

Test Plan: Run all existing tests.

Differential Revision: D16466251

fbshipit-source-id: 262b885eec28bf741d91e9191c3cb5ff964e1bce
This commit is contained in:
sdong 2019-07-24 15:11:36 -07:00 committed by Facebook Github Bot
parent 5daa426a18
commit 7260347fd1
2 changed files with 17 additions and 1 deletions

View File

@ -155,6 +155,11 @@ std::string AutoRollLogger::ValistToString(const char* format,
void AutoRollLogger::LogInternal(const char* format, ...) { void AutoRollLogger::LogInternal(const char* format, ...) {
mutex_.AssertHeld(); mutex_.AssertHeld();
if (!logger_) {
return;
}
va_list args; va_list args;
va_start(args, format); va_start(args, format);
logger_->Logv(format, args); logger_->Logv(format, args);
@ -163,6 +168,9 @@ void AutoRollLogger::LogInternal(const char* format, ...) {
void AutoRollLogger::Logv(const char* format, va_list ap) { void AutoRollLogger::Logv(const char* format, va_list ap) {
assert(GetStatus().ok()); assert(GetStatus().ok());
if (!logger_) {
return;
}
std::shared_ptr<Logger> logger; std::shared_ptr<Logger> logger;
{ {
@ -207,6 +215,10 @@ void AutoRollLogger::WriteHeaderInfo() {
} }
void AutoRollLogger::LogHeader(const char* format, va_list args) { void AutoRollLogger::LogHeader(const char* format, va_list args) {
if (!logger_) {
return;
}
// header message are to be retained in memory. Since we cannot make any // header message are to be retained in memory. Since we cannot make any
// assumptions about the data contained in va_list, we will retain them as // assumptions about the data contained in va_list, we will retain them as
// strings // strings

View File

@ -41,6 +41,10 @@ class AutoRollLogger : public Logger {
} }
size_t GetLogFileSize() const override { size_t GetLogFileSize() const override {
if (!logger_) {
return 0;
}
std::shared_ptr<Logger> logger; std::shared_ptr<Logger> logger;
{ {
MutexLock l(&mutex_); MutexLock l(&mutex_);