18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-28 23:13:23 +01:00

fix(scripting): gsub with a metatable'd table, the trim idiom, the memory doc

- replacement_length() popped nothing after lua_getmetatable() pushed a metatable, so gsub got it
  as its 4th argument ("bad argument #4 to 'gsub'"); it is popped now.
- lua_pattern_cost(): a repetition followed by what always matches (as the first %s* of
  "^%s*(.-)%s*$") is tried once and doesn't multiply; a repetition of a character or a %class
  right before the final '$' counts the longest run of it in the subject, not the subject. The
  trim of a 10 KB line of words is allowed, a long run of spaces is still refused (it takes the
  square of the run). The doc names a trim that is linear on any string.
- the doc says every concatenation within one hook interval is made whole before the memory check.
This commit is contained in:
jerrrrycho
2026-11-17 09:10:08 +01:00
parent 8201ccb3b0
commit 178f5b63fe
6 changed files with 267 additions and 56 deletions

View File

@@ -942,8 +942,14 @@ std::optional<double> replacement_length(lua_State *L, int const Index, double c
// the matches don't overlap: a capture of each takes at most the subject; a position capture is a number
return Matches * static_cast<double>(length) + captures * (Subject + 24.0 * Matches);
}
if (lua_type(L, Index) == LUA_TTABLE && 0 == lua_getmetatable(L, Index))
if (lua_type(L, Index) == LUA_TTABLE)
{
if (lua_getmetatable(L, Index) != 0)
{
// pushed: off the stack again, or the library gets it as the 4th argument
lua_pop(L, 1);
return std::nullopt;
}
auto longest{0.0};
lua_pushnil(L);
while (lua_next(L, Index) != 0)
@@ -964,11 +970,11 @@ int api_pattern(lua_State *L)
auto const function{static_cast<pattern_function>(lua_tointeger(L, lua_upvalueindex(2)))};
auto const *name{pattern_function_names[static_cast<std::size_t>(function)]};
std::size_t subject{0};
luaL_checklstring(L, 1, &subject);
auto const *text{luaL_checklstring(L, 1, &subject)};
std::size_t length{0};
auto const *pattern{luaL_checklstring(L, 2, &length)};
auto const plain{function == pattern_function::find && lua_toboolean(L, 4) != 0};
if (lua_pattern_cost({pattern, length}, subject, plain, function != pattern_function::gmatch) > max_pattern_cost)
if (lua_pattern_cost(std::string_view{pattern, length}, std::string_view{text, subject}, plain, function != pattern_function::gmatch) > max_pattern_cost)
{
return luaL_error(L, "%s: the pattern may take too long on a subject of %d bytes", name, static_cast<int>(subject));
}