source-sdk-2013 icon indicating copy to clipboard operation
source-sdk-2013 copied to clipboard

Fix entity name matching

Open TotallyMehis opened this issue 5 years ago • 0 comments

Entity name matching is broken with certain characters. For example, character 'X' will match to '8'. This will have unintended side-effects in the map logic. It worked fine in Source SDK 2006 because the comparison used tolower. Problems caused by this is quite rare due to the fact that most maps use numbers and lowercase letters only.

Example of the problem:

#include <cstdio>

int main()
{
    auto badmatch = []( unsigned char cName, unsigned char cQuery ) {
        /* */if ( cName - 'A' <= (unsigned char)'Z' - 'A' && cName - 'A' + 'a' == cQuery )
            return true;
        else if ( cName - 'a' <= (unsigned char)'z' - 'a' && cName - 'a' + 'A' == cQuery )
            return true;
            
        return false;
    };
    
    auto goodmatch = []( unsigned char cName, unsigned char cQuery ) {
        /* */if ( (unsigned char)(cName - 'A') <= (unsigned char)('Z' - 'A') && (unsigned char)(cName - 'A' + 'a') == cQuery )
            return true;
        else if ( (unsigned char)(cName - 'a') <= (unsigned char)('z' - 'a') && (unsigned char)(cName - 'a' + 'A') == cQuery )
            return true;
            
        return false;
    };
    
    // Loop through all characters.
    for ( auto i = 0; i <= 255; i++ )
    {
        auto cQuery = (unsigned char)i;
        for ( auto j = 0; j <= 255; j++ )
        {
            auto cName = (unsigned char)j;
            
            if ( cName == cQuery )
                continue;
            
            if ( badmatch( cName, cQuery ) )
            //if ( goodmatch( cName, cQuery ) )
            {
                printf("%c (%x) matches %c (%x)\n", cName, cName, cQuery, cQuery );
            }
        }
    }
}

TotallyMehis avatar Jun 22 '20 13:06 TotallyMehis