From 9f32edf4dfd2acd18eb37d18bcfbd3ed2fe3bbd2 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Mon, 29 Jul 2019 17:11:45 -0700 Subject: [PATCH] Avoid memcpy for small types This is undefined behavior, since it's potentially a misaligned access. But it's _probably_ not worse than the status quo --- fdbclient/FDBTypes.h | 4 ++-- flow/Arena.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fdbclient/FDBTypes.h b/fdbclient/FDBTypes.h index 06b574ec89..690ebb9865 100644 --- a/fdbclient/FDBTypes.h +++ b/fdbclient/FDBTypes.h @@ -331,12 +331,12 @@ struct string_serialized_traits : std::true_type { uint32_t save(uint8_t* out, const KeyValueRef& item) const { auto begin = out; uint32_t sz = item.key.size(); - memcpy(out, &sz, sizeof(sz)); + *reinterpret_cast(out) = sz; out += sizeof(sz); memcpy(out, item.key.begin(), sz); out += sz; sz = item.value.size(); - memcpy(out, &sz, sizeof(sz)); + *reinterpret_cast(out) = sz; out += sizeof(sz); memcpy(out, item.value.begin(), sz); out += sz; diff --git a/flow/Arena.h b/flow/Arena.h index b956b195b0..0f78d41290 100644 --- a/flow/Arena.h +++ b/flow/Arena.h @@ -1143,7 +1143,7 @@ struct dynamic_size_traits> : std::true_typ string_serialized_traits traits; auto* p = out; uint32_t length = t.size(); - memcpy(out, &length, sizeof(length)); + *reinterpret_cast(out) = length; out += sizeof(length); for (const auto& item : t) { out += traits.save(out, item);