From b7900977b53ce4792dda35648f4da268af95c490 Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Mon, 7 Mar 2022 09:31:48 -0800 Subject: [PATCH] Fixed use-of-uninitialized-value with msan (#29007) * Fixed use-of-uninitialized-value with msan * Add msvc 2015 support * Fix #include * Supports GCC versions < 5 --- src/core/lib/gprpp/status_helper.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/core/lib/gprpp/status_helper.cc b/src/core/lib/gprpp/status_helper.cc index d2fb8f57001..35956641c94 100644 --- a/src/core/lib/gprpp/status_helper.cc +++ b/src/core/lib/gprpp/status_helper.cc @@ -20,6 +20,8 @@ #include "src/core/lib/gprpp/status_helper.h" +#include + #include "absl/strings/cord.h" #include "absl/strings/escaping.h" #include "absl/strings/match.h" @@ -218,9 +220,23 @@ absl::optional StatusGetStr(const absl::Status& status, void StatusSetTime(absl::Status* status, StatusTimeProperty key, absl::Time time) { +#if !defined(__clang__) && defined(_MSC_VER) && _MSC_VER < 1910 + // Abseil has a workaround for MSVC 2015 which prevents absl::Time + // from being is_trivially_copyable but it's still safe to be + // memcopied. +#elif defined(__GNUG__) && __GNUC__ < 5 + // GCC versions < 5 do not support std::is_trivially_copyable +#else + static_assert(std::is_trivially_copyable::value, + "absl::Time needs to be able to be memcopied"); +#endif + // This is required not to get uninitialized padding of absl::Time. + alignas(absl::Time) char buf[sizeof(time)] = { + 0, + }; + new (buf) absl::Time(time); status->SetPayload(GetStatusTimePropertyUrl(key), - absl::Cord(absl::string_view( - reinterpret_cast(&time), sizeof(time)))); + absl::Cord(absl::string_view(buf, sizeof(time)))); } absl::optional StatusGetTime(const absl::Status& status,