...
Wiki Markup |
---|
{{Runtime.halt()}} works similarlyis similar to {{Runtime.exit()}} but does _not_ run shutdown hooks or finalizers. According to the Java API \[[API 06|AA. Bibliography#API 06]\], {{Runtime.halt()}} |
forcibly terminates the currently running Java virtual machine. This method never returns normally.
This method should be used with extreme caution. Unlike the exit method, this method does not cause shutdown hooks to be started and does not run uninvoked finalizers if finalization-on-exit has been enabled. If the shutdown sequence has already been initiated, then this method does not wait for any running shutdown hooks or finalizers to finish their work.
In contrast with C and C++, Java does Java programs do not flush unwritten buffered data or close open files when it exitsthey exit, so programs must perform these operations manually. Programs must also perform any other cleanup that involves external resources, such as releasing shared locks.
...
This example creates a new file, outputs some text to it, and abruptly exits using Runtime.exit()
. Consequently, the file is may be closed without the text actually being written.
Code Block | ||
---|---|---|
| ||
public class CreateFile { public static void main(String[] args) throws FileNotFoundException { final PrintStream out = new PrintStream(new BufferedOutputStream( new FileOutputStream("foo.txt"))); out.println("hello"); Runtime.getRuntime().exit(1); } } |
...
Code Block | ||
---|---|---|
| ||
public class CreateFile { public static void main(String[] args) throws FileNotFoundException { final PrintStream out = new PrintStream(new BufferedOutputStream( new FileOutputStream("foo.txt"))); try { out.println("hello"); } finally { try { out.close(); } }catch (IOException x) { // handle error } } Runtime.getRuntime().exit(1); } } |
...
When a user forcefully exits a program, for example by pressing the ctrl + c
key keys or by using the kill
command, the JVM terminates abruptly. Although this event cannot be captured, the program should nevertheless perform any mandatory cleanup operations before exiting. This noncompliant code example fails to do so.
...
The JVM can abort for external reasons, such as an external SIGKILL
signal (POSIX) or the TerminateProcess()
call (Windows), or memory corruption caused by native methods. Shutdown hooks may fail to execute as expected in such cases because the JVM cannot guarantee that they will be executed as intended.
Risk Assessment
Using Runtime.halt()
in place of Runtime.exit()
may not perform necessary cleanup, potentially leaving sensitive data exposed or leaving data Failure to perform necessary cleanup at program termination may leave the system in an inconsistent state.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="aadff91918aa881d-2cde2e97-4e8543e5-89f88f41-f53ee2b4a25141153deeedf2"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | Termination Strategy [REU] | ]]></ac:plain-text-body></ac:structured-macro> |
CWE-705. Incorrect control flow scoping |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7124f673b061fe4b-7fd527f4-49e04d78-8d11b2d4-a051084d183f1270deb4b3aa"><ac:plain-text-body><![CDATA[ | [[API 06 | AA. Bibliography#API 06]] | [Class | http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2aee7ac2fbf8b3af-c8228aa0-4691473e-b44b98f4-d0615dfc4eacb7628094f869"><ac:plain-text-body><![CDATA[ | [[ISO/IEC TR 24772:2010 | AA. Bibliography#ISO/IEC TR 24772-2010]] | Section 6.46, Termination Strategy [REU] | ]]></ac:plain-text-body></ac:structured-macro> |
...