aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWouter van Oortmerssen <aardappel@gmail.com>2019-04-19 11:49:49 -0700
committerWouter van Oortmerssen <aardappel@gmail.com>2019-04-19 11:49:49 -0700
commit5b43e4bbb81c170f1e83100e88157b2f55fe0e74 (patch)
tree776edf5a65cd0aab705bf5d363b24c0113f60b71
parentecd76e898dadb71096fdb3a7701a863c399de6c0 (diff)
downloadflatbuffers-5b43e4bbb81c170f1e83100e88157b2f55fe0e74.tar.gz
Fix heap-buffer-overflow if there is a struct within a union
The validator previously did not check if a struct within a union was valid, causing a heap buffer overflow. Add a check to make sure that the struct is valid in this case. Change-Id: I87d41b12fdfc2a99406789531ba92b841c063c76
-rw-r--r--src/idl_gen_cpp.cpp3
-rw-r--r--tests/union_vector/union_vector_generated.h6
2 files changed, 5 insertions, 4 deletions
diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp
index a50d28ac..268c436e 100644
--- a/src/idl_gen_cpp.cpp
+++ b/src/idl_gen_cpp.cpp
@@ -1213,7 +1213,8 @@ class CppGenerator : public BaseGenerator {
" auto ptr = reinterpret_cast<const {{TYPE}} *>(obj);";
if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
if (ev.union_type.struct_def->fixed) {
- code_ += " return true;";
+ code_ += " return verifier.Verify<{{TYPE}}>(static_cast<const "
+ "uint8_t *>(obj), 0);";
} else {
code_ += getptr;
code_ += " return verifier.VerifyTable(ptr);";
diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h
index 9da819a3..757a9584 100644
--- a/tests/union_vector/union_vector_generated.h
+++ b/tests/union_vector/union_vector_generated.h
@@ -547,13 +547,13 @@ inline bool VerifyCharacter(flatbuffers::Verifier &verifier, const void *obj, Ch
return verifier.VerifyTable(ptr);
}
case Character_Rapunzel: {
- return true;
+ return verifier.Verify<Rapunzel>(static_cast<const uint8_t *>(obj), 0);
}
case Character_Belle: {
- return true;
+ return verifier.Verify<BookReader>(static_cast<const uint8_t *>(obj), 0);
}
case Character_BookFan: {
- return true;
+ return verifier.Verify<BookReader>(static_cast<const uint8_t *>(obj), 0);
}
case Character_Other: {
auto ptr = reinterpret_cast<const flatbuffers::String *>(obj);