Prevent creation of watchers/fetchers with non-raw flatbuffers
It turns out that static flatbuffers look enough like regular flatbuffer
types that the compiler would happily let people write watchers/fetchers
that used static flatbuffer types, but which would then produce an
invalid static flatbuffer due to how the memory was getting passed
around.
To address this, add some static asserts to ensure that the type being
used is actually a flatbuffer::Table type of some vintage.
Change-Id: I80736ca1ca8d33f52bb3b27219fe22876ba5a5f5
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/events/event_loop.h b/aos/events/event_loop.h
index ff629f9..93c5663 100644
--- a/aos/events/event_loop.h
+++ b/aos/events/event_loop.h
@@ -694,6 +694,13 @@
// Fetcher before using it.
template <typename T>
Fetcher<T> TryMakeFetcher(const std::string_view channel_name) {
+ // Note: This could be done with SFINAE, but then you don't get as good an
+ // error message and the main benefit of SFINAE is to be able to make
+ // compilation *not* fail if we e.g. had another MakeFetcher overload that
+ // could take static flatbuffers.
+ static_assert(std::is_base_of<flatbuffers::Table, T>::value,
+ "Fetchers must be created with raw flatbuffer types---static "
+ "flatbuffers are currently not supported with fetchers.");
const Channel *const channel = GetChannel<T>(channel_name);
if (channel == nullptr) {
return Fetcher<T>();
diff --git a/aos/events/event_loop_tmpl.h b/aos/events/event_loop_tmpl.h
index 3f41ec1..25aa36c 100644
--- a/aos/events/event_loop_tmpl.h
+++ b/aos/events/event_loop_tmpl.h
@@ -39,6 +39,13 @@
void EventLoop::MakeWatcher(const std::string_view channel_name, Watch &&w) {
using MessageType = typename event_loop_internal::watch_message_type_trait<
decltype(&Watch::operator())>::message_type;
+ // Note: This could be done with SFINAE, but then you don't get as good an
+ // error message and the main benefit of SFINAE is to be able to make
+ // compilation *not* fail if we e.g. had another MakeWatcher overload that
+ // could take static flatbuffers.
+ static_assert(std::is_base_of<flatbuffers::Table, MessageType>::value,
+ "Watchers must be created with raw flatbuffer types---static "
+ "flatbuffers are currently not supported with watchers.");
const Channel *channel = configuration::GetChannel(
configuration_, channel_name, MessageType::GetFullyQualifiedName(),
name(), node());