Handle \u-escaped surrogate pairs correctly in IDL parser

This commit is contained in:
Ben Gertzfield
2016-04-28 12:27:38 -07:00
parent 208c15f29d
commit 6704b19db6
2 changed files with 82 additions and 2 deletions

View File

@@ -859,6 +859,44 @@ void UnicodeTest() {
"\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\"}", true);
}
void UnicodeSurrogatesTest() {
flatbuffers::Parser parser;
TEST_EQ(
parser.Parse(
"table T { F:string (id: 0); }"
"root_type T;"
"{ F:\"\\uD83D\\uDCA9\"}"), true);
auto root = flatbuffers::GetRoot<flatbuffers::Table>(
parser.builder_.GetBufferPointer());
auto string = root->GetPointer<flatbuffers::String *>(
flatbuffers::FieldIndexToOffset(0));
TEST_EQ(strcmp(string->c_str(), "\xF0\x9F\x92\xA9"), 0);
}
void UnicodeInvalidSurrogatesTest() {
TestError(
"table T { F:string; }"
"root_type T;"
"{ F:\"\\uD800\"}", "unpaired high surrogate");
TestError(
"table T { F:string; }"
"root_type T;"
"{ F:\"\\uD800abcd\"}", "unpaired high surrogate");
TestError(
"table T { F:string; }"
"root_type T;"
"{ F:\"\\uD800\\n\"}", "unpaired high surrogate");
TestError(
"table T { F:string; }"
"root_type T;"
"{ F:\"\\uD800\\uD800\"}", "multiple high surrogates");
TestError(
"table T { F:string; }"
"root_type T;"
"{ F:\"\\uDC00\"}", "unpaired low surrogate");
}
void UnknownFieldsTest() {
flatbuffers::IDLOptions opts;
opts.skip_unexpected_fields_in_json = true;
@@ -907,6 +945,8 @@ int main(int /*argc*/, const char * /*argv*/[]) {
EnumStringsTest();
IntegerOutOfRangeTest();
UnicodeTest();
UnicodeSurrogatesTest();
UnicodeInvalidSurrogatesTest();
UnknownFieldsTest();
if (!testing_fails) {