From 75f3c7eac3f9173647b703ce0e7632a7acff5309 Mon Sep 17 00:00:00 2001 From: bol-van Date: Tue, 6 Jan 2026 22:14:53 +0300 Subject: [PATCH] nfqws2: free zlib stream in __gc --- nfq2/lua.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/nfq2/lua.c b/nfq2/lua.c index b1d3c7f..f282603 100644 --- a/nfq2/lua.c +++ b/nfq2/lua.c @@ -2596,6 +2596,30 @@ struct userdata_zs bool valid, inflate; z_stream zs; }; +static int lua_cfunc_zstream_gc(lua_State *L) +{ + struct userdata_zs *uzs = (struct userdata_zs *)luaL_checkudata(L, 1, "userdata_zstream"); + if (uzs->valid) + { + if (uzs->inflate) + inflateEnd(&uzs->zs); + else + deflateEnd(&uzs->zs); + uzs->valid = false; + } + return 0; +} +static void lua_mt_init_zstream() +{ + LUA_STACK_GUARD_ENTER(params.L) + + luaL_newmetatable(params.L, "userdata_zstream"); + lua_pushcfunction(params.L, lua_cfunc_zstream_gc); + lua_setfield(params.L, -2, "__gc"); + lua_pop(params.L,1); + + LUA_STACK_GUARD_LEAVE(params.L, 0) +} static struct userdata_zs *lua_uzs(int idx, bool bInflate) { struct userdata_zs *uzs = (struct userdata_zs *)luaL_checkudata(params.L, idx, "userdata_zstream"); @@ -2620,7 +2644,7 @@ static int luacall_gunzip_init(lua_State *L) { uzs->inflate = true; uzs->valid = true; - luaL_newmetatable(L, "userdata_zstream"); + luaL_getmetatable(L, "userdata_zstream"); lua_setmetatable(L, -2); } else @@ -2889,6 +2913,8 @@ static bool lua_basic_init() } lua_settop(params.L, 0); + lua_mt_init_zstream(); + return true; }