-
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
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.
Pull Request Overview
This PR enhances the java/jvm-exit
query by improving its accuracy and reducing false positives, then promotes it from low to medium precision and includes it in the quality query suite.
- Enhanced the query logic to better distinguish between legitimate and problematic JVM exit calls
- Added comprehensive test coverage with examples for System.exit(), Runtime.exit(), and Runtime.halt()
- Updated documentation with improved guidance on proper error handling patterns
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
CallsToSystemExit.ql | Refactored query with improved filtering logic to reduce false positives and better precision |
CallsToSystemExit.qhelp | Enhanced documentation with clearer recommendations and better examples |
CallsToSystemExit.java | Extended example code with both problematic and recommended patterns |
Test files (ExampleSystemExit.java, etc.) | Added comprehensive test cases covering different exit method scenarios |
Query suite files | Added the enhanced query to the quality suite and removed from excluded list |
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.