From 7260347fd1af7d6f631bd4263368c8fd2a3bbbf2 Mon Sep 17 00:00:00 2001 From: sdong Date: Wed, 24 Jul 2019 15:11:36 -0700 Subject: [PATCH] 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 --- logging/auto_roll_logger.cc | 14 +++++++++++++- logging/auto_roll_logger.h | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/logging/auto_roll_logger.cc b/logging/auto_roll_logger.cc index 223dfbe30..3109f0bc6 100644 --- a/logging/auto_roll_logger.cc +++ b/logging/auto_roll_logger.cc @@ -155,6 +155,11 @@ std::string AutoRollLogger::ValistToString(const char* format, void AutoRollLogger::LogInternal(const char* format, ...) { mutex_.AssertHeld(); + + if (!logger_) { + return; + } + va_list args; va_start(args, format); logger_->Logv(format, args); @@ -163,7 +168,10 @@ void AutoRollLogger::LogInternal(const char* format, ...) { void AutoRollLogger::Logv(const char* format, va_list ap) { assert(GetStatus().ok()); - + if (!logger_) { + return; + } + std::shared_ptr logger; { MutexLock l(&mutex_); @@ -207,6 +215,10 @@ void AutoRollLogger::WriteHeaderInfo() { } 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 // assumptions about the data contained in va_list, we will retain them as // strings diff --git a/logging/auto_roll_logger.h b/logging/auto_roll_logger.h index a14fbfd58..45cbc2697 100644 --- a/logging/auto_roll_logger.h +++ b/logging/auto_roll_logger.h @@ -41,6 +41,10 @@ class AutoRollLogger : public Logger { } size_t GetLogFileSize() const override { + if (!logger_) { + return 0; + } + std::shared_ptr logger; { MutexLock l(&mutex_);