nfqws2: improve ctx magic protection

This commit is contained in:
bol-van committed 2025-12-16 16:00:29 +03:00
1 parent f76beba434
commit b9a0d42815
1 file changed
+8 -2
+8 -2
View File
@@ -710,7 +710,7 @@ static uint8_t desync(
struct func_list *func;
int ref_arg = LUA_NOREF, status;
bool b, b_cutoff_all, b_unwanted_payload;
t_lua_desync_context ctx = { .magic = MAGIC_CTX, .dp = dp, .ctrack = ctrack, .dis = dis, .cancel = false, .incoming = bIncoming };
t_lua_desync_context ctx = { .magic = 0, .dp = dp, .ctrack = ctrack, .dis = dis, .cancel = false, .incoming = bIncoming };
const char *sDirection = bIncoming ? "in" : "out";
struct packet_range *range;
size_t l;
@@ -860,8 +860,14 @@ static uint8_t desync(
lua_pushf_str("func", func->func);
lua_pushf_int("func_n", ctx.func_n);
lua_pushf_str("func_instance", instance);
int initial_stack_top = lua_gettop(params.L);
// lua should not store and access ctx outside of this call
// if this happens make our best to prevent access to bad memory
// this is not crash-proof but better than nothing
ctx.magic = MAGIC_CTX; // mark struct as valid
status = lua_pcall(params.L, 2, LUA_MULTRET, 0);
ctx.magic = 0; // mark struct as invalid
if (status)
{
lua_dlog_error();