Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions include/tscore/CryptoHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<CryptoContextBase *>(_base)->~CryptoContextBase(); }

private:
CryptoContextBase *_base = nullptr;
static size_t constexpr OBJ_SIZE = 256;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I may be able to calculate the minimal size but realized that it requires including header files for SHA256 and MD5.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be good to have some test or debug logic that does calculate the minimum size.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AC_CHECK_SIZEOF? Then maybe we can do std::max. Try it if you are interested.

char _base[OBJ_SIZE];
};

inline bool
CryptoContext::update(void const *data, int length)
{
return _base->update(data, length);
return reinterpret_cast<CryptoContextBase *>(_base)->update(data, length);
}

inline bool
CryptoContext::finalize(CryptoHash &hash)
{
return _base->finalize(hash);
return reinterpret_cast<CryptoContextBase *>(_base)->finalize(hash);
}

ts::BufferWriter &bwformat(ts::BufferWriter &w, ts::BWFSpec const &spec, ats::CryptoHash const &hash);
Expand Down
6 changes: 4 additions & 2 deletions src/tscore/CryptoHash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ CryptoContext::CryptoContext()
case UNSPECIFIED:
#if TS_ENABLE_FIPS == 0
case MD5:
_base = new MD5Context;
static_assert(OBJ_SIZE >= sizeof(MD5Context));
new (_base) MD5Context;
break;
#else
case SHA256:
_base = new SHA256Context;
static_assert(OBJ_SIZE >= sizeof(SHA256Context));
new (_base) SHA256Context;
break;
#endif
default:
Expand Down