diff options
author | babenko <babenko@yandex-team.com> | 2024-08-31 10:34:35 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2024-08-31 10:53:58 +0300 |
commit | 936bbf50940b23dafb36eac2117611136fa94b1e (patch) | |
tree | 3a16087d3b93508ecb0df3b0bf49fce4329099e4 /library | |
parent | 14ddd77a270f3fd464f24ebc25f142c04d6269a8 (diff) | |
download | ydb-936bbf50940b23dafb36eac2117611136fa94b1e.tar.gz |
YT-22642: Fix unaligned access UB
378099ca41e7698fba0ceda68b8d2b554e61b6ea
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/yt/misc/unaligned-inl.h | 31 | ||||
-rw-r--r-- | library/cpp/yt/misc/unaligned.h | 23 |
2 files changed, 54 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/unaligned-inl.h b/library/cpp/yt/misc/unaligned-inl.h new file mode 100644 index 0000000000..68e1c9b499 --- /dev/null +++ b/library/cpp/yt/misc/unaligned-inl.h @@ -0,0 +1,31 @@ +#ifndef UNALIGNED_INL_H_ +#error "Direct inclusion of this file is not allowed, include unaligned.h" +// For the sake of sane code completion. +#include "unaligned.h" +#endif + +#include <cstring> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +template <class T> + requires std::is_trivial_v<T> +T UnalignedLoad(const T* ptr) +{ + T value; + std::memcpy(&value, ptr, sizeof(T)); + return value; +} + +template <class T> + requires std::is_trivial_v<T> +void UnalignedStore(T* ptr, const T& value) +{ + std::memcpy(ptr, &value, sizeof(T)); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/misc/unaligned.h b/library/cpp/yt/misc/unaligned.h new file mode 100644 index 0000000000..68c124183f --- /dev/null +++ b/library/cpp/yt/misc/unaligned.h @@ -0,0 +1,23 @@ +#pragma once + +#include <type_traits> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +template <class T> + requires std::is_trivial_v<T> +T UnalignedLoad(const T* ptr); + +template <class T> + requires std::is_trivial_v<T> +void UnalignedStore(T* ptr, const T& value); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT + +#define UNALIGNED_INL_H_ +#include "unaligned-inl.h" +#undef UNALIGNED_INL_H_ |