Skip to content

Off by one error in StringUtils.parseLocaleString [SPR-10364] #14996

@spring-projects-issues

Description

@spring-projects-issues

Zhihong Zhang opened SPR-10364 and commented

If you have locale "fr_fr", Spring will change it into "fr_fr_fr". The bug is in this method in StringUtils.java,

public static Locale parseLocaleString(String localeString) {
        String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
        String language = (parts.length > 0 ? parts[0] : "");
        String country = (parts.length > 1 ? parts[1] : "");
        validateLocalePart(language);
        validateLocalePart(country);
        String variant = "";
        if (parts.length >= 2) {
                // There is definitely a variant, and it is everything after the country
                // code sans the separator between the country code and the variant.
                int endIndexOfCountryCode = localeString.indexOf(country) + country.length();
                // Strip off any leading '_' and whitespace, what's left is the variant.
                variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
                if (variant.startsWith("_")) {
                        variant = trimLeadingCharacter(variant, '_');
                }
        }
        return (language.length() > 0 ? new Locale(language, country, variant) : null);
}

It assumes that language and country are never the same but in case of "fr_fr", they are the same.


Affects: 3.1 GA

Referenced from: commits e7f89f8

1 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions