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

BugPattern for using the default system Locale #632

Closed
leventov opened this issue May 26, 2017 · 5 comments · Fixed by #4496
Closed

BugPattern for using the default system Locale #632

leventov opened this issue May 26, 2017 · 5 comments · Fixed by #4496

Comments

@leventov
Copy link

Similar to http://errorprone.info/bugpattern/DefaultCharset, it seems reasonable to have a warning of using the default system Locale, e. g. in String.format().

@Stephan202
Copy link
Contributor

A list of candidate methods can be extracted from the forbidden-apis project. I'd like to see "smart" support for String.format, though, for reasons outlined in policeman-tools/forbidden-apis#119. (The good thing is that Error Prone is sufficiently expressive to pull this off.)

This was referenced Aug 3, 2022
bulldozer-bot bot referenced this issue in palantir/witchcraft-api Aug 8, 2022
###### _excavator_ is a bot for automating changes across repositories.

Changes produced by the roomba/latest-baseline-oss check.

# Release Notes
## 4.152.0
| Type | Description | Link |
| ---- | ----------- | ---- |
| Feature | Add DefaultLocale check<br><br>Related to https://github.com/google/error-prone/issues/632<br><br>Adds a `DefaultLocale` check that replaces uses of `String.toLowerCase()` and `String.toUpperCase()` with the overloads that take a `Locale`, using `Locale.ROOT`. | palantir/gradle-baseline#2343 |


## 4.153.0
| Type | Description | Link |
| ---- | ----------- | ---- |
| Fix | Set the java launcher for Checkstyle tasks, too | palantir/gradle-baseline#2351 |



To enable or disable this check, please contact the maintainers of Excavator.
@tbroyer
Copy link
Contributor

tbroyer commented Jul 14, 2024

Fwiw, looking at which checks it provides, this might be the only reason for using forbidden-apis (unless you also want the commons.io checks), all others are either @Deprecated that can be handled by -Xlint and -Werror, or are covered by DefaultCharset and JavaTimeDefaultTimeZone (only covers java.time APIs but should be enough nowadays)

Of course, forbidden-apis works at the bytecode level so can also check bytecode generated by Groovy, Kotlin, or Scala, so can still be useful.

@cushon
Copy link
Collaborator

cushon commented Jul 16, 2024

We have https://errorprone.info/bugpattern/StringCaseLocaleUsage specifically for this issue in toLowerCase/toUpperCase, I'd be interesting in a contribution for a more general check.

A list of candidate methods can be extracted from the forbidden-apis project.

It looks like their list is here: https://github.com/policeman-tools/forbidden-apis/blob/ce478c1ad357535518c39c5311d46ec7c0311777/src/main/resources/de/thetaphi/forbiddenapis/signatures/jdk-unsafe-1.7.txt#L52-L108

I'd like to see "smart" support for String.format, though, for reasons outlined in policeman-tools/forbidden-apis#119. (The good thing is that Error Prone is sufficiently expressive to pull this off.)

That makes sense to me, I think for something that was enabled by default in Error Prone we'd probably want it to have a lower false positive rate, and perhaps only report diagnostics if the format string is a compile-time constant that contains format specifiers that are known to be bad.

@tbroyer
Copy link
Contributor

tbroyer commented Jul 17, 2024

Oh, hadn't even seen that StringCaseLocaleUsage check! (or had forgotten about it)

Anyway, I'm working on that DefaultLocale check, trying to categorize the various constructors and methods into buckets (between those that could use Locale.getDefault(FORMAT) vs Locale.getDefault(DISPLAY) vs just Locale.getDefault() when it's not perfectly clear which category to use; those that have more overloads to handle, like java.text.DateFormat.getXxxInstance; and those with no fix to suggest such as java.text.MessageFormat.format(String, Object...))
Not sure I'll be able to do that smart format/printf check though, so maybe do it in 2 steps: first add the check without it and not enabled by default, then make it smarter and enable the check by default.

Also fwiw, there are more than those listed in the jdk-unsafe-1.7 bundled signatures file: https://github.com/search?q=repo%3Apoliceman-tools%2Fforbidden-apis%20%22uses%20default%20locale%22&type=code

@tbroyer
Copy link
Contributor

tbroyer commented Jul 20, 2024

That finally wasn't that hard 😅

PR #4487 is obviously opinionated: I finally didn't include many "display" methods (only Currency.getSymbol), I didn't include Console and explicitly excluded System.out/System.err as they're directed towards "users" and should probably use the default locale in most cases, the "smart" String.format (and similar format methods) should (hopefully) not have too much false positives (and no false negative 🤞), and I didn't suggest fixes for everything (some Formatter constructors that also use the default charset and would imply permutation "explosion" of fixes), and similarly didn't include the "Uses default locale or time zone" methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants