Edit

Share via


Warning C6401

Using 'function name' in a default locale to perform a case-insensitive compare to constant string 'string name'. Yields unexpected results in non-English locales

Remarks

This warning indicates that a case-insensitive comparison to a constant string is being done when specifying the default locale. Usually, a locale-independent comparison was intended.

The typical consequence of this defect is incorrect behavior in non-English speaking locales. For example, in Turkish, ".gif" won't match ".GIF"; in Vietnamese, "LookUp" won't match "LOOKUP".

The CompareString function takes a locale as an argument; however, passing in a default locale, for example, the constant LOCALE_USER_DEFAULT, will cause different behaviors in different locales, depending on the user's default. Usually, case-insensitive comparisons against a constant string should be performed in a locale-independent comparison.

To perform a locale-independent comparison using CompareString on Windows XP, the first parameter should be the constant LOCALE_INVARIANT; for example, to perform a locale-independent test for whether pString matches file1.gif ignoring upper/lower case differences, use a call such as:

CompareString(LOCALE_INVARIANT,
              NORM_IGNORECASE,
              pString,
              -1,
              TEXT("file1.gif"),
              -1) == CSTR_EQUAL

Code analysis name: DEFAULT_LOCALE_CONSTANT_STRING_COMPARISON

Example

The following code generates this warning:

include <windows.h>

int fd(char *ext)
{
  return (CompareString(LOCALE_USER_DEFAULT,
                        NORM_IGNORECASE,
                        ext,
                        -1,
                        TEXT("gif"),
                        -1) == 2);
}

To correct this warning, use the following code:

include <windows.h>
int f(char *ext)
{
  return (CompareString(LOCALE_INVARIANT,
                        NORM_IGNORECASE,
                        ext,
                        -1,
                        TEXT("gif"),
                        -1) == 2);
}

See also

CompareString