...
Runtime.exit()
is the typical way of exiting a program. According to the Java API [API 2014], Runtime.exit()
:
terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
The virtual machine's shutdown sequence consists of two phases. In the first phase all registered shutdown hooks, if any, are started in some unspecified order and allowed to run concurrently until they finish. In the second phase all uninvoked finalizers are run if finalization-on-exit has been enabled. Once this is performed the virtual machine halts.
If this method is invoked after the virtual machine has begun its shutdown sequence, then if shutdown hooks are being run, this method will block indefinitely. If shutdown hooks have already been run and on-exit finalization has been enabled, then this method halts the virtual machine with the given status code if the status is nonzero; otherwise, it blocks indefinitely.
The
System.exit()
method is the conventional and convenient means of invoking this method.
...
Failure to perform necessary cleanup at program termination may leave the system in an inconsistent state.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
FIO14-J | Medium | Likely | Medium | P12 | L1 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.FIO14.CIO CERT.FIO14.CCR CERT.FIO14.CRWD | Close input and output resources in "finally" blocks Close all "java.io.Closeable" resources in a "finally" block Close resources as early as possible |
Related Guidelines
Termination Strategy [REU] | |
CWE-705, Incorrect Control Flow Scoping |
Android Implementation Details
Although most of the code examples are not applicable to the Android platform, the principle is applicable to Android. Aprocess on Android can be terminated in a number of ways: android.app.Activity.finish()
and the related finish()
methods, android.app.Activity.moveTaskToBack(boolean flag)
, android.os.Process.killProcess(int pid)
, and System.exit()
.
Bibliography
[API 2014] | |
Section 6.46, "Termination Strategy [REU]" |
...
...