mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 11:42:40 +00:00
* Clean-up nested_parser on all paths (#5179) * Added test for parsing json with invalid nested flatbuffer * Removed utf-8 BOM from test.cpp
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
2e865f4d4e
commit
1c7d91cc55
@@ -1216,7 +1216,14 @@ CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
|
|||||||
nested_parser.uses_flexbuffers_ = uses_flexbuffers_;
|
nested_parser.uses_flexbuffers_ = uses_flexbuffers_;
|
||||||
|
|
||||||
// Parse JSON substring into new flatbuffer builder using nested_parser
|
// Parse JSON substring into new flatbuffer builder using nested_parser
|
||||||
if (!nested_parser.Parse(substring.c_str(), nullptr, nullptr)) {
|
bool ok = nested_parser.Parse(substring.c_str(), nullptr, nullptr);
|
||||||
|
|
||||||
|
// Clean nested_parser to avoid deleting the elements in
|
||||||
|
// the SymbolTables on destruction
|
||||||
|
nested_parser.enums_.dict.clear();
|
||||||
|
nested_parser.enums_.vec.clear();
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
ECHECK(Error(nested_parser.error_));
|
ECHECK(Error(nested_parser.error_));
|
||||||
}
|
}
|
||||||
// Force alignment for nested flatbuffer
|
// Force alignment for nested flatbuffer
|
||||||
@@ -1226,11 +1233,6 @@ CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
|
|||||||
auto off = builder_.CreateVector(nested_parser.builder_.GetBufferPointer(),
|
auto off = builder_.CreateVector(nested_parser.builder_.GetBufferPointer(),
|
||||||
nested_parser.builder_.GetSize());
|
nested_parser.builder_.GetSize());
|
||||||
val.constant = NumToString(off.o);
|
val.constant = NumToString(off.o);
|
||||||
|
|
||||||
// Clean nested_parser before destruction to avoid deleting the elements in
|
|
||||||
// the SymbolTables
|
|
||||||
nested_parser.enums_.dict.clear();
|
|
||||||
nested_parser.enums_.vec.clear();
|
|
||||||
}
|
}
|
||||||
return NoError();
|
return NoError();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1996,6 +1996,26 @@ void ParseUnionTest() {
|
|||||||
true);
|
true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InvalidNestedFlatbufferTest() {
|
||||||
|
// First, load and parse FlatBuffer schema (.fbs)
|
||||||
|
std::string schemafile;
|
||||||
|
TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
|
||||||
|
false, &schemafile),
|
||||||
|
true);
|
||||||
|
auto include_test_path =
|
||||||
|
flatbuffers::ConCatPathFileName(test_data_path, "include_test");
|
||||||
|
const char *include_directories[] = { test_data_path.c_str(),
|
||||||
|
include_test_path.c_str(), nullptr };
|
||||||
|
flatbuffers::Parser parser1;
|
||||||
|
TEST_EQ(parser1.Parse(schemafile.c_str(), include_directories), true);
|
||||||
|
|
||||||
|
// "color" inside nested flatbuffer contains invalid enum value
|
||||||
|
TEST_EQ(parser1.Parse("{ name: \"Bender\", testnestedflatbuffer: { name: "
|
||||||
|
"\"Leela\", color: \"nonexistent\"}}"),
|
||||||
|
false);
|
||||||
|
// Check that Parser is destroyed correctly after parsing invalid json
|
||||||
|
}
|
||||||
|
|
||||||
void UnionVectorTest() {
|
void UnionVectorTest() {
|
||||||
// load FlatBuffer fbs schema.
|
// load FlatBuffer fbs schema.
|
||||||
// TODO: load a JSON file with such a vector when JSON support is ready.
|
// TODO: load a JSON file with such a vector when JSON support is ready.
|
||||||
@@ -2524,6 +2544,7 @@ int FlatBufferTests() {
|
|||||||
InvalidUTF8Test();
|
InvalidUTF8Test();
|
||||||
UnknownFieldsTest();
|
UnknownFieldsTest();
|
||||||
ParseUnionTest();
|
ParseUnionTest();
|
||||||
|
InvalidNestedFlatbufferTest();
|
||||||
ConformTest();
|
ConformTest();
|
||||||
ParseProtoBufAsciiTest();
|
ParseProtoBufAsciiTest();
|
||||||
TypeAliasesTest();
|
TypeAliasesTest();
|
||||||
|
|||||||
Reference in New Issue
Block a user