From 4cd6d25ee680dfc128d2817625f5e3db228248d4 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Fri, 5 Jun 2026 08:42:46 +0500 Subject: [PATCH] engine: common: account for header size when sending first file fragment (which contains filename + compressor on goldsrc) --- engine/common/net_chan.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/engine/common/net_chan.c b/engine/common/net_chan.c index c9e96751..bb167d3c 100644 --- a/engine/common/net_chan.c +++ b/engine/common/net_chan.c @@ -1009,6 +1009,17 @@ int Netchan_CreateFileFragments( netchan_t *chan, const char *filename ) Mem_Free( uncompressed ); } + // filename string + null terminator; for gs_netchan also compressor string + null + uint32 original size + int header_size = Q_strlen( filename ) + 1; + if( chan->gs_netchan ) + header_size += Q_strlen( compressor ) + 1 + 4; + + if( unlikely( chunksize < 0 || chunksize < header_size + 1 )) + { + Con_Printf( S_ERROR "%s: could not fit header for \"%s\" (%d bytes) into chunk of length %d\n", NET_AdrToString( chan->remote_address ), filename, header_size, chunksize ); + return 0; + } + fragbufwaiting_t *wait = (fragbufwaiting_t *)Mem_Calloc( net_mempool, sizeof( fragbufwaiting_t )); int remaining = filesize; int pos = 0; @@ -1017,6 +1028,9 @@ int Netchan_CreateFileFragments( netchan_t *chan, const char *filename ) { int send = Q_min( remaining, chunksize ); + if( firstfragment ) + send = Q_min( header_size + remaining, chunksize ); + buf = Netchan_AllocFragbuf( send ); buf->bufferid = bufferid++; @@ -1025,7 +1039,7 @@ int Netchan_CreateFileFragments( netchan_t *chan, const char *filename ) if( firstfragment ) { - // Write filename + // write filename MSG_WriteString( &buf->frag_message, filename ); // write compressor name and uncompressed size @@ -1035,9 +1049,11 @@ int Netchan_CreateFileFragments( netchan_t *chan, const char *filename ) MSG_WriteLong( &buf->frag_message, (uint)originalSize ); } - // Send a bit less on first package - send -= MSG_GetNumBytesWritten( &buf->frag_message ); + if( unlikely( MSG_GetNumBytesWritten( &buf->frag_message ) != header_size )) + Con_Printf( S_ERROR "%s: header size mismatch for \"%s\" (%d vs %d)\n", NET_AdrToString( chan->remote_address ), filename, header_size, MSG_GetNumBytesWritten( &buf->frag_message )); + // send a bit less on first package + send -= MSG_GetNumBytesWritten( &buf->frag_message ); firstfragment = false; }