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

Case-insensitive comparisons may be adversely affected by the user's locale #42719

Closed
philwebb opened this issue Oct 16, 2024 · 5 comments
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@philwebb
Copy link
Member

We should align with Spring Framework (see spring-projects/spring-framework#33708). We should also add a checkstyle rule (spring-projects/spring-framework@0a64591)

@philwebb philwebb added the type: task A general task label Oct 16, 2024
@philwebb philwebb added this to the 3.2.x milestone Oct 16, 2024
@philwebb philwebb added type: bug A general bug and removed type: task A general task labels Oct 16, 2024
@bclozel
Copy link
Member

bclozel commented Oct 16, 2024

I've had a look at creating a custom check for spring-javaformat - in the end, it's not really far from a "single line regexp check" because the AST doesn't give any type information about the instance nor arguments. Checking for the method name and argument count and heuristics is the best we can do.

@wilkinsona
Copy link
Member

We might be able to check more precisely using ArchUnit.

@nosan
Copy link
Contributor

nosan commented Oct 16, 2024

I tried ArchUnit in my branch and it works fine.

	private ArchRule noClassesShouldCallStringToLowerCaseWithoutLocale() {
		return ArchRuleDefinition.noClasses()
			.should()
			.callMethod(String.class, "toLowerCase")
			.because("String.toLowerCase(Locale.ROOT) should be used instead");
	}

	private ArchRule noClassesShouldCallStringToUpperCaseWithoutLocale() {
		return ArchRuleDefinition.noClasses()
			.should()
			.callMethod(String.class, "toUpperCase")
			.because("String.toUpperCase(Locale.ROOT) should be used instead");
	}

Architecture Violation [Priority: MEDIUM] - Rule 'no classes should call method String.toUpperCase(), because String.toUpperCase(Locale.ROOT) should be used instead' was violated (1 times):
Method <org.springframework.boot.build.architecture.string.toUpperCase.ToUpperCase.exampleMethod()> calls method <java.lang.String.toUpperCase()> in (ToUpperCase.java:23)

@wilkinsona wilkinsona changed the title Use Locale.ROOT for locale neutral, case insensitive comparisons Case-insensitive comparisons may be adversely affected by the user's locale Oct 17, 2024
@mhalbritter mhalbritter self-assigned this Oct 17, 2024
@mhalbritter mhalbritter modified the milestones: 3.2.x, 3.2.11 Oct 17, 2024
@rickie
Copy link

rickie commented Oct 25, 2024

Another tool that could've helped with flagging this issue is Error Prone. It's a compiler plugin that, in addition to pointing out problems, can also automatically apply the fixes (patch mode), so no manual change is required.

For this particular case, there is the StringCaseLocaleUsage check that flags such cases. To see the full list of checks you can go to the docs: https://errorprone.info/bugpatterns.

Is using Error Prone something the Spring team would find interesting to consider and explore?

@philwebb
Copy link
Member Author

Thanks @rickie, error prone has been on the radar for a while, but we haven't yet found the time to integrate it. We'll need it when we tackle #10712 and at that point will probably evaluate more checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants