mirror of
https://github.com/github/codeql.git
synced 2026-04-26 01:05:15 +02:00
Java: Added basic test cases for java/jvm-exit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
| ExampleRuntimeExit.java:22:17:22:44 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
|
||||
| ExampleRuntimeExit.java:25:17:25:44 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
|
||||
| ExampleRuntimeExit.java:35:9:35:43 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
|
||||
| ExampleRuntimeHalt.java:18:17:18:44 | halt(...) | Avoid calls to Runtime.halt() as this makes code harder to reuse. |
|
||||
| ExampleRuntimeHalt.java:21:17:21:44 | halt(...) | Avoid calls to Runtime.halt() as this makes code harder to reuse. |
|
||||
| ExampleSystemExit.java:22:17:22:30 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
|
||||
| ExampleSystemExit.java:25:17:25:30 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
|
||||
| ExampleSystemExit.java:35:9:35:29 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
|
||||
@@ -0,0 +1,2 @@
|
||||
query: Violations of Best Practice/Undesirable Calls/CallsToSystemExit.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
@@ -0,0 +1,37 @@
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExampleRuntimeExit {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Action action = new Action();
|
||||
try {
|
||||
action.run();
|
||||
} catch (Exception e) {
|
||||
printUsageAndExit(e.getMessage(), 1);
|
||||
}
|
||||
Runtime.getRuntime().exit(0); // COMPLIANT
|
||||
}
|
||||
|
||||
static class Action {
|
||||
public void run() {
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream("output.txt");
|
||||
fos.write("Hello, World!".getBytes());
|
||||
fos.close();
|
||||
Runtime.getRuntime().exit(0); // $ Alert
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Runtime.getRuntime().exit(1); // $ Alert
|
||||
} catch (Exception e) {
|
||||
// re-throw the exception
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static void printUsageAndExit(final String message, final int exitCode) {
|
||||
System.err.println("Usage: <example_cmd> <example_args> : " + message);
|
||||
Runtime.getRuntime().exit(exitCode); // $ SPURIOUS: Alert
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExampleRuntimeHalt {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Action action = new Action();
|
||||
action.run();
|
||||
Runtime.getRuntime().halt(0); // COMPLIANT
|
||||
}
|
||||
|
||||
static class Action {
|
||||
public void run() {
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream("output.txt");
|
||||
fos.write("Hello, World!".getBytes());
|
||||
fos.close();
|
||||
Runtime.getRuntime().halt(0); // $ Alert
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Runtime.getRuntime().halt(1); // $ Alert
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExampleSystemExit {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Action action = new Action();
|
||||
try {
|
||||
action.run();
|
||||
} catch (Exception e) {
|
||||
printUsageAndExit(e.getMessage(), 1);
|
||||
}
|
||||
System.exit(0); // COMPLIANT
|
||||
}
|
||||
|
||||
static class Action {
|
||||
public void run() {
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream("output.txt");
|
||||
fos.write("Hello, World!".getBytes());
|
||||
fos.close();
|
||||
System.exit(0); // $ Alert
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1); // $ Alert
|
||||
} catch (Exception e) {
|
||||
// re-throw the exception
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static void printUsageAndExit(final String message, final int exitCode) {
|
||||
System.err.println("Usage: <example_cmd> <example_args> : " + message);
|
||||
System.exit(exitCode); // $ SPURIOUS: Alert
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user