Java: Enhance java/jvm-exit query and add to quality#20190
Java: Enhance java/jvm-exit query and add to quality#20190Napalys merged 9 commits intogithub:mainfrom
java/jvm-exit query and add to quality#20190Conversation
|
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
|
2cf0828 to
5a643be
Compare
5a643be to
f6aad96
Compare
There was a problem hiding this comment.
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 |
…sToSystemExit.ql Co-authored-by: Michael Nebel <michaelnebel@github.com>
681e568 to
71e827e
Compare
71e827e to
eb6e9b8
Compare
…sToSystemExit.ql Co-authored-by: Michael Nebel <michaelnebel@github.com>
michaelnebel
left a comment
There was a problem hiding this comment.
Excellent!
No more comments from my side, if you are happy with the DCA results.
…sToSystemExit.ql Co-authored-by: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com>
…sToSystemExit.ql Co-authored-by: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com>
590a299 to
b3f90bb
Compare
This PR integrates improvements from the
java/jvm-exit-prevents-cleanup-and-reusequery incodeql-quality-queriesinto 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 low/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.
Note: Since the query's precision is low/medium, it will only be included in the code quality extended suite, and not in the main code quality suite.