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

fix(scripting): close the small holes in the script screen sandbox

- Lua: once the instruction budget of a call is spent, the count hook
  runs at every instruction and raises again, so pcall, nested pcall and
  coroutine.resume can't swallow the limit; xpcall is rebuilt on pcall
  so its handler runs after the error left f, while the hook works
- Lua: the metatables of eu07.image/font/sprite are locked
  (__metatable = false); __gc destroys only an object of its own type,
  once
- Lua: read_png checks the size with stbi_info before decoding
- Lua: string.rep makes no string over 1 MiB
- Lua and WASM: at most 64 commands per call, names up to 64 bytes,
  32 log lines per call cut at 1024 bytes; the drops are logged once
- fonts: a glyph wider than 4096 is skipped; more than 4096 glyphs,
  8192 columns or an image side over 4096 refuse the font; cut_glyph
  reads only the columns inside the image
- the script path from the .mmd (cab screens, destination signs,
  make: textures) must stay inside the game folder; the files a screen
  reads, its script and its module must be regular files of 16 MiB at
  most

Tests for each in test_script_screen_host.cpp, test_script_screen_drawing.cpp
and test_script_screens.cpp; the test stubs can capture the log.
This commit is contained in:
jerrrrycho
2026-11-13 14:25:18 +01:00
parent 39e8bcc59d
commit 1bd7187db7
14 changed files with 750 additions and 54 deletions

View File

@@ -64,13 +64,11 @@ bool lit_pixel(std::span<std::uint8_t const> const Rgba, int const Width, int co
glyph cut_glyph(std::span<std::uint8_t const> const Rgba, int const Width, int const Height, int const First, int const Count)
{
std::vector<bool> lit(static_cast<std::size_t>(Count) * Height, false);
for (auto column = 0; column < Count; ++column)
// only the columns inside the image are read
auto const last{static_cast<int>(std::min<std::int64_t>(static_cast<std::int64_t>(First) + Count, Width))};
for (auto x = std::max(First, 0); x < last; ++x)
{
auto const x{First + column};
if (x < 0 || x >= Width)
{
continue;
}
auto const column{x - First};
for (auto y = 0; y < Height; ++y)
{
lit[static_cast<std::size_t>(column) * Height + y] = lit_pixel(Rgba, Width, x, y);
@@ -128,14 +126,15 @@ std::u32string decode_utf8(std::string_view const Text)
std::optional<bitmap_font> bitmap_font::parse(std::span<std::uint8_t const> const Rgba, int const Width, int const Height, std::string_view const Description)
{
if (Width <= 0 || Height <= 0 || Rgba.size() < static_cast<std::size_t>(Width) * Height * 4)
if (Width <= 0 || Height <= 0 || Width > max_font_image_side || Height > max_font_image_side || Rgba.size() < static_cast<std::size_t>(Width) * Height * 4)
{
return std::nullopt;
}
bitmap_font font;
font.m_height = Height;
auto const all{lines(Description)};
auto x{0};
auto x{0}; // where the glyph starts: at most max_font_columns
auto glyphs{0};
// the first line is a comment
for (std::size_t line = 1; line < all.size(); ++line)
{
@@ -157,10 +156,14 @@ std::optional<bitmap_font> bitmap_font::parse(std::span<std::uint8_t const> cons
digits.remove_suffix(1);
}
auto width{0};
if (std::from_chars(digits.data(), digits.data() + digits.size(), width).ec != std::errc{} || width < 0)
if (std::from_chars(digits.data(), digits.data() + digits.size(), width).ec != std::errc{} || width < 0 || width > max_font_image_side)
{
continue;
}
if (++glyphs > max_font_glyphs || x + width > max_font_columns)
{
return std::nullopt;
}
auto cut{cut_glyph(Rgba, Width, Height, x, width)};
if (font.m_glyphs.empty())
{