Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ class SymbolTable {
}

bool Add(const std::string& name, T* e) {
vec.emplace_back(e);
auto it = dict.find(name);
if (it != dict.end()) return true;
vec.emplace_back(e);
dict[name] = e;
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/idl_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3126,8 +3126,10 @@ CheckedError Parser::StartEnum(const std::string& name, bool is_union,
enum_def.is_union = is_union;
enum_def.defined_namespace = current_namespace_;
const auto qualified_name = current_namespace_->GetFullyQualifiedName(name);
if (enums_.Add(qualified_name, &enum_def))
if (enums_.Add(qualified_name, &enum_def)) {
delete &enum_def;
return Error("enum already exists: " + qualified_name);
}
enum_def.underlying_type.base_type =
is_union ? BASE_TYPE_UTYPE : BASE_TYPE_INT;
enum_def.underlying_type.enum_def = &enum_def;
Expand Down
29 changes: 29 additions & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,34 @@ void EmbeddedSchemaAccess() {
EmbeddedSchemaAccessByType<StatT>();
}

void DuplicateEnumNameBinarySchemaTest() {
static const uint8_t duplicate_enum_name_schema[] = {
0x10, 0x00, 0x00, 0x00, 0x42, 0x46, 0x42, 0x53, 0x08, 0x00, 0x0c, 0x00,
0x04, 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x30, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x10, 0x00,
0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x4c, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
0x0c, 0x00, 0x12, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0c, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x06, 0x00, 0x05, 0x00,
0x06, 0x00, 0x00, 0x00, 0x00, 0x03, 0x06, 0x00, 0x08, 0x00, 0x07, 0x00,
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x44, 0x75, 0x70, 0x6c,
0x69, 0x63, 0x61, 0x74, 0x65, 0x45, 0x6e, 0x75, 0x6d, 0x00, 0x00, 0x00,
0x0d, 0x00, 0x00, 0x00, 0x44, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74,
0x65, 0x45, 0x6e, 0x75, 0x6d, 0x00, 0x00, 0x00};

flatbuffers::Verifier verifier(duplicate_enum_name_schema,
sizeof(duplicate_enum_name_schema));
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);

flatbuffers::Parser parser;
TEST_EQ(parser.Deserialize(duplicate_enum_name_schema,
sizeof(duplicate_enum_name_schema)),
false);
}

void NestedVerifierTest() {
// Create a nested monster.
flatbuffers::FlatBufferBuilder nested_builder;
Expand Down Expand Up @@ -1848,6 +1876,7 @@ int FlatBufferTests(const std::string& tests_data_path) {
FlexBuffersFloatingPointTest();
FlatbuffersIteratorsTest();
WarningsAsErrorsTest();
DuplicateEnumNameBinarySchemaTest();
NestedVerifierTest();
SizeVerifierTest();
PrivateAnnotationsLeaks();
Expand Down