View Single Post
helloworlds
Junior Member
Join Date: Oct 2022
Old 11-08-2022 , 04:55   Re: SourcePawn compiler suggestion
Reply With Quote #7

Edit: answering my own question: there's a <testing> include in SourceMod I was completely unaware of that solves this.

PHP Code:
AssertEq("Array size mismatch"sizeof(foo), sizeof(bar)); 
___________

Quote:
Originally Posted by Psyk0tik View Post
Instead of asking for a feature to disable warnings (which are helpful for maintaining your code), why not actually fix the code to get rid of the warnings instead?
Ok.

PHP Code:
#include <sourcemod>

#define DEBUG true

char foo[123];
char bar[123];

public 
void CopyFooBar()
{
#if DEBUG
    // Checking for programmer error in DEBUG,
    // because these sizes *must* equal to avoid a truncated strcopy,
    // which would not trigger a compiler error but
    // silently introduce unintended behaviour at runtime.
    
if (sizeof(foo) != sizeof(bar)) // suppress-warning: 205: redundant code... if only
    
{
        
SetFailState("Array size mismatch");
    }
#endif
    
strcopy(foosizeof(foo), bar);

How would you fix this? I would rather not write warning comments near the stack allocations fingers crossed that the programmer read them, but rather have the program fail loudly.

In SourceMod pre-1.11, you could write
PHP Code:
#if sizeof(foo) != sizeof(bar)
#error You're doing it wrong!
#endif 
to manually fail the compile, but this behaviour has been sadly disabled in SM 1.11.

Last edited by helloworlds; 11-08-2022 at 05:12. Reason: solved
helloworlds is offline