mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
Throw exception when user-provided input contains BOM.
This commit is contained in:
parent
1e8f7f9fa5
commit
efbebee9da
@ -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,19 +2569,16 @@ 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 (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] == '!')) {
|
if ((t_input.size() > 1) && (t_input[0] == '#') && (t_input[1] == '!')) {
|
||||||
while (m_position.has_more() && (!Eol())) {
|
while (m_position.has_more() && (!Eol())) {
|
||||||
++m_position;
|
++m_position;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//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 (Statements(true)) {
|
||||||
if (m_position.has_more()) {
|
if (m_position.has_more()) {
|
||||||
throw exception::eval_error("Unparsed input", File_Position(m_position.line, m_position.col), *m_filename);
|
throw exception::eval_error("Unparsed input", File_Position(m_position.line, m_position.col), *m_filename);
|
||||||
|
|||||||
@ -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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
2
unittests/eval_file_with_bom.chai
Normal file
2
unittests/eval_file_with_bom.chai
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
eval_file("file_with_bom.inc")
|
||||||
|
assert_true(alwaysTrue())
|
||||||
3
unittests/file_with_bom.inc
Normal file
3
unittests/file_with_bom.inc
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
def alwaysTrue() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user