summaryrefslogtreecommitdiff
path: root/absl/log
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-02-23 13:05:39 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2023-02-23 13:06:27 -0800
commit4825ef40953aeb4a71e3625203b760381da6b0d7 (patch)
treed8931476e759cd07d9b22ebad887700161b7e725 /absl/log
parent6d16237574fed8538a4864954a5e2b5a098f32f2 (diff)
Initialize ScopedMockLog.is_triggered_ with false.
In C++20, this happens by default, but in C++17, it's left uninitialized if no logs are sent and StartCapturingLogs is never called. The destructor then makes an attempt to access it, which is both unsafe and not very useful while it is uninitialized. Also add a test that expects the appropriate CHECK fail when StartCapturingLogs is not called. PiperOrigin-RevId: 511865440 Change-Id: Ie23ff3f901e926761d5f487e10d33c21c3bd43d3
Diffstat (limited to 'absl/log')
-rw-r--r--absl/log/scoped_mock_log.cc2
-rw-r--r--absl/log/scoped_mock_log.h3
-rw-r--r--absl/log/scoped_mock_log_test.cc5
3 files changed, 9 insertions, 1 deletions
diff --git a/absl/log/scoped_mock_log.cc b/absl/log/scoped_mock_log.cc
index 4ebc0a9f..39a0a52a 100644
--- a/absl/log/scoped_mock_log.cc
+++ b/absl/log/scoped_mock_log.cc
@@ -30,7 +30,7 @@ namespace absl {
ABSL_NAMESPACE_BEGIN
ScopedMockLog::ScopedMockLog(MockLogDefault default_exp)
- : sink_(this), is_capturing_logs_(false) {
+ : sink_(this), is_capturing_logs_(false), is_triggered_(false) {
if (default_exp == MockLogDefault::kIgnoreUnexpected) {
// Ignore all calls to Log we did not set expectations for.
EXPECT_CALL(*this, Log).Times(::testing::AnyNumber());
diff --git a/absl/log/scoped_mock_log.h b/absl/log/scoped_mock_log.h
index 44470c16..399e604d 100644
--- a/absl/log/scoped_mock_log.h
+++ b/absl/log/scoped_mock_log.h
@@ -185,6 +185,9 @@ class ScopedMockLog final {
ForwardingSink sink_;
bool is_capturing_logs_;
+ // Until C++20, the default constructor leaves the underlying value wrapped in
+ // std::atomic uninitialized, so all constructors should be sure to initialize
+ // is_triggered_.
std::atomic<bool> is_triggered_;
};
diff --git a/absl/log/scoped_mock_log_test.cc b/absl/log/scoped_mock_log_test.cc
index 44b8d737..42736939 100644
--- a/absl/log/scoped_mock_log_test.cc
+++ b/absl/log/scoped_mock_log_test.cc
@@ -71,6 +71,11 @@ TEST(ScopedMockLogDeathTest, StopCapturingLogsCannotBeCalledWhenNotCapturing) {
},
"StopCapturingLogs");
}
+
+TEST(ScopedMockLogDeathTest, FailsCheckIfStartCapturingLogsIsNeverCalled) {
+ EXPECT_DEATH({ absl::ScopedMockLog log; },
+ "Did you forget to call StartCapturingLogs");
+}
#endif
// Tests that ScopedMockLog intercepts LOG()s when it's alive.