From 1c963f0c1ebdf86d7cda9f3f2138e24b64154176 Mon Sep 17 00:00:00 2001 From: notcancername Date: Mon, 31 Jul 2023 22:21:56 +0200 Subject: [PATCH] Fix yet more UB. Provide explanatory comments. Signed-off-by: notcancername --- cl_main.c | 3 ++- com_msg.c | 11 +++++++---- r_shadow.c | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cl_main.c b/cl_main.c index fe1840af..4794081c 100644 --- a/cl_main.c +++ b/cl_main.c @@ -3112,7 +3112,8 @@ void CL_Init (void) // for QW connections Cvar_RegisterVariable(&qport); - Cvar_SetValueQuick(&qport, (rand() * RAND_MAX + rand()) & 0xffff); + // multiplying by RAND_MAX necessary for Windows, for which RAND_MAX is only 32767. + Cvar_SetValueQuick(&qport, ((unsigned int)rand() * RAND_MAX + (unsigned int)rand()) & 0xffff); Cmd_AddCommand(CF_CLIENT, "timerefresh", CL_TimeRefresh_f, "turn quickly and print rendering statistcs"); diff --git a/com_msg.c b/com_msg.c index 1da366af..80503b62 100644 --- a/com_msg.c +++ b/com_msg.c @@ -30,6 +30,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ============================================================================ */ +/* Casting to unsigned when shifting by 24 bits here is necessary to prevent UB + * caused by shifting outside the range of int on platforms where int is 32 bits. + */ float BuffBigFloat (const unsigned char *buffer) { @@ -287,7 +290,7 @@ int MSG_ReadLittleLong (sizebuf_t *sb) return -1; } sb->readcount += 4; - return sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24); + return sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | ((unsigned)sb->data[sb->readcount-1]<<24); } int MSG_ReadBigLong (sizebuf_t *sb) @@ -298,7 +301,7 @@ int MSG_ReadBigLong (sizebuf_t *sb) return -1; } sb->readcount += 4; - return (sb->data[sb->readcount-4]<<24) + (sb->data[sb->readcount-3]<<16) + (sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1]; + return ((unsigned)sb->data[sb->readcount-4]<<24) + (sb->data[sb->readcount-3]<<16) + (sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1]; } float MSG_ReadLittleFloat (sizebuf_t *sb) @@ -314,7 +317,7 @@ float MSG_ReadLittleFloat (sizebuf_t *sb) return -1; } sb->readcount += 4; - dat.l = sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24); + dat.l = sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | ((unsigned)sb->data[sb->readcount-1]<<24); return dat.f; } @@ -331,7 +334,7 @@ float MSG_ReadBigFloat (sizebuf_t *sb) return -1; } sb->readcount += 4; - dat.l = (sb->data[sb->readcount-4]<<24) | (sb->data[sb->readcount-3]<<16) | (sb->data[sb->readcount-2]<<8) | sb->data[sb->readcount-1]; + dat.l = ((unsigned)sb->data[sb->readcount-4]<<24) | (sb->data[sb->readcount-3]<<16) | (sb->data[sb->readcount-2]<<8) | sb->data[sb->readcount-1]; return dat.f; } diff --git a/r_shadow.c b/r_shadow.c index 3f77792d..c80f0d7d 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -1226,7 +1226,7 @@ static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z) float dist = sqrt(x*x+y*y+z*z); float intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0; // note this code could suffer byte order issues except that it is multiplying by an integer that reads the same both ways - return ((unsigned int)bound(0, intensity * 256.0f, 255)) * 0x01010101U; + return bound(0, (unsigned int)(intensity * 256.0f), 255) * 0x01010101U; } static void R_Shadow_MakeTextures(void) -- 2.39.2