Fix load_string() ignoring the passed chunk name (#6794)

Complete with a unit test!
The test results in PASS_BY_VICTORY if it fails,
which is a bit weird, but it does work.
This commit is contained in:
Celtic Minstrel 2022-06-24 01:49:28 -04:00 committed by GitHub
parent 28831fe1dd
commit 666571caa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 1 deletions

View file

@ -0,0 +1,18 @@
# Test that an error message in Lua includes the value of the tag's name key
{GENERIC_UNIT_TEST test_lua_name (
[event]
name=start
[lua]
name=test_lua_name
code=<<
-- Can't use assert_throws_with here because we want to test the preamble that's automatically prefixed to the error.
local ret, err = pcall(function()
local some_variable = bar
end)
unit_test.assert_contains(err, 'test_lua_name', 'error contains name')
unit_test.succeed()
>>
[/lua]
[/event]
)}

View file

@ -1021,7 +1021,7 @@ bool lua_kernel_base::protected_call(lua_State * L, int nArgs, int nRets, error_
bool lua_kernel_base::load_string(char const * prog, const std::string& name, error_handler e_h)
{
// pass 't' to prevent loading bytecode which is unsafe and can be used to escape the sandbox.
int errcode = luaL_loadbufferx(mState, prog, strlen(prog), name.empty() ? name.c_str() : prog, "t");
int errcode = luaL_loadbufferx(mState, prog, strlen(prog), name.empty() ? prog : name.c_str(), "t");
if (errcode != LUA_OK) {
char const * msg = lua_tostring(mState, -1);
std::string message = msg ? msg : "null string";

View file

@ -139,6 +139,7 @@
0 test_ability_id_active
0 test_ability_id_not_active
0 event_test_filter_attack
0 test_lua_name
0 filter_vision
0 test_shroud_init
0 test_shroud_place_wml