Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Mar 8, 2013 · 3 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

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

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Mar 10, 2013

Phil Webb commented

Have you tried the latest Spring 3.2 release? It looks like this bug might be a duplicate of #14056.

@spring-projects-issues
Copy link
Collaborator Author

Zhihong Zhang commented

Just checked lastest release v3.2.1.RELEASE and the bug is fixed.

However, the code still has a logic bug. This condition is wrong,

if (parts.length >= 2) {

It should be

if (parts.length > 2) {

When there are only 2 parts, there is no variant. You can mark it as fixed.

@spring-projects-issues
Copy link
Collaborator Author

Stevo Slavić commented

Better change issue type from bug to improvement.
I've created a pull request which optimizes parsing locale string, to skip variant processing when it's clear that locale string doesn't contain variant part.

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.0 RC1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants