From 3eb45e552f831481bee004db95688dc335251b28 Mon Sep 17 00:00:00 2001 From: sssooonnnggg Date: Tue, 29 Nov 2022 12:59:53 +0800 Subject: [PATCH] chore: emit more reasonable error message when using incomplete type in struct (#7678) Co-authored-by: Derek Bailey --- include/flatbuffers/idl.h | 4 ++++ src/idl_parser.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index a07f62a30b2..f6384894535 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -473,6 +473,10 @@ inline bool IsStruct(const Type &type) { return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed; } +inline bool IsIncompleteStruct(const Type &type) { + return type.base_type == BASE_TYPE_STRUCT && type.struct_def->predecl; +} + inline bool IsTable(const Type &type) { return type.base_type == BASE_TYPE_STRUCT && !type.struct_def->fixed; } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index b6b7e9db31a..0d93afbc17c 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -918,6 +918,12 @@ CheckedError Parser::ParseField(StructDef &struct_def) { ECHECK(ParseType(type)); if (struct_def.fixed) { + if (IsIncompleteStruct(type) || + (IsArray(type) && IsIncompleteStruct(type.VectorType()))) { + std::string type_name = IsArray(type) ? type.VectorType().struct_def->name : type.struct_def->name; + return Error(std::string("Incomplete type in struct is not allowed, type name: ") + type_name); + } + auto valid = IsScalar(type.base_type) || IsStruct(type); if (!valid && IsArray(type)) { const auto &elem_type = type.VectorType();