Throw exception when user-provided input contains BOM.

This commit is contained in:
Alek Mosingiewicz 2018-05-13 10:25:04 +02:00
parent 1e8f7f9fa5
commit efbebee9da
4 changed files with 16 additions and 13 deletions

View File

@ -114,6 +114,12 @@ namespace chaiscript
// little SFINAE trick to avoid base class
return Char_Parser_Helper<std::true_type>::u8str_from_ll(val);
}
static bool has_utf8_bom(const std::string &t_input)
{
//skip UTF-8 BOM
return ((t_input.size() > 3) && (t_input[0] == '\xef') && (t_input[1] == '\xbb' && t_input[2] == '\xbf'));
}
};
}
@ -2562,6 +2568,10 @@ namespace chaiscript
AST_NodePtr parse_internal(const std::string &t_input, std::string t_fname) {
m_position = Position(t_input.begin(), t_input.end());
m_filename = std::make_shared<std::string>(std::move(t_fname));
if (detail::Char_Parser_Helper<std::string>::has_utf8_bom(t_input)) {
throw exception::eval_error("UTF-8 in user provided input!");
}
if ((t_input.size() > 1) && (t_input[0] == '#') && (t_input[1] == '!')) {
while (m_position.has_more() && (!Eol())) {
@ -2569,13 +2579,6 @@ namespace chaiscript
}
}
//skip UTF-8 BOM
if ((t_input.size() > 3) && (t_input[0] == '\xef') && (t_input[1] == '\xbb' && t_input[2] == '\xbf')) {
while(m_position.has_more() && (m_position.col < 4)) {
++m_position;
}
}
if (Statements(true)) {
if (m_position.has_more()) {
throw exception::eval_error("Unparsed input", File_Position(m_position.line, m_position.col), *m_filename);

View File

@ -355,12 +355,7 @@ TEST_CASE("Functor cast")
TEST_CASE("BOM at beginning of string")
{
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
chai.add(chaiscript::fun(&functor_cast_test_call), "test_call");
chai.eval("def func() { return \"Hello World\"; };");
CHECK(chai.eval<std::string>("\xef\xbb\xbf(func())") == "Hello World");
CHECK_THROWS_AS(chai.eval<std::string>("\xef\xbb\xbfprint \"Hello World\""), chaiscript::exception::eval_error);
}

View File

@ -0,0 +1,2 @@
eval_file("file_with_bom.inc")
assert_true(alwaysTrue())

View File

@ -0,0 +1,3 @@
def alwaysTrue() {
return true
}