Catch a null schema more gracefully
We were dereferencing the null pointer. It gives a rather cryptic
error. We can do a lot better.
Change-Id: I1aae2109cee6aa9f972d4e149a5779df42ab7d36
diff --git a/aos/flatbuffer_introspection.cc b/aos/flatbuffer_introspection.cc
index 6f32424..ae402fe 100644
--- a/aos/flatbuffer_introspection.cc
+++ b/aos/flatbuffer_introspection.cc
@@ -351,6 +351,14 @@
std::string FlatbufferToJson(const reflection::Schema *schema,
const uint8_t *data, bool multi_line,
size_t max_vector_size) {
+ CHECK(schema != nullptr) << ": Need to provide a schema";
+
+ // It is pretty common to get passed in a nullptr when a test fails. Rather
+ // than CHECK, return a more user friendly result.
+ if (data == nullptr) {
+ return "null";
+ }
+
const flatbuffers::Table *table = flatbuffers::GetAnyRoot(data);
const reflection::Object *obj = schema->root_table();
diff --git a/aos/flatbuffer_introspection_test.cc b/aos/flatbuffer_introspection_test.cc
index 4214202..9dacd41 100644
--- a/aos/flatbuffer_introspection_test.cc
+++ b/aos/flatbuffer_introspection_test.cc
@@ -464,5 +464,20 @@
"}");
}
+// Tests that a nullptr buffer prints nullptr.
+TEST_F(FlatbufferIntrospectionTest, NullptrData) {
+ EXPECT_EQ("null", FlatbufferToJson(schema_, nullptr));
+}
+
+// Tests that a null schema gets caught.
+TEST(FlatbufferIntrospectionDeathTest, NullSchema) {
+ EXPECT_DEATH(
+ {
+ FlatbufferToJson(static_cast<const reflection::Schema *>(nullptr),
+ nullptr);
+ },
+ "Need to provide a schema");
+}
+
} // namespace testing
} // namespace aos
diff --git a/aos/json_to_flatbuffer_test.cc b/aos/json_to_flatbuffer_test.cc
index a9112f4..6dbb7ea 100644
--- a/aos/json_to_flatbuffer_test.cc
+++ b/aos/json_to_flatbuffer_test.cc
@@ -231,5 +231,11 @@
EXPECT_EQ("{ \"vector_foo_int\": [ ... 101 elements ... ] }", back_json_long);
}
+// Tests that a nullptr buffer prints nullptr.
+TEST_F(JsonToFlatbufferTest, NullptrData) {
+ EXPECT_EQ("null", TableFlatbufferToJson((const flatbuffers::Table *)(nullptr),
+ ConfigurationTypeTable(), false));
+}
+
} // namespace testing
} // namespace aos