[idl_parser] Check the range of explicitly set field's id value (#6363)

* [idl_parser] Check the range of explicitly set field's id value

The explicitly set `id` attribute should be a non-negative value of the `voffset_t` type.

* Format FieldIdentifierTest()
This commit is contained in:
Vladimir Glavnyy
2021-01-05 03:33:31 +07:00
committed by GitHub
parent 24dd85fd28
commit e7430bbebd
2 changed files with 53 additions and 9 deletions

View File

@@ -3710,6 +3710,24 @@ void ParseFlexbuffersFromJsonWithNullTest() {
}
}
void FieldIdentifierTest() {
using flatbuffers::Parser;
TEST_EQ(true, Parser().Parse("table T{ f: int (id:0); }"));
// non-integer `id` should be rejected
TEST_EQ(false, Parser().Parse("table T{ f: int (id:text); }"));
TEST_EQ(false, Parser().Parse("table T{ f: int (id:\"text\"); }"));
TEST_EQ(false, Parser().Parse("table T{ f: int (id:0text); }"));
TEST_EQ(false, Parser().Parse("table T{ f: int (id:1.0); }"));
TEST_EQ(false, Parser().Parse("table T{ f: int (id:-1); g: int (id:0); }"));
TEST_EQ(false, Parser().Parse("table T{ f: int (id:129496726); }"));
// A unuion filed occupys two ids: enumerator + pointer (offset).
TEST_EQ(false,
Parser().Parse("union X{} table T{ u: X(id:0); table F{x:int;\n}"));
// Positive tests for unions
TEST_EQ(true, Parser().Parse("union X{} table T{ u: X (id:1); }"));
TEST_EQ(true, Parser().Parse("union X{} table T{ u: X; }"));
}
int FlatBufferTests() {
// clang-format off
@@ -3802,6 +3820,7 @@ int FlatBufferTests() {
ParseFlexbuffersFromJsonWithNullTest();
FlatbuffersSpanTest();
FixedLengthArrayConstructorTest();
FieldIdentifierTest();
return 0;
}