From cbe8747b59d143ad2bfe73ecc838b711f8102886 Mon Sep 17 00:00:00 2001 From: Oli Wilkinson Date: Mon, 18 Jan 2016 18:58:53 +0000 Subject: [PATCH] Added check (& skipping) of the utf-8 byte order mark (0xEF BB BF) at the beginning of the file --- include/flatbuffers/idl.h | 1 + src/idl_parser.cpp | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index d052d6f4d..6708f1382 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -444,6 +444,7 @@ private: FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg); FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, int64_t *val); FLATBUFFERS_CHECKED_ERROR Next(); + FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark(); bool Is(int t); FLATBUFFERS_CHECKED_ERROR Expect(int t); std::string TokenToStringId(int t); diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index e4013e8d4..96ea012be 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -200,6 +200,14 @@ CheckedError Parser::ParseHexNum(int nibbles, int64_t *val) { return NoError(); } +CheckedError Parser::SkipByteOrderMark() { + if (static_cast(*cursor_) != 0xef) return NoError(); + cursor_++; + if (static_cast(*cursor_++) != 0xbb) return Error("invalid utf-8 byte order mark"); + if (static_cast(*cursor_++) != 0xbf) return Error("invalid utf-8 byte order mark"); + return NoError(); +} + CheckedError Parser::Next() { doc_comment_.clear(); bool seen_newline = false; @@ -1584,6 +1592,7 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, builder_.Clear(); // Start with a blank namespace just in case this file doesn't have one. namespaces_.push_back(new Namespace()); + ECHECK(SkipByteOrderMark()); NEXT(); // Includes must come before type declarations: for (;;) {