8316964: Security tools should not call System.exit

Reviewed-by: valeriep
This commit is contained in:
Weijun Wang 2023-10-24 18:32:01 +00:00
parent 1f2a80b78a
commit 1ddf826aea
8 changed files with 370 additions and 220 deletions

View file

@ -405,26 +405,38 @@ public final class Main {
collator.setStrength(Collator.PRIMARY);
}
private Main() { }
public static void main(String[] args) throws Exception {
Main kt = new Main();
kt.run(args, System.out);
int exitCode = kt.run(args, System.out);
if (exitCode != 0) {
System.exit(exitCode);
}
}
private void run(String[] args, PrintStream out) throws Exception {
private static class ExitException extends RuntimeException {
@java.io.Serial
static final long serialVersionUID = 0L;
private final int errorCode;
public ExitException(int errorCode) {
this.errorCode = errorCode;
}
}
public int run(String[] args, PrintStream out) throws Exception {
try {
args = parseArgs(args);
parseArgs(args);
if (command != null) {
doCommands(out);
}
} catch (ExitException ee) {
return ee.errorCode;
} catch (Exception e) {
System.out.println(rb.getString("keytool.error.") + e);
if (verbose) {
e.printStackTrace(System.out);
}
if (!debug) {
System.exit(1);
return 1;
} else {
throw e;
}
@ -441,6 +453,7 @@ public final class Main {
ksStream.close();
}
}
return 0;
}
/**
@ -5247,7 +5260,7 @@ public final class Main {
if (debug) {
throw new RuntimeException("NO BIG ERROR, SORRY");
} else {
System.exit(1);
throw new ExitException(1);
}
}