Fix memory deallocation while yielding in redbean. (#384)

The yielded coroutine was removed from the stack too early,
leaving it not being anchored, which led to memory freed prematurely.
This commit is contained in:
Paul Kulchenko 2022-04-14 18:13:53 -07:00 committed by GitHub
parent fb7e8ef1e6
commit 233144b19d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 4 deletions

View file

@ -42,7 +42,10 @@ int LuaCallWithTrace(lua_State *L, int nargs, int nres, lua_State *C) {
lua_xmove(L, C, 1 + nargs);
// resume the coroutine thus executing the function
status = lua_resume(C, L, nargs, &nresults);
lua_remove(L, 1); // remove coroutine (still) at the bottom
// remove coroutine (still) at the bottom, but only if not yielding
// keep it when yielding to anchor, so it's not GC-collected
// it's going to be removed at the beggining of the request handling
if (!canyield) lua_remove(L, 1);
if (status != LUA_OK && status != LUA_YIELD) {
// move the error message
lua_xmove(C, L, 1);