From ed1794fe172c209c0c354a085c3e995e9c4462fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Tue, 16 Jan 2018 16:04:17 +0000 Subject: [PATCH] Change shift expressions to unsigned types. Shifting signed values to a result that is not representable has undefined behaviour. --- code/client/cl_main.c | 4 ++-- code/qcommon/md4.c | 7 +++++-- code/qcommon/net_chan.c | 2 +- code/qcommon/vm_x86.c | 2 +- code/server/sv_client.c | 2 +- code/server/sv_init.c | 2 +- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/code/client/cl_main.c b/code/client/cl_main.c index fde9fa10..59be427e 100644 --- a/code/client/cl_main.c +++ b/code/client/cl_main.c @@ -1539,7 +1539,7 @@ void CL_RequestMotd( void ) { info[0] = 0; - Com_sprintf( cls.updateChallenge, sizeof( cls.updateChallenge ), "%i", ((rand() << 16) ^ rand()) ^ Com_Milliseconds()); + Com_sprintf( cls.updateChallenge, sizeof( cls.updateChallenge ), "%i", (int)((((unsigned int)rand() << 16) ^ (unsigned int)rand()) ^ Com_Milliseconds())); Info_SetValueForKey( info, "challenge", cls.updateChallenge ); Info_SetValueForKey( info, "renderer", cls.glconfig.renderer_string ); @@ -1768,7 +1768,7 @@ void CL_Connect_f( void ) { clc.state = CA_CONNECTING; // Set a client challenge number that ideally is mirrored back by the server. - clc.challenge = ((rand() << 16) ^ rand()) ^ Com_Milliseconds(); + clc.challenge = (((unsigned int)rand() << 16) ^ (unsigned int)rand()) ^ Com_Milliseconds(); } Key_SetCatcher( 0 ); diff --git a/code/qcommon/md4.c b/code/qcommon/md4.c index 0eb23a50..abb951c8 100644 --- a/code/qcommon/md4.c +++ b/code/qcommon/md4.c @@ -106,8 +106,11 @@ static void copy64(uint32_t *M, byte *in) int i; for (i=0;i<16;i++) - M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) | - (in[i*4+1]<<8) | (in[i*4+0]<<0); + M[i] = + ((uint32_t)in[i*4+3] << 24) | + ((uint32_t)in[i*4+2] << 16) | + ((uint32_t)in[i*4+1] << 8) | + ((uint32_t)in[i*4+0] << 0) ; } static void copy4(byte *out,uint32_t x) diff --git a/code/qcommon/net_chan.c b/code/qcommon/net_chan.c index 0b099b95..9e7d9b87 100644 --- a/code/qcommon/net_chan.c +++ b/code/qcommon/net_chan.c @@ -52,7 +52,7 @@ to the new value before sending out any replies. #define FRAGMENT_SIZE (MAX_PACKETLEN - 100) #define PACKET_HEADER 10 // two ints and a short -#define FRAGMENT_BIT (1<<31) +#define FRAGMENT_BIT (1U<<31) cvar_t *showpackets; cvar_t *showdrop; diff --git a/code/qcommon/vm_x86.c b/code/qcommon/vm_x86.c index 2435a6e9..6bf349f2 100644 --- a/code/qcommon/vm_x86.c +++ b/code/qcommon/vm_x86.c @@ -103,7 +103,7 @@ static int isu8(uint32_t v) static int NextConstant4(void) { - return (code[pc] | (code[pc+1]<<8) | (code[pc+2]<<16) | (code[pc+3]<<24)); + return ((unsigned int)code[pc] | ((unsigned int)code[pc+1]<<8) | ((unsigned int)code[pc+2]<<16) | ((unsigned int)code[pc+3]<<24)); } static int Constant4( void ) { diff --git a/code/server/sv_client.c b/code/server/sv_client.c index ad759eff..d2950172 100644 --- a/code/server/sv_client.c +++ b/code/server/sv_client.c @@ -140,7 +140,7 @@ void SV_GetChallenge(netadr_t from) } // always generate a new challenge number, so the client cannot circumvent sv_maxping - challenge->challenge = ( (rand() << 16) ^ rand() ) ^ svs.time; + challenge->challenge = ( ((unsigned int)rand() << 16) ^ (unsigned int)rand() ) ^ svs.time; challenge->wasrefused = qfalse; challenge->time = svs.time; diff --git a/code/server/sv_init.c b/code/server/sv_init.c index aba0e8db..51f228fd 100644 --- a/code/server/sv_init.c +++ b/code/server/sv_init.c @@ -468,7 +468,7 @@ void SV_SpawnServer( char *server, qboolean killBots ) { Cvar_Set("cl_paused", "0"); // get a new checksum feed and restart the file system - sv.checksumFeed = ( ((int) rand() << 16) ^ rand() ) ^ Com_Milliseconds(); + sv.checksumFeed = ( ((unsigned int)rand() << 16) ^ (unsigned int)rand() ) ^ Com_Milliseconds(); FS_Restart( sv.checksumFeed ); CM_LoadMap( va("maps/%s.bsp", server), qfalse, &checksum );