From b949da291ee50509e6355bb0092d703151aa9061 Mon Sep 17 00:00:00 2001 From: mittorn Date: Sat, 21 Oct 2023 18:56:30 +0300 Subject: [PATCH] engine: fix strict aliasing issues found by an old armv7hf compiler --- engine/client/cl_main.c | 3 ++- engine/client/cl_parse.c | 2 +- engine/common/imagelib/img_utils.c | 14 ++++++++++---- engine/common/net_ws.c | 6 ++++-- engine/server/sv_custom.c | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/engine/client/cl_main.c b/engine/client/cl_main.c index e7a4c856..41ec4c0f 100644 --- a/engine/client/cl_main.c +++ b/engine/client/cl_main.c @@ -2247,7 +2247,8 @@ void CL_ReadNetMessage( void ) while( CL_GetMessage( net_message_buffer, &curSize )) { - if( cls.legacymode && *((int *)&net_message_buffer) == 0xFFFFFFFE ) + const int split_header = 0xFFFFFFFE; + if( cls.legacymode && !memcmp( &split_header, net_message_buffer, sizeof( split_header ))) // Will rewrite existing packet by merged if( !NetSplit_GetLong( &cls.netchan.netsplit, &net_from, net_message_buffer, &curSize ) ) continue; diff --git a/engine/client/cl_parse.c b/engine/client/cl_parse.c index a84734f4..8f0f7b89 100644 --- a/engine/client/cl_parse.c +++ b/engine/client/cl_parse.c @@ -1534,7 +1534,7 @@ void CL_SendConsistencyInfo( sizebuf_t *msg ) { case force_exactfile: MD5_HashFile( md5, filename, NULL ); - pc->value = *(int *)md5; + memcpy( &pc->value, md5, sizeof( pc->value )); if( user_changed_diskfile ) MSG_WriteUBitLong( msg, 0, 32 ); diff --git a/engine/common/imagelib/img_utils.c b/engine/common/imagelib/img_utils.c index a6cd189e..29107e65 100644 --- a/engine/common/imagelib/img_utils.c +++ b/engine/common/imagelib/img_utils.c @@ -284,8 +284,10 @@ int Image_ComparePalette( const byte *pal ) void Image_SetPalette( const byte *pal, uint *d_table ) { byte rgba[4]; + uint uirgba; // TODO: palette looks byte-swapped on big-endian int i; + // setup palette switch( image.d_rendermode ) { @@ -296,7 +298,8 @@ void Image_SetPalette( const byte *pal, uint *d_table ) rgba[1] = pal[i*3+1]; rgba[2] = pal[i*3+2]; rgba[3] = 0xFF; - d_table[i] = *(uint *)rgba; + memcpy( &uirgba, rgba, sizeof( uirgba )); + d_table[i] = uirgba; } break; case LUMP_GRADIENT: @@ -306,7 +309,8 @@ void Image_SetPalette( const byte *pal, uint *d_table ) rgba[1] = pal[766]; rgba[2] = pal[767]; rgba[3] = i; - d_table[i] = *(uint *)rgba; + memcpy( &uirgba, rgba, sizeof( uirgba )); + d_table[i] = uirgba; } break; case LUMP_MASKED: @@ -316,7 +320,8 @@ void Image_SetPalette( const byte *pal, uint *d_table ) rgba[1] = pal[i*3+1]; rgba[2] = pal[i*3+2]; rgba[3] = 0xFF; - d_table[i] = *(uint *)rgba; + memcpy( &uirgba, rgba, sizeof( uirgba )); + d_table[i] = uirgba; } d_table[255] = 0; break; @@ -327,7 +332,8 @@ void Image_SetPalette( const byte *pal, uint *d_table ) rgba[1] = pal[i*4+1]; rgba[2] = pal[i*4+2]; rgba[3] = pal[i*4+3]; - d_table[i] = *(uint *)rgba; + memcpy( &uirgba, rgba, sizeof( uirgba )); + d_table[i] = uirgba; } break; } diff --git a/engine/common/net_ws.c b/engine/common/net_ws.c index df34be20..b65ee41b 100644 --- a/engine/common/net_ws.c +++ b/engine/common/net_ws.c @@ -273,8 +273,10 @@ static void NET_NetadrToSockadr( netadr_t *a, struct sockaddr_storage *s ) } else if( a->type == NA_IP ) { + uint32_t ip; ((struct sockaddr_in *)s)->sin_family = AF_INET; - ((struct sockaddr_in *)s)->sin_addr.s_addr = *(uint32_t *)&a->ip; + memcpy( &ip, &a->ip, sizeof( ip )); + ((struct sockaddr_in *)s)->sin_addr.s_addr = ip; ((struct sockaddr_in *)s)->sin_port = a->port; } else if( a->type6 == NA_IP6 ) @@ -314,7 +316,7 @@ static void NET_SockadrToNetadr( const struct sockaddr_storage *s, netadr_t *a ) if( s->ss_family == AF_INET ) { a->type = NA_IP; - *(int *)&a->ip = ((struct sockaddr_in *)s)->sin_addr.s_addr; + memcpy( &a->ip, &((struct sockaddr_in *)s)->sin_addr.s_addr, sizeof( uint32_t )); a->port = ((struct sockaddr_in *)s)->sin_port; } else if( s->ss_family == AF_INET6 ) diff --git a/engine/server/sv_custom.c b/engine/server/sv_custom.c index cb034efb..588a0515 100644 --- a/engine/server/sv_custom.c +++ b/engine/server/sv_custom.c @@ -122,7 +122,7 @@ void SV_ParseConsistencyResponse( sv_client_t *cl, sizebuf_t *msg ) value = MSG_ReadUBitLong( msg, 32 ); // will be compare only first 4 bytes - if( value != *(int *)r->rgucMD5_hash ) + if( memcmp( &value , r->rgucMD5_hash, 4 )) badresindex = idx + 1; } else