-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Java: Enhance java/jvm-exit
query and add to quality
#20190
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
base: main
Are you sure you want to change the base?
Conversation
QHelp previews: java/ql/src/Violations of Best Practice/Undesirable Calls/CallsToSystemExit.qhelpForcible JVM terminationCalling one of the methods It is sometimes considered acceptable to call RecommendationInstead of calling ExampleIn the following example, problem 1 shows that Problem 2 is more subtle. In this example, there is just one entry point to the program (the // Problem 1: Miss out cleanup code
class FileOutput {
boolean write(String[] s) {
try {
output.write(s.getBytes());
} catch (IOException e) {
System.exit(1); // BAD: Should handle or propagate error instead of exiting
}
return true;
}
}
// Problem 2: Make code reuse difficult
class Action {
public void run() {
// ...
// Perform tasks ...
// ...
System.exit(0); // BAD: Should return status or throw exception
}
public static void main(String[] args) {
new Action().run();
}
}
// Good example: Proper error handling
class BetterAction {
public int run() throws Exception {
// ...
// Perform tasks ...
// ...
return 0; // Return status instead of calling System.exit
}
public static void main(String[] args) {
try {
BetterAction action = new BetterAction();
int exitCode = action.run();
System.exit(exitCode); // GOOD: Exit from main method
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.exit(1); // GOOD: Exit from main method on error
}
}
} References
|
java/ql/src/Violations of Best Practice/Undesirable Calls/CallsToSystemExit.ql
Fixed
Show fixed
Hide fixed
2cf0828
to
5a643be
Compare
5a643be
to
f6aad96
Compare
This PR integrates improvements from the
java/jvm-exit-prevents-cleanup-and-reuse
query incodeql-quality-queries
into the main CodeQL repository. The enhanced query now provides better detection of problematic JVM termination calls with reduced false positives and has been promoted quality query suite.It was decided to keep this query at medium precision, as there are quite a few false positives. This is largely due to the way command-line interfaces (CLI) and build tools handle application termination: they often use exit calls in ways that differ from standard software practices. As a result, the query may incorrectly flag these legitimate uses as potential issues, which impacts its overall accuracy.
Autofix appears feasible, though acceptance may vary depending on the specific case, as some instances could require more extensive refactoring within the repository.