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 // little SFINAE trick to avoid base class
return Char_Parser_Helper<std::true_type>::u8str_from_ll(val); 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'));
}
}; };
} }
@ -2563,15 +2569,12 @@ namespace chaiscript
m_position = Position(t_input.begin(), t_input.end()); m_position = Position(t_input.begin(), t_input.end());
m_filename = std::make_shared<std::string>(std::move(t_fname)); m_filename = std::make_shared<std::string>(std::move(t_fname));
if ((t_input.size() > 1) && (t_input[0] == '#') && (t_input[1] == '!')) { if (detail::Char_Parser_Helper<std::string>::has_utf8_bom(t_input)) {
while (m_position.has_more() && (!Eol())) { throw exception::eval_error("UTF-8 in user provided input!");
++m_position;
}
} }
//skip UTF-8 BOM if ((t_input.size() > 1) && (t_input[0] == '#') && (t_input[1] == '!')) {
if ((t_input.size() > 3) && (t_input[0] == '\xef') && (t_input[1] == '\xbb' && t_input[2] == '\xbf')) { while (m_position.has_more() && (!Eol())) {
while(m_position.has_more() && (m_position.col < 4)) {
++m_position; ++m_position;
} }
} }

View File

@ -355,12 +355,7 @@ TEST_CASE("Functor cast")
TEST_CASE("BOM at beginning of string") TEST_CASE("BOM at beginning of string")
{ {
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
CHECK_THROWS_AS(chai.eval<std::string>("\xef\xbb\xbfprint \"Hello World\""), chaiscript::exception::eval_error);
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");
} }

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
}