eclipse-jdt / eclipse-jdt/eclipse.jdt.core

ECJ incorrectly accepts code with potentially uninitialized variable

Open
#4,130 8 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Environment:
ECJ Version:
```
Eclipse Compiler for Java(TM) v20250613-1403, 3.42.50, Copyright IBM Corp 2000, 2020. All rights reserved
```
Java Version:

```
java version "1.8.0_451"
Java(TM) SE Runtime Environment (build 1.8.0_451-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.451-b10, mixed mode)

java version "11.0.27" 2025-04-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.27+8-LTS-232)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.27+8-LTS-232, mixed mode)

java version "17.0.15" 2025-04-15 LTS
Java(TM) SE Runtime Environment (build 17.0.15+9-LTS-241)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.15+9-LTS-241, mixed mode, sharing)

java version "21.0.7" 2025-04-15 LTS
Java(TM) SE Runtime Environment (build 21.0.7+8-LTS-245)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.7+8-LTS-245, mixed mode, sharing)
```
Javac Version:

```
javac 1.8.0_451
javac 11.0.27
javac 17.0.15
javac 21.0.7
```

Description:
We have identified a test program rejected by javac but accepted by ECJ. This happens in JDK 8, 11, 17, and 21.
However, the program is invalid as it has a variable might not be initialized.

Test program:
```java
public class Test {
public static void main(String[] strArr) {
while (true) {
if (true)
break;
try {
java.lang.reflect.Method cloneMethod;
try {
cloneMethod = Object.class.getMethod("clone");
} catch (NoSuchMethodException e) {
}
Object obj = new Object();
try {
Object clonedObj = (Object) cloneMethod.invoke(obj);
} catch (Exception e) {
}
} catch (ArithmeticException a_e) {
}

}
}
}
```

Using javac it will have an error:
```bash
javac Test.java
Test.java:15: error: variable cloneMethod might not have been initialized
Object clonedObj = (Object) cloneMethod.invoke(obj);
^
1 error
```

However, using ECJ it will be compiled successfully.

```bash
java -jar ecj-4.36M3.jar --release 21 -d . Test.java
# No error
```

Expected behavior: ECJ should report same error as javac.
Actual behavior: ECJ incorrectly accepts the program.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.