summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorDaniel Erat <derat@google.com>2015-09-30 14:25:50 -0600
committerDaniel Erat <derat@google.com>2015-09-30 14:32:37 -0600
commitb8c214021901c7f18bf76f1fdb4cec6e8cf8db49 (patch)
tree4a54cf3284274cf2d59422eab73520987d409769 /base
parent1735db4f0706ec871937968301e26474db940856 (diff)
downloadlibchrome-b8c214021901c7f18bf76f1fdb4cec6e8cf8db49.tar.gz
Don't crash when logging if CommandLine is uninitialized.
The base::CommandLine singleton is usually initialized from main(), but some gtest-based unit tests don't have control over that function. Guard CommandLine accesses in base/logging.cc to handle this gracefully. Bug: 24536569 Change-Id: If3eab0f36c28bb6b3cf3569c61c7d7cec5cf7b10
Diffstat (limited to 'base')
-rw-r--r--base/logging.cc38
1 files changed, 20 insertions, 18 deletions
diff --git a/base/logging.cc b/base/logging.cc
index 98d149d6e7..1c86779526 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -359,21 +359,23 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
// Can log only to the system debug log.
CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
#endif
- base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
- // Don't bother initializing |g_vlog_info| unless we use one of the
- // vlog switches.
- if (command_line->HasSwitch(switches::kV) ||
- command_line->HasSwitch(switches::kVModule)) {
- // NOTE: If |g_vlog_info| has already been initialized, it might be in use
- // by another thread. Don't delete the old VLogInfo, just create a second
- // one. We keep track of both to avoid memory leak warnings.
- CHECK(!g_vlog_info_prev);
- g_vlog_info_prev = g_vlog_info;
-
- g_vlog_info =
- new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
- command_line->GetSwitchValueASCII(switches::kVModule),
- &g_min_log_level);
+ if (base::CommandLine::InitializedForCurrentProcess()) {
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
+ // Don't bother initializing |g_vlog_info| unless we use one of the
+ // vlog switches.
+ if (command_line->HasSwitch(switches::kV) ||
+ command_line->HasSwitch(switches::kVModule)) {
+ // NOTE: If |g_vlog_info| has already been initialized, it might be in use
+ // by another thread. Don't delete the old VLogInfo, just create a second
+ // one. We keep track of both to avoid memory leak warnings.
+ CHECK(!g_vlog_info_prev);
+ g_vlog_info_prev = g_vlog_info;
+
+ g_vlog_info =
+ new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
+ command_line->GetSwitchValueASCII(switches::kVModule),
+ &g_min_log_level);
+ }
}
g_logging_destination = settings.logging_dest;
@@ -581,11 +583,11 @@ LogMessage::~LogMessage() {
#if defined(OS_ANDROID)
__android_log_write(priority, "chromium", str_newline.c_str());
#else
- assert(base::CommandLine::InitializedForCurrentProcess());
__android_log_write(
priority,
- base::CommandLine::ForCurrentProcess()->
- GetProgram().BaseName().value().c_str(),
+ base::CommandLine::InitializedForCurrentProcess() ?
+ base::CommandLine::ForCurrentProcess()->
+ GetProgram().BaseName().value().c_str() : nullptr,
str_newline.c_str());
#endif // defined(OS_ANDROID)
#endif