Skip to content

Commit

Permalink
Fix a crash when calling EstimatedMemoryUsage() on an empty checksu…
Browse files Browse the repository at this point in the history
…mmed `absl::Cord`.

PiperOrigin-RevId: 567695227
Change-Id: I13eb8a1872d2fe703b5f3b9bc8df7fec4381fb55
  • Loading branch information
Abseil Team authored and copybara-github committed Sep 22, 2023
1 parent 90e8f6f commit 1ad2209
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions absl/strings/cord_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ size_t GetEstimatedUsage(const CordRep* rep) {
// Consume the top level CRC node if present.
if (repref.rep->tag == CRC) {
raw_usage.Add(sizeof(CordRepCrc), repref);
if (repref.rep->crc()->child == nullptr) {
return static_cast<size_t>(raw_usage.total);
}
repref = repref.Child(repref.rep->crc()->child);
}

Expand Down
6 changes: 6 additions & 0 deletions absl/strings/cord_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3240,6 +3240,12 @@ TEST_P(CordTest, ChecksummedEmptyCord) {
EXPECT_EQ(absl::HashOf(c3), absl::HashOf(absl::string_view()));
}

TEST(CrcCordTest, ChecksummedEmptyCordEstimateMemoryUsage) {
absl::Cord cord;
cord.SetExpectedChecksum(0);
EXPECT_NE(cord.EstimatedMemoryUsage(), 0);
}

#if defined(GTEST_HAS_DEATH_TEST) && defined(ABSL_INTERNAL_CORD_HAVE_SANITIZER)

// Returns an expected poison / uninitialized death message expression.
Expand Down

0 comments on commit 1ad2209

Please sign in to comment.