Search code examples
javaerror-handlingfortify

Error: the call causes portability problems because it has different locales


The full error: The call causes portability problems because it has different locales which may lead to unexpected output. This may also circumvent custom validation routines.

Running a tool over my code I get this issue depending on this part of my code:

 if ("paid".equals(type.toLowerCase())) {
        return PaymentType.PAID.getDescription();
    }

I don't really understand what the problem should be with this code here?


Solution

  • The toLowerCase() and toUpperCase() methods use the default locale. This can give different results depending on the locale and the characters in the string you're converting. It's kinda like the default encoding problem (except less likely to bite you in the ass, unless you're working with uncommon characters).

    You can avoid the warning by explicitly specifying toLowerCase(Locale.ENGLISH) or just use "paid".equalsIgnoreCase(type).