Skip to content

Lua FFI fixes - #657

Open
CoffeeFlux wants to merge 12 commits into
TypesettingTools:masterfrom
CoffeeFlux:lua-ffi-fixes
Open

Lua FFI fixes#657
CoffeeFlux wants to merge 12 commits into
TypesettingTools:masterfrom
CoffeeFlux:lua-ffi-fixes

Conversation

@CoffeeFlux

Copy link
Copy Markdown
Member

No description provided.

clipboard_get returns strndup'd memory but typed it const char *, and
aegisub.internal.ffi's string helper deliberately only frees non-const
char * (const means the native side retains ownership). Type it char *
so the buffer is actually freed.
regex_compile strdups the exception text into the error out-param, but
the moon side read it with ffi.string, which does not free. Use
ffi_util.string, which does.
boost::regex can throw -- most notably on invalid UTF-8 via the
UTF-8-to-UTF-32 iterators (the crash backtrace in Aegisub#99),
and on regex complexity/backtracking limits -- but only regex_compile
caught anything. An exception unwinding through the LuaJIT interpreter
or JIT frames which called these functions is undefined behavior and
can take down the process.

Catch exceptions in regex_search/regex_match/regex_replace and report
them through a char **err out-param as lfs already does; the moon side
raises them as proper Lua errors.
On a bogus conversion the wrapper set the error out-param but then ran
the conversion anyway and returned a result, which the moon side's
error branch never freed. Return null once the error is set; the moon
side already handles a null result plus an error message correctly.
Previously OOM meant memcpy through a null pointer. The Lua side
already maps a null return to nil.
The impl functions return a C bool, which LuaJIT's ffi converts to a
Lua boolean, and tonumber(true) is nil -- so these four functions
returned nil, nil on success, indistinguishable from failure to any
caller checking the standard lfs true-or-nil,err convention. Found
because busted's own chdir call refuses to run the automation test
suite against the aegisub lfs.
error 2, err raises the number 2 with err as the level; failing to open
a directory should raise the error message.
It was only used to generate unique names under /tmp, which os.tmpname
does without an external rock (whose API has also drifted: uuid 1.0
refuses to run until an rng is explicitly configured). os.tmpname
creates the file on POSIX, so unlink it and use just the name.
Builds the standalone Lua host from automation/tests and registers the
busted suite as a meson test when busted is installed through luarocks
for Lua 5.1; nothing has been running these tests since Travis went
away. The suite's fixtures are POSIX-only, so it is only registered on
non-Windows hosts. Run with: meson test --suite automation
This has been institutional knowledge since the /MT era of the old
Visual Studio build broke automation (2016-2018); write it down where
the next person flipping b_vscrt will find it.
The test step only ran "gtest main" by name, so luajit-52 (and now the
automation suite) never ran in CI. Install busted through luarocks on
Ubuntu and macOS so the automation tests are registered; Windows keeps
skipping them since luarocks has no painless setup there and the meson
gate handles the absence.
@CoffeeFlux
CoffeeFlux force-pushed the lua-ffi-fixes branch 2 times, most recently from b3f261d to f49d812 Compare August 4, 2026 18:17
lfs.dir had no tests at all; the re specs pin that invalid UTF-8 is
reported as a Lua error, which is the path that previously crashed
(Aegisub#99); the unicode spec pins ICU's substitution
behavior for invalid input. Notably lfs.dir's error branch turns out
to be unreachable: DirectoryIterator swallows construction failures
on every platform and yields an empty iteration instead.
@CoffeeFlux
CoffeeFlux requested a review from arch1t3cht August 4, 2026 21:49
@CoffeeFlux
CoffeeFlux marked this pull request as ready for review August 4, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant