serialization: protect blob serialization from undefined behavior

There is currently no compiler protection when someone tries to
do (for example) `BLOB_SERIALIZER(std::vector<int>)`. You just
get runtime allocation errors. This has already eaten up dev time
before, so this PR adds a static assertion that the type must be
trivially copyable, as defined by the C++ standard. Types can
override this if applicable if they use `BLOB_SERIALIZER_FORCED`.
This commit is contained in:
jeffro256 2024-02-21 13:08:15 -06:00
parent 059028a30a
commit 9d101d5ea0
No known key found for this signature in database
GPG key ID: 6F79797A6E392442
4 changed files with 31 additions and 4 deletions

View file

@ -51,6 +51,11 @@
using namespace std;
using namespace crypto;
static_assert(!std::is_trivially_copyable<std::vector<unsigned char>>(),
"should fail to compile when applying blob serializer");
static_assert(!std::is_trivially_copyable<std::string>(),
"should fail to compile when applying blob serializer");
struct Struct
{
int32_t a;