Skip to content

Commit b90de30

Browse files
committed
Fix GCC -Wstringop-overflow warnings in Swap.h and SysCmds.cpp
Use if constexpr in idSwap::Big() so the compiler discards dead branches for mismatched sizeof(type), preventing false-positive out-of-bounds access warnings when inlined. Guard PrintFloat against negative snPrintf return value to prevent GCC from warning about out-of-bounds buf[] writes when inlined.
1 parent 4310fbd commit b90de30

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

neo/d3xp/gamesys/SysCmds.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,12 @@ static void PrintFloat( float f )
14371437
char buf[128];
14381438
int i;
14391439

1440-
for( i = idStr::snPrintf( buf, sizeof( buf ), "%3.2f", f ); i < 7; i++ )
1440+
i = idStr::snPrintf( buf, sizeof( buf ), "%3.2f", f );
1441+
if( i < 0 )
1442+
{
1443+
i = 0;
1444+
}
1445+
for( ; i < 7; i++ )
14411446
{
14421447
buf[i] = ' ';
14431448
}

neo/idlib/Swap.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@ class idSwap
8282
// byte swapping pointers is pointless because we should never store pointers on disk
8383
assert( !IsPointer( c ) );
8484

85-
if( sizeof( type ) == 1 )
85+
if constexpr( sizeof( type ) == 1 )
8686
{
8787
}
88-
else if( sizeof( type ) == 2 )
88+
else if constexpr( sizeof( type ) == 2 )
8989
{
9090
byte* b = ( byte* )&c;
9191
SwapBytes( b[0], b[1] );
9292
}
93-
else if( sizeof( type ) == 4 )
93+
else if constexpr( sizeof( type ) == 4 )
9494
{
9595
byte* b = ( byte* )&c;
9696
SwapBytes( b[0], b[3] );
9797
SwapBytes( b[1], b[2] );
9898
}
99-
else if( sizeof( type ) == 8 )
99+
else if constexpr( sizeof( type ) == 8 )
100100
{
101101
byte* b = ( byte* )&c;
102102
SwapBytes( b[0], b[7] );
@@ -106,7 +106,7 @@ class idSwap
106106
}
107107
else
108108
{
109-
assert( false );
109+
static_assert( sizeof( type ) == 0, "Unsupported type size for byte swapping" );
110110
}
111111
}
112112

0 commit comments

Comments
 (0)