Files
ROSS/Src/Common/globals.cpp
T
ThomasandClaude Sonnet 5 5f600d0e61 Fix memory leaks and add bounds checks in packet/drop handling
GetNewDrop() allocated newdrop up front but returned NULL without
freeing it on every rejection path (failed drop-rate check, missing
drop table, unknown item type, etc.), leaking on every non-drop roll.
Free it on all early-return paths, and only assign a clientid once a
drop is actually going to be returned.

CPacket::AddByte/Word/DWord/QWord/Float now bounds-check against the
buffer before writing, preventing an overflow if a packet is built
past its allocated size.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-09 06:35:18 +02:00

97 lines
2.3 KiB
C++

#include "sockets.h"
CPacket::CPacket( unsigned short mycommand, unsigned short mysize, unsigned short myunused )
{
Command = mycommand;
Size = mysize;
Unused = myunused;
}
CPacket::~CPacket( ) { }
void CPacket::StartPacket( unsigned short mycommand, unsigned short mysize, unsigned short myunused )
{
Command = mycommand;
Size = mysize;
Unused = myunused;
}
void CPacket::AddByte( unsigned char value )
{
if ( Size < 6 || Size-6 >= sizeof(Buffer) ) return;
Buffer[Size-6] = value;
Size += 1;
}
void CPacket::AddWord( unsigned short value )
{
if ( Size < 6 || Size-6+2 > sizeof(Buffer) ) return;
*((unsigned short*)&Buffer[Size-6]) = value;
Size += 2;
}
void CPacket::AddDWord( unsigned value )
{
if ( Size < 6 || Size-6+4 > sizeof(Buffer) ) return;
*((unsigned*)&Buffer[Size-6]) = value;
Size += 4;
}
void CPacket::AddQWord( unsigned long long value )
{
if ( Size < 6 || Size-6+8 > sizeof(Buffer) ) return;
*((unsigned long long*)&Buffer[Size-6]) = value;
Size += 8;
}
void CPacket::AddFloat( float value )
{
if ( Size < 6 || Size-6+4 > sizeof(Buffer) ) return;
*((float*)&Buffer[Size-6]) = value;
Size += 4;
}
void CPacket::AddString( char* value )
{
for(unsigned c=0; c<strlen(value); c++)
AddByte( value[c] );
}
void CPacket::SetByte( unsigned short pos, unsigned char value )
{
Buffer[pos] = value;
}
void CPacket::SetWord( unsigned short pos, unsigned short value )
{
*((unsigned short*)&Buffer[pos]) = value;
}
void CPacket::SetDWord( unsigned short pos, unsigned value )
{
*((unsigned*)&Buffer[pos]) = value;
}
void CPacket::SetQWord( unsigned short pos, unsigned long long value )
{
*((unsigned long long*)&Buffer[pos]) = value;
}
void CPacket::SetFloat( unsigned short pos, float value )
{
*((float*)&Buffer[pos]) = value;
}
unsigned char CPacket::GetByte( unsigned short pos )
{
return Buffer[pos];
}
unsigned short CPacket::GetWord( unsigned short pos )
{
return *((unsigned short*)&Buffer[pos]);
}
unsigned CPacket::GetDWord( unsigned short pos )
{
return *((unsigned*)&Buffer[pos]);
}
unsigned long long CPacket::GetQWord( unsigned short pos )
{
return *((unsigned long long*)&Buffer[pos]);
}
float CPacket::GetFloat( unsigned short pos )
{
return *((float*)&Buffer[pos]);
}
char* CPacket::GetString( unsigned short pos )
{
return (char*)&Buffer[pos];
}