Skip to content

Commit e2b0faa

Browse files
committed
Java: Add java/javautilconcurrentscheduledthreadpoolexecutor query for zero thread pool size
1 parent 12cda86 commit e2b0faa

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# J-Q-002: Setting `java.util.concurrent.ScheduledThreadPoolExecutor` to have 0 threads serves no purpose
2+
3+
Setting `java.util.concurrent.ScheduledThreadPoolExecutor` to have 0 threads serves no purpose and may indicate programmer error.
4+
5+
## Overview
6+
7+
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.
8+
9+
## Recommendation
10+
11+
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.
12+
13+
## Example
14+
15+
```java
16+
public class Test {
17+
void f() {
18+
int i = 0;
19+
ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(1); // COMPLIANT
20+
ScheduledThreadPoolExecutor s1 = new ScheduledThreadPoolExecutor(0); // NON_COMPLIANT
21+
s.setCorePoolSize(0); // NON_COMPLIANT
22+
s.setCorePoolSize(i); // NON_COMPLIANT
23+
}
24+
}
25+
```
26+
27+
## References
28+
- [ScheduledThreadPoolExecutor](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/concurrent/ScheduledThreadPoolExecutor.html)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @id java/javautilconcurrentscheduledthreadpoolexecutor
3+
* @name J-Q-002: Setting `java.util.concurrent.ScheduledThreadPoolExecutor` to have 0 threads serves no purpose
4+
* @description Setting `java.util.concurrent.ScheduledThreadPoolExecutor` to have 0 threads serves
5+
* no purpose and may indicate programmer error.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity recommendation
9+
* @tags quality
10+
* reliability
11+
* correctness
12+
* concurrency
13+
*/
14+
15+
import java
16+
import semmle.code.java.dataflow.DataFlow
17+
18+
/**
19+
* `Call`s that have the ability to set or modify the `corePoolSize` of the `java.util.concurrent.ScheduledThreadPoolExecutor` type
20+
*/
21+
class Sink extends Call {
22+
Sink() {
23+
this.getCallee()
24+
.hasQualifiedName("java.util.concurrent", "ThreadPoolExecutor", "setCorePoolSize") or
25+
this.getCallee()
26+
.hasQualifiedName("java.util.concurrent", "ScheduledThreadPoolExecutor",
27+
"ScheduledThreadPoolExecutor")
28+
}
29+
}
30+
31+
from IntegerLiteral zero, Sink set
32+
where
33+
DataFlow::localFlow(DataFlow::exprNode(zero), DataFlow::exprNode(set.getArgument(0))) and
34+
zero.getIntValue() = 0
35+
select set, "ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Test.java:7:42:7:75 | new ScheduledThreadPoolExecutor(...) | ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads. |
2+
| Test.java:8:9:8:28 | setCorePoolSize(...) | ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads. |
3+
| Test.java:9:9:9:28 | setCorePoolSize(...) | ScheduledThreadPoolExecutor.corePoolSize is set to have 0 threads. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/Concurrency/ScheduledThreadPoolExecutorZeroThread.ql
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java.util.concurrent.ScheduledThreadPoolExecutor;
2+
3+
public class Test {
4+
void f() {
5+
int i = 0;
6+
ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(1); // COMPLIANT
7+
ScheduledThreadPoolExecutor s1 = new ScheduledThreadPoolExecutor(0); // NON_COMPLIANT
8+
s.setCorePoolSize(0); // NON_COMPLIANT
9+
s.setCorePoolSize(i); // NON_COMPLIANT
10+
}
11+
}

0 commit comments

Comments
 (0)