Skip to content

Java: Remove requirements for final and access mods from DateFormatThreadUnsafe #6725

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

Merged
merged 2 commits into from
Sep 23, 2021

Conversation

emilejq
Copy link
Contributor

@emilejq emilejq commented Sep 21, 2021

The rule checking for unsafe use of the DateFormat class in Java is incorrectly requiring that the DateFormat variable be declared final and either public or protected.

This PR removes those two requirements

@emilejq emilejq requested a review from a team as a code owner September 21, 2021 16:00
@github-actions github-actions bot added the Java label Sep 21, 2021
@smowton
Copy link
Contributor

smowton commented Sep 22, 2021

I suspect these are here as heuristics -- after all even static-ness is just a heuristic; you can get this just as wrong by sharing between threads using instance fields or local variables with some way to gain a cross-thread alias.

Excluding private or package-private fields is likely acknowledging that at least then you control the accesses and might enforce some appropriate locking discipline or know your code is single-threaded.

On the other hand a final and non-final exposed field seem equally problematic to me, so I would support removing that constraint.

@emilejq
Copy link
Contributor Author

emilejq commented Sep 22, 2021

I take the point about heuristics. I guess the risk with requiring the field to be private or protected is that it assumes someone falling foul of this rule is aware of the concurrency issues with the DateFormat class and has written the code accordingly.

At any rate, the reason I created this PR is because the example of non-compliant code given in the help files wouldn't be detected by the current implementation of this rule due to the two reasons given here. The field is declared private static DateFormat dateF = new SimpleDateFormat("yyyyMMdd");

https://github.com/github/codeql/blob/main/java/ql/src/Likely%20Bugs/Concurrency/DateFormatThreadUnsafe.java

@smowton
Copy link
Contributor

smowton commented Sep 23, 2021

Good point, please do update that to use a public field.

At the end of the day it's a tradeoff between nuisance alerts and missing ones; I don't see either clearly better than the other. Like I say if you want to change this to omit isFinal but keep the visibility constraint I'd merge that.

@emilejq
Copy link
Contributor Author

emilejq commented Sep 23, 2021

Done

@smowton smowton added the no-change-note-required This PR does not need a change note label Sep 23, 2021
@aschackmull aschackmull merged commit 6be4b3b into github:main Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants