From d6e77e531dabcb76c8bead6e7a1394498643a944 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 3 Aug 2024 08:40:31 +0300 Subject: [PATCH] engine: fix memory leak in netchan frag_message_buf wasn't always freed alongside with fragbuf_t instead of ensuring frag_message_buf being freed, I turned it into a flexible array member --- engine/common/net_chan.c | 6 ++---- engine/common/netchan.h | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/engine/common/net_chan.c b/engine/common/net_chan.c index a092f8a9..45993c3a 100644 --- a/engine/common/net_chan.c +++ b/engine/common/net_chan.c @@ -379,7 +379,6 @@ static void Netchan_ClearFragbufs( fragbuf_t **ppbuf ) while( buf ) { n = buf->next; - Mem_Free( buf->frag_message_buf ); Mem_Free( buf ); buf = n; } @@ -505,8 +504,7 @@ static fragbuf_t *Netchan_AllocFragbuf( int fragment_size ) { fragbuf_t *buf; - buf = (fragbuf_t *)Mem_Calloc( net_mempool, sizeof( fragbuf_t )); - buf->frag_message_buf = (byte *)Mem_Calloc( net_mempool, fragment_size ); + buf = (fragbuf_t *)Mem_Calloc( net_mempool, sizeof( fragbuf_t ) + ( fragment_size - 1 ) ); MSG_Init( &buf->frag_message, "Frag Message", buf->frag_message_buf, fragment_size ); return buf; @@ -520,7 +518,7 @@ Netchan_AddFragbufToTail */ static void Netchan_AddFragbufToTail( fragbufwaiting_t *wait, fragbuf_t *buf ) { - fragbuf_t *p; + fragbuf_t *p; buf->next = NULL; wait->fragbufcount++; diff --git a/engine/common/netchan.h b/engine/common/netchan.h index a1c3cbd3..0c2a76ea 100644 --- a/engine/common/netchan.h +++ b/engine/common/netchan.h @@ -184,13 +184,13 @@ typedef struct fragbuf_s struct fragbuf_s *next; // next buffer in chain int bufferid; // id of this buffer sizebuf_t frag_message; // message buffer where raw data is stored - byte *frag_message_buf; // the actual data sits here qboolean isfile; // is this a file buffer? qboolean isbuffer; // is this file buffer from memory ( custom decal, etc. ). qboolean iscompressed; // is compressed file, we should using filename.ztmp char filename[MAX_OSPATH]; // name of the file to save out on remote host int foffset; // offset in file from which to read data int size; // size of data to read at that offset + byte frag_message_buf[1]; // the actual data sits here (flexible) } fragbuf_t; // Waiting list of fragbuf chains