summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@google.com>2022-05-12 13:38:33 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-05-12 13:39:22 -0700
commit3af16ffea816504d44a60fcc083d9f58b7d40d0d (patch)
tree7bc05986f12a2c2d41a729e3615f59a06692500b
parent9aa7d0bd2079f287162d4fd0722a1b9032e39a6a (diff)
Fix an msan warning in cord_ringbuffer_test
Stop the absl::Cord destructor from running on the constinit cord in CordTest.ConstinitConstructor. This allows inspecting the cord at any point while the test is exiting, which is important for the semantics of the test. PiperOrigin-RevId: 448327386 Change-Id: Icef9faa2b63f1f0ae60b3430dcf6184f5dead885
-rw-r--r--absl/strings/cord_test.cc24
1 files changed, 23 insertions, 1 deletions
diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc
index f69209de..4261c99a 100644
--- a/absl/strings/cord_test.cc
+++ b/absl/strings/cord_test.cc
@@ -2241,12 +2241,34 @@ class AfterExitCordTester {
absl::string_view expected_;
};
+// Deliberately prevents the destructor for an absl::Cord from running. The cord
+// is accessible via the cord member during the lifetime of the CordLeaker.
+// After the CordLeaker is destroyed, pointers to the cord will remain valid
+// until the CordLeaker's memory is deallocated.
+struct CordLeaker {
+ union {
+ absl::Cord cord;
+ };
+
+ template <typename Str>
+ constexpr explicit CordLeaker(const Str& str) : cord(str) {}
+
+ ~CordLeaker() {
+ // Don't do anything, including running cord's destructor. (cord's
+ // destructor won't run automatically because cord is hidden inside a
+ // union.)
+ }
+};
+
template <typename Str>
void TestConstinitConstructor(Str) {
const auto expected = Str::value;
// Defined before `cord` to be destroyed after it.
static AfterExitCordTester exit_tester; // NOLINT
- ABSL_CONST_INIT static absl::Cord cord(Str{}); // NOLINT
+ ABSL_CONST_INIT static CordLeaker cord_leaker(Str{}); // NOLINT
+ // cord_leaker is static, so this reference will remain valid through the end
+ // of program execution.
+ static absl::Cord& cord = cord_leaker.cord;
static bool init_exit_tester = exit_tester.Set(&cord, expected);
(void)init_exit_tester;