]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
Fix yet more UB. Provide explanatory comments.
authornotcancername <notcancername@protomail.com>
Mon, 31 Jul 2023 20:21:56 +0000 (22:21 +0200)
committerbones_was_here <bones_was_here@xonotic.au>
Sat, 28 Oct 2023 07:46:36 +0000 (17:46 +1000)
Signed-off-by: notcancername <notcancername@protomail.com>
cl_main.c
com_msg.c
r_shadow.c

index fe1840aff42db97cb43db2ec931f381da739312b..4794081cb0c115eb8b3eb7613f71911bca56c4a5 100644 (file)
--- 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");
 
index 1da366af24ac424708303469fc599f4421899a2e..80503b62b5307ff3d1ebf44ebf94da2aa02144e4 100644 (file)
--- 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;
 }
 
index 3f77792d7b111a8171c8cb14c629b4387c12946f..c80f0d7da57133924a66b73f634a035e7b59577e 100644 (file)
@@ -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)