Merge pull request #2799 from atn34/atn34/fix-void

Encode Void as a valid flatbuffers message
This commit is contained in:
A.J. Beamon 2020-03-11 11:32:25 -07:00 committed by GitHub
commit 4b60c4ebba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -488,6 +488,10 @@ TEST_CASE("/flow/FlatBuffers/Standalone") {
// Meant to be run with valgrind or asan, to catch heap buffer overflows
TEST_CASE("/flow/FlatBuffers/Void") {
Standalone<StringRef> msg = ObjectWriter::toValue(Void(), Unversioned());
// Manually verified to be a valid flatbuffers message. This is technically brittle since there are other valid
// encodings of this message, but our implementation is unlikely to change.
ASSERT(msg == LiteralStringRef("\x14\x00\x00\x00J\xad\x1e\x00\x00\x00\x04\x00\x04\x00\x06\x00\x08\x00\x04\x00\x06"
"\x00\x00\x00\x04\x00\x00\x00\x12\x00\x00\x00"));
auto buffer = std::make_unique<uint8_t[]>(msg.size()); // Make a heap allocation of precisely the right size, so
// that asan or valgrind will catch any overflows
memcpy(buffer.get(), msg.begin(), msg.size());

View File

@ -82,20 +82,21 @@ inline typename Archive::READER& operator >> (Archive& ar, Item& item ) {
return ar;
}
template <class Archive>
void serializer(Archive& ar) {}
template <class Archive, class Item, class... Items>
typename Archive::WRITER& serializer(Archive& ar, const Item& item, const Items&... items) {
save(ar, item);
serializer(ar, items...);
if constexpr (sizeof...(Items) > 0) {
serializer(ar, items...);
}
return ar;
}
template <class Archive, class Item, class... Items>
typename Archive::READER& serializer(Archive& ar, Item& item, Items&... items) {
load(ar, item);
serializer(ar, items...);
if constexpr (sizeof...(Items) > 0) {
serializer(ar, items...);
}
return ar;
}