-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Java: Add java/javautilconcurrentscheduledthreadpoolexecutor
query for zero thread pool size
#19844
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
Open
tamasvajk
wants to merge
1
commit into
github:main
Choose a base branch
from
tamasvajk:tamasvajk/threadpoolexecutor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
java/ql/src/Likely Bugs/Concurrency/ScheduledThreadPoolExecutorZeroThread.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## Overview | ||
|
||
According the Java documentation on `ScheduledThreadPoolExecutor`, it is not a good idea to set `corePoolSize` to zero, since doing so indicates the executor to keep 0 threads in its pool and the executor will serve no purpose. | ||
|
||
## Recommendation | ||
|
||
Set the `ScheduledThreadPoolExecutor` to have 1 or more threads in its thread pool and use the class's other methods to create a thread execution schedule. | ||
|
||
## Example | ||
|
||
```java | ||
public class Test { | ||
void f() { | ||
int i = 0; | ||
ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(1); // COMPLIANT | ||
ScheduledThreadPoolExecutor s1 = new ScheduledThreadPoolExecutor(0); // NON_COMPLIANT | ||
s.setCorePoolSize(0); // NON_COMPLIANT | ||
s.setCorePoolSize(i); // NON_COMPLIANT | ||
} | ||
} | ||
``` | ||
|
||
## References | ||
- [ScheduledThreadPoolExecutor](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/concurrent/ScheduledThreadPoolExecutor.html) |
35 changes: 35 additions & 0 deletions
35
java/ql/src/Likely Bugs/Concurrency/ScheduledThreadPoolExecutorZeroThread.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @id java/javautilconcurrentscheduledthreadpoolexecutor | ||
* @name Zero threads set for `java.util.concurrent.ScheduledThreadPoolExecutor` | ||
* @description Setting `java.util.concurrent.ScheduledThreadPoolExecutor` to have 0 threads serves | ||
* no purpose and may indicate programmer error. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity recommendation | ||
* @tags quality | ||
* reliability | ||
* correctness | ||
* concurrency | ||
*/ | ||
|
||
import java | ||
import semmle.code.java.dataflow.DataFlow | ||
|
||
/** | ||
* A `Call` that has the ability to set or modify the `corePoolSize` of the `java.util.concurrent.ScheduledThreadPoolExecutor` type. | ||
*/ | ||
class Sink extends Call { | ||
Sink() { | ||
this.getCallee() | ||
.hasQualifiedName("java.util.concurrent", "ThreadPoolExecutor", "setCorePoolSize") or | ||
this.getCallee() | ||
.hasQualifiedName("java.util.concurrent", "ScheduledThreadPoolExecutor", | ||
"ScheduledThreadPoolExecutor") | ||
} | ||
} | ||
|
||
from IntegerLiteral zero, Sink set | ||
where | ||
DataFlow::localFlow(DataFlow::exprNode(zero), DataFlow::exprNode(set.getArgument(0))) and | ||
zero.getIntValue() = 0 | ||
select set, "ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads." |
3 changes: 3 additions & 0 deletions
3
...ests/ScheduledThreadPoolExecutorZeroThread/ScheduledThreadPoolExecutorZeroThread.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
| Test.java:7:42:7:75 | new ScheduledThreadPoolExecutor(...) | ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads. | | ||
| Test.java:8:9:8:28 | setCorePoolSize(...) | ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads. | | ||
| Test.java:9:9:9:28 | setCorePoolSize(...) | ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads. | |
2 changes: 2 additions & 0 deletions
2
...y-tests/ScheduledThreadPoolExecutorZeroThread/ScheduledThreadPoolExecutorZeroThread.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
query: Likely Bugs/Concurrency/ScheduledThreadPoolExecutorZeroThread.ql | ||
postprocess: utils/test/InlineExpectationsTestQuery.ql |
11 changes: 11 additions & 0 deletions
11
java/ql/test/query-tests/ScheduledThreadPoolExecutorZeroThread/Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
|
||
public class Test { | ||
void f() { | ||
int i = 0; | ||
ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(1); // Compliant | ||
ScheduledThreadPoolExecutor s1 = new ScheduledThreadPoolExecutor(0); // $ Alert | ||
s.setCorePoolSize(0); // $ Alert | ||
s.setCorePoolSize(i); // $ Alert | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any particular reason why only local flow is used (instead of making a data flow configuration and using global flow)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if there's any reason. I simply migrated the query as is from the private repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried the below:
It resulted in some new findings, for example this, which is a FP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
threads > 0
check?