ValveSoftware / source-sdk-2013

The 2013 edition of the Source SDK
https://developer.valvesoftware.com/wiki/SDK2013_GettingStarted
Other
3.84k stars 2.02k forks source link

Fix entity name matching #498

Open TotallyMehis opened 4 years ago

TotallyMehis commented 4 years ago

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 );
            }
        }
    }
}