Skip to content

Commit

Permalink
Correct buffer size for formatted net address with port
Browse files Browse the repository at this point in the history
- fixes "Com_sprintf: overflow of 48 bytes buffer" console spam on server listing
  • Loading branch information
f4rnham committed Feb 25, 2015
1 parent fe50af5 commit 7f2c27f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/engine/client/cl_main.cpp
Expand Up @@ -3079,7 +3079,7 @@ void CL_ServersResponsePacket( const netadr_t *from, msg_t *msg, qboolean extend
if ( addresses[ numservers ].port == cls.serverLinks[ j ].port4 && !memcmp( addresses[ numservers ].ip, cls.serverLinks[ j ].ip, 4 ) )
{
// found it, so look up the corresponding address
char s[ NET_ADDRSTRMAXLEN ];
char s[ NET_ADDR_W_PORT_STR_MAX_LEN ];

// hax to get the IP address & port as a string (memcmp etc. SHOULD work, but...)
cls.serverLinks[ j ].type = NA_IP6;
Expand Down Expand Up @@ -3131,7 +3131,7 @@ void CL_ServersResponsePacket( const netadr_t *from, msg_t *msg, qboolean extend
if ( addresses[ numservers ].port == cls.serverLinks[ j ].port6 && !memcmp( addresses[ numservers ].ip6, cls.serverLinks[ j ].ip6, 16 ) )
{
// found it, so look up the corresponding address
char s[ NET_ADDRSTRMAXLEN ];
char s[ NET_ADDR_W_PORT_STR_MAX_LEN ];

// hax to get the IP address & port as a string (memcmp etc. SHOULD work, but...)
cls.serverLinks[ j ].type = NA_IP;
Expand Down
10 changes: 5 additions & 5 deletions src/engine/qcommon/net_ip.cpp
Expand Up @@ -625,7 +625,7 @@ qboolean NET_CompareBaseAdr( netadr_t a, netadr_t b )

const char *NET_AdrToString( netadr_t a )
{
static char s[ NET_ADDRSTRMAXLEN ];
static char s[ NET_ADDR_STR_MAX_LEN ];

if ( a.type == NA_LOOPBACK )
{
Expand All @@ -649,7 +649,7 @@ const char *NET_AdrToString( netadr_t a )

const char *NET_AdrToStringwPort( netadr_t a )
{
static char s[ NET_ADDRSTRMAXLEN ];
static char s[ NET_ADDR_W_PORT_STR_MAX_LEN ];

if ( a.type == NA_LOOPBACK )
{
Expand All @@ -661,11 +661,11 @@ const char *NET_AdrToStringwPort( netadr_t a )
}
else if ( NET_IS_IPv4( a.type ) )
{
Com_sprintf( s, sizeof( s ), "%s:%lu", NET_AdrToString( a ), ( unsigned long ) ntohs( a.type == NA_IP_DUAL ? a.port4 : a.port ) );
Com_sprintf( s, sizeof( s ), "%s:%hu", NET_AdrToString( a ), ntohs( a.type == NA_IP_DUAL ? a.port4 : a.port ) );
}
else if ( NET_IS_IPv6( a.type ) )
{
Com_sprintf( s, sizeof( s ), "[%s]:%lu", NET_AdrToString( a ), ( unsigned long ) ntohs( a.type == NA_IP_DUAL ? a.port6 : a.port ) );
Com_sprintf( s, sizeof( s ), "[%s]:%hu", NET_AdrToString( a ), ntohs( a.type == NA_IP_DUAL ? a.port6 : a.port ) );
}
return s;
}
Expand Down Expand Up @@ -1031,7 +1031,7 @@ Sys_ShowIP
void Sys_ShowIP( void )
{
int i;
char addrbuf[ NET_ADDRSTRMAXLEN ];
char addrbuf[ NET_ADDR_STR_MAX_LEN ];

for ( i = 0; i < numIP; i++ )
{
Expand Down
8 changes: 7 additions & 1 deletion src/engine/qcommon/qcommon.h
Expand Up @@ -174,7 +174,13 @@ typedef enum
NS_SERVER
} netsrc_t;

#define NET_ADDRSTRMAXLEN 48 // maximum length of an IPv6 address string including trailing '\0'
// maximum length of an IPv6 address string including trailing '\0'
#define NET_ADDR_STR_MAX_LEN 48

// maximum length of an formatted IPv6 address string including port and trailing '\0'
// format [%s]:%hu - 48 for %s (address), 3 for []: and 5 for %hu (port number, max value 65535)
#define NET_ADDR_W_PORT_STR_MAX_LEN ( NET_ADDR_STR_MAX_LEN + 3 + 5 )

typedef struct
{
netadrtype_t type;
Expand Down

0 comments on commit 7f2c27f

Please sign in to comment.