From 07ac26d700c5633d6a0662c188aee50f1fba3d62 Mon Sep 17 00:00:00 2001 From: Masakazu Kitajo Date: Tue, 28 Feb 2023 14:59:45 -0700 Subject: [PATCH 1/3] Avoid memory allocation in CryptoHash --- include/tscore/CryptoHash.h | 13 +++++-------- src/tscore/CryptoHash.cc | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/include/tscore/CryptoHash.h b/include/tscore/CryptoHash.h index 4c2d97e3ff6..02c1f9d7147 100644 --- a/include/tscore/CryptoHash.h +++ b/include/tscore/CryptoHash.h @@ -168,26 +168,23 @@ class CryptoContext : public CryptoContextBase }; ///< What type of hash we really are. static HashType Setting; - ~CryptoContext() - { - delete _base; - _base = nullptr; - } + ~CryptoContext() { reinterpret_cast(_base)->~CryptoContextBase(); } private: - CryptoContextBase *_base = nullptr; + static size_t constexpr OBJ_SIZE = 256; + char _base[OBJ_SIZE]; }; inline bool CryptoContext::update(void const *data, int length) { - return _base->update(data, length); + return reinterpret_cast(_base)->update(data, length); } inline bool CryptoContext::finalize(CryptoHash &hash) { - return _base->finalize(hash); + return reinterpret_cast(_base)->finalize(hash); } ts::BufferWriter &bwformat(ts::BufferWriter &w, ts::BWFSpec const &spec, ats::CryptoHash const &hash); diff --git a/src/tscore/CryptoHash.cc b/src/tscore/CryptoHash.cc index 8ee07eec11f..56049224396 100644 --- a/src/tscore/CryptoHash.cc +++ b/src/tscore/CryptoHash.cc @@ -45,11 +45,11 @@ CryptoContext::CryptoContext() case UNSPECIFIED: #if TS_ENABLE_FIPS == 0 case MD5: - _base = new MD5Context; + new (_base) MD5Context; break; #else case SHA256: - _base = new SHA256Context; + new (_base) SHA256Context; break; #endif default: From 52ed2e0a512fd0d6ea852ad2a265918e7c6c9829 Mon Sep 17 00:00:00 2001 From: Masakazu Kitajo Date: Tue, 28 Feb 2023 19:36:39 -0700 Subject: [PATCH 2/3] Add static_assert to ensure _base is big enough --- src/tscore/CryptoHash.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tscore/CryptoHash.cc b/src/tscore/CryptoHash.cc index 56049224396..5448df77b9a 100644 --- a/src/tscore/CryptoHash.cc +++ b/src/tscore/CryptoHash.cc @@ -45,10 +45,12 @@ CryptoContext::CryptoContext() case UNSPECIFIED: #if TS_ENABLE_FIPS == 0 case MD5: + static_assert(OBJ_SIZE > sizeof(MD5Context)); new (_base) MD5Context; break; #else case SHA256: + static_assert(OBJ_SIZE > sizeof(SHA256Context)); new (_base) SHA256Context; break; #endif From d874ac5ea4832e8b190f856ec646d0b005bf0104 Mon Sep 17 00:00:00 2001 From: Masakazu Kitajo Date: Tue, 7 Mar 2023 11:39:36 -0700 Subject: [PATCH 3/3] Fix static_assert condition --- src/tscore/CryptoHash.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tscore/CryptoHash.cc b/src/tscore/CryptoHash.cc index 5448df77b9a..211bd1a843d 100644 --- a/src/tscore/CryptoHash.cc +++ b/src/tscore/CryptoHash.cc @@ -45,12 +45,12 @@ CryptoContext::CryptoContext() case UNSPECIFIED: #if TS_ENABLE_FIPS == 0 case MD5: - static_assert(OBJ_SIZE > sizeof(MD5Context)); + static_assert(OBJ_SIZE >= sizeof(MD5Context)); new (_base) MD5Context; break; #else case SHA256: - static_assert(OBJ_SIZE > sizeof(SHA256Context)); + static_assert(OBJ_SIZE >= sizeof(SHA256Context)); new (_base) SHA256Context; break; #endif