aboutsummaryrefslogtreecommitdiff
path: root/absl/strings/internal/cord_internal.h
diff options
context:
space:
mode:
authorMartijn Vels <mvels@google.com>2022-12-07 07:14:42 -0800
committerCopybara-Service <copybara-worker@google.com>2022-12-07 07:15:19 -0800
commit5736d76ae60c1746cf6fe60466df655c91bf7a7b (patch)
treef2a307cbb31cc2de71ba424784626d3b3a2568f8 /absl/strings/internal/cord_internal.h
parentc96db73c09dbb528eca6d19a50bd258b37e9fd5e (diff)
downloadabseil-5736d76ae60c1746cf6fe60466df655c91bf7a7b.tar.gz
abseil-5736d76ae60c1746cf6fe60466df655c91bf7a7b.tar.bz2
abseil-5736d76ae60c1746cf6fe60466df655c91bf7a7b.zip
Remove possible UB from CopyRaw()
This change removes the 'create by single memcpy' CopyRaw() logic to avoid potential UB from the atomic refcount data being included in the memcpy. While we know this works on all supported platforms, it is officially UB, and we should change it to something else. This change changes the CopyRaw() method to explicitly create a (default initialized) instance, initialize `length` and `refcount`, and then memcpy the remaining contents which are trivial uint8_t and CordRep* values. PiperOrigin-RevId: 493596072 Change-Id: I46618883eb1c7c9ed9eb083f4d3e7fc501f23df5
Diffstat (limited to 'absl/strings/internal/cord_internal.h')
-rw-r--r--absl/strings/internal/cord_internal.h7
1 files changed, 6 insertions, 1 deletions
diff --git a/absl/strings/internal/cord_internal.h b/absl/strings/internal/cord_internal.h
index fcca3a28..6022c1df 100644
--- a/absl/strings/internal/cord_internal.h
+++ b/absl/strings/internal/cord_internal.h
@@ -225,7 +225,11 @@ struct CordRep {
: length(l), refcount(immortal), tag(EXTERNAL), storage{} {}
// The following three fields have to be less than 32 bytes since
- // that is the smallest supported flat node size.
+ // that is the smallest supported flat node size. Some code optimizations rely
+ // on the specific layout of these fields. Notably: the non-trivial field
+ // `refcount` being preceeded by `length`, and being tailed by POD data
+ // members only.
+ // # LINT.IfChange
size_t length;
RefcountAndFlags refcount;
// If tag < FLAT, it represents CordRepKind and indicates the type of node.
@@ -241,6 +245,7 @@ struct CordRep {
// allocate room for these in the derived class, as not all compilers reuse
// padding space from the base class (clang and gcc do, MSVC does not, etc)
uint8_t storage[3];
+ // # LINT.ThenChange(cord_rep_btree.h:copy_raw)
// Returns true if this instance's tag matches the requested type.
constexpr bool IsRing() const { return tag == RING; }