Skip to content

Commit

Permalink
fix possible null dereference for nested_root accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaileychess committed May 3, 2023
1 parent 19d8942 commit fb4f6fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
44 changes: 24 additions & 20 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ class CppGenerator : public BaseGenerator {

std::string UnionVectorVerifySignature(const EnumDef &enum_def) {
const std::string name = Name(enum_def);
const std::string & type = opts_.scoped_enums ? name : "uint8_t";
const std::string &type = opts_.scoped_enums ? name : "uint8_t";
return "bool Verify" + name + "Vector" +
"(::flatbuffers::Verifier &verifier, " +
"const ::flatbuffers::Vector<::flatbuffers::Offset<void>> "
Expand Down Expand Up @@ -1807,7 +1807,7 @@ class CppGenerator : public BaseGenerator {
field.value.type.element != BASE_TYPE_UTYPE)) {
auto type = GenTypeNative(field.value.type, false, field);
auto cpp_type = field.attributes.Lookup("cpp_type");
const std::string & full_type =
const std::string &full_type =
(cpp_type
? (IsVector(field.value.type)
? "std::vector<" +
Expand Down Expand Up @@ -1954,9 +1954,10 @@ class CppGenerator : public BaseGenerator {
if (!initializer_list.empty()) { initializer_list += ",\n "; }
const auto cpp_type = field->attributes.Lookup("cpp_type");
const auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type");
const std::string & type_name = (cpp_type) ? cpp_type->constant
: GenTypeNative(type, /*invector*/ false,
*field, /*forcopy*/ true);
const std::string &type_name =
(cpp_type) ? cpp_type->constant
: GenTypeNative(type, /*invector*/ false, *field,
/*forcopy*/ true);
const bool is_ptr = !(IsStruct(type) && field->native_inline) ||
(cpp_type && cpp_ptr_type->constant != "naked");
CodeWriter cw;
Expand All @@ -1976,10 +1977,10 @@ class CppGenerator : public BaseGenerator {
if (vec_type.base_type == BASE_TYPE_UTYPE) continue;
const auto cpp_type = field->attributes.Lookup("cpp_type");
const auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type");
const std::string & type_name = (cpp_type)
? cpp_type->constant
: GenTypeNative(vec_type, /*invector*/ true,
*field, /*forcopy*/ true);
const std::string &type_name =
(cpp_type) ? cpp_type->constant
: GenTypeNative(vec_type, /*invector*/ true, *field,
/*forcopy*/ true);
const bool is_ptr = IsVectorOfPointers(*field) ||
(cpp_type && cpp_ptr_type->constant != "naked");
CodeWriter cw(" ");
Expand Down Expand Up @@ -2733,9 +2734,10 @@ class CppGenerator : public BaseGenerator {
if (!nfn.empty()) {
code_.SetValue("CPP_NAME", nfn);
code_ += " const {{CPP_NAME}} *{{FIELD_NAME}}_nested_root() const {";
code_ += " const auto _f = {{FIELD_NAME}}();";
code_ +=
" return "
"::flatbuffers::GetRoot<{{CPP_NAME}}>({{FIELD_NAME}}()->Data());";
" return _f ? ::flatbuffers::GetRoot<{{CPP_NAME}}>(_f->Data())";
code_ += " : nullptr;";
code_ += " }";
}

Expand All @@ -2745,9 +2747,9 @@ class CppGenerator : public BaseGenerator {
" const {";
// Both Data() and size() are const-methods, therefore call order
// doesn't matter.
code_ +=
" return flexbuffers::GetRoot({{FIELD_NAME}}()->Data(), "
"{{FIELD_NAME}}()->size());";
code_ += " const auto _f = {{FIELD_NAME}}();";
code_ += " return _f ? flexbuffers::GetRoot(_f->Data(), _f->size())";
code_ += " : flexbuffers::Reference();";
code_ += " }";
}

Expand Down Expand Up @@ -2835,8 +2837,9 @@ class CppGenerator : public BaseGenerator {
// Generate code to do force_align for the vector.
if (align > 1) {
const auto vtype = field.value.type.VectorType();
const std::string & type = IsStruct(vtype) ? WrapInNameSpace(*vtype.struct_def)
: GenTypeWire(vtype, "", false);
const std::string &type = IsStruct(vtype)
? WrapInNameSpace(*vtype.struct_def)
: GenTypeWire(vtype, "", false);
return "_fbb.ForceVectorAlignment(" + field_size + ", sizeof(" + type +
"), " + std::to_string(static_cast<long long>(align)) + ");";
}
Expand Down Expand Up @@ -3357,8 +3360,9 @@ class CppGenerator : public BaseGenerator {
}
case BASE_TYPE_UTYPE: {
value = StripUnionType(value);
const std::string & type = opts_.scoped_enums ? Name(*field.value.type.enum_def)
: "uint8_t";
const std::string &type = opts_.scoped_enums
? Name(*field.value.type.enum_def)
: "uint8_t";
auto enum_value = "__va->_" + value + "[i].type";
if (!opts_.scoped_enums)
enum_value = "static_cast<uint8_t>(" + enum_value + ")";
Expand Down Expand Up @@ -3424,7 +3428,7 @@ class CppGenerator : public BaseGenerator {
}
} else {
// _o->field ? CreateT(_fbb, _o->field.get(), _rehasher);
const std::string & type = field.value.type.struct_def->name;
const std::string &type = field.value.type.struct_def->name;
code += value + " ? Create" + type;
code += "(_fbb, " + value;
if (!field.native_inline) code += GenPtrGet(field);
Expand Down Expand Up @@ -3810,7 +3814,7 @@ class CppGenerator : public BaseGenerator {
const auto field_type = GenTypeGet(type, " ", is_array ? "" : "const ",
is_array ? "" : " &", true);
auto member = Name(*field) + "_";
const std::string & value =
const std::string &value =
is_scalar ? "::flatbuffers::EndianScalar(" + member + ")" : member;

code_.SetValue("FIELD_NAME", Name(*field));
Expand Down
12 changes: 9 additions & 3 deletions tests/monster_test_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
return GetPointer<::flatbuffers::Vector<uint8_t> *>(VT_TESTNESTEDFLATBUFFER);
}
const MyGame::Example::Monster *testnestedflatbuffer_nested_root() const {
return ::flatbuffers::GetRoot<MyGame::Example::Monster>(testnestedflatbuffer()->Data());
const auto _f = testnestedflatbuffer();
return _f ? ::flatbuffers::GetRoot<MyGame::Example::Monster>(_f->Data())
: nullptr;
}
const MyGame::Example::Stat *testempty() const {
return GetPointer<const MyGame::Example::Stat *>(VT_TESTEMPTY);
Expand Down Expand Up @@ -1592,7 +1594,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
return GetPointer<::flatbuffers::Vector<uint8_t> *>(VT_FLEX);
}
flexbuffers::Reference flex_flexbuffer_root() const {
return flexbuffers::GetRoot(flex()->Data(), flex()->size());
const auto _f = flex();
return _f ? flexbuffers::GetRoot(_f->Data(), _f->size())
: flexbuffers::Reference();
}
const ::flatbuffers::Vector<const MyGame::Example::Test *> *test5() const {
return GetPointer<const ::flatbuffers::Vector<const MyGame::Example::Test *> *>(VT_TEST5);
Expand Down Expand Up @@ -1722,7 +1726,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
return GetPointer<::flatbuffers::Vector<uint8_t> *>(VT_TESTREQUIREDNESTEDFLATBUFFER);
}
const MyGame::Example::Monster *testrequirednestedflatbuffer_nested_root() const {
return ::flatbuffers::GetRoot<MyGame::Example::Monster>(testrequirednestedflatbuffer()->Data());
const auto _f = testrequirednestedflatbuffer();
return _f ? ::flatbuffers::GetRoot<MyGame::Example::Monster>(_f->Data())
: nullptr;
}
const ::flatbuffers::Vector<::flatbuffers::Offset<MyGame::Example::Stat>> *scalar_key_sorted_tables() const {
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<MyGame::Example::Stat>> *>(VT_SCALAR_KEY_SORTED_TABLES);
Expand Down

0 comments on commit fb4f6fb

Please sign in to comment.