From d9aa88e772a7a460272e3efc79f029b64ac18022 Mon Sep 17 00:00:00 2001 From: Varun Saini <61795485+vrn-sn@users.noreply.github.com> Date: Mon, 28 Apr 2025 11:15:43 -0700 Subject: [PATCH] Fix crash when `require` is called from root VM stack (#1788) Copied from #1785: > If require is called from the root interpreter stack (e.g. using C API) then lua_getinfo call will not succeed, leaving garbage in lua_Debug ar struct. > Accessing later ar.source as null-terminated string is unsafe and can cause a crash. > > This PR adds a check to ensure that lua_getinfo call is successful. Co-authored-by: Alex Orlenko --- Require/Runtime/src/RequireImpl.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Require/Runtime/src/RequireImpl.cpp b/Require/Runtime/src/RequireImpl.cpp index 30575964..5b0dd987 100644 --- a/Require/Runtime/src/RequireImpl.cpp +++ b/Require/Runtime/src/RequireImpl.cpp @@ -170,7 +170,8 @@ int lua_proxyrequire(lua_State* L) int lua_require(lua_State* L) { lua_Debug ar; - lua_getinfo(L, 1, "s", &ar); + if (!lua_getinfo(L, 1, "s", &ar)) + luaL_error(L, "require is not supported in this context"); return lua_requireinternal(L, ar.source); }