diff options
author | Marcin Kowalczyk <qrczak@google.com> | 2024-03-26 01:43:16 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-03-26 01:44:07 -0700 |
commit | 86f3019430baa53ca1bc38b7e370fcd5fc8d2503 (patch) | |
tree | a8f7f8654ca1a5cced2a0e67788b7a6431b56bb9 /absl | |
parent | ad5499a290fd98de54ee54dcf8120f8d287640ce (diff) |
Fix a bug in `Cord::{Append,Prepend}(CordBuffer)`: call
`MaybeRemoveEmptyCrcNode()`. Otherwise appending a `CordBuffer` an empty Cord
with a CRC node crashes (`RemoveCrcNode()` which increases the refcount of a
nullptr child).
Cosmetics: in `Cord::InlineRep::AppendArray()`, return early for empty `src`
before removing the empty CRC node.
PiperOrigin-RevId: 619107278
Change-Id: I4f1bc6b75c662f4678c61e3ef310e8597d62e2e1
Diffstat (limited to 'absl')
-rw-r--r-- | absl/strings/cord.cc | 2 | ||||
-rw-r--r-- | absl/strings/cord.h | 3 |
2 files changed, 4 insertions, 1 deletions
diff --git a/absl/strings/cord.cc b/absl/strings/cord.cc index 2b61bdfc..ea84e446 100644 --- a/absl/strings/cord.cc +++ b/absl/strings/cord.cc @@ -425,8 +425,8 @@ Cord& Cord::operator=(absl::string_view src) { // we keep it here to make diffs easier. void Cord::InlineRep::AppendArray(absl::string_view src, MethodIdentifier method) { - MaybeRemoveEmptyCrcNode(); if (src.empty()) return; // memcpy(_, nullptr, 0) is undefined. + MaybeRemoveEmptyCrcNode(); size_t appended = 0; CordRep* rep = tree(); diff --git a/absl/strings/cord.h b/absl/strings/cord.h index 2583aa8a..5aebd567 100644 --- a/absl/strings/cord.h +++ b/absl/strings/cord.h @@ -75,6 +75,7 @@ #include "absl/base/internal/per_thread_tls.h" #include "absl/base/macros.h" #include "absl/base/nullability.h" +#include "absl/base/optimization.h" #include "absl/base/port.h" #include "absl/container/inlined_vector.h" #include "absl/crc/internal/crc_cord_state.h" @@ -1388,6 +1389,7 @@ inline void Cord::Prepend(absl::string_view src) { inline void Cord::Append(CordBuffer buffer) { if (ABSL_PREDICT_FALSE(buffer.length() == 0)) return; + contents_.MaybeRemoveEmptyCrcNode(); absl::string_view short_value; if (CordRep* rep = buffer.ConsumeValue(short_value)) { contents_.AppendTree(rep, CordzUpdateTracker::kAppendCordBuffer); @@ -1398,6 +1400,7 @@ inline void Cord::Append(CordBuffer buffer) { inline void Cord::Prepend(CordBuffer buffer) { if (ABSL_PREDICT_FALSE(buffer.length() == 0)) return; + contents_.MaybeRemoveEmptyCrcNode(); absl::string_view short_value; if (CordRep* rep = buffer.ConsumeValue(short_value)) { contents_.PrependTree(rep, CordzUpdateTracker::kPrependCordBuffer); |