From 6091be7606996bc91ba89acae51ff868596db2d8 Mon Sep 17 00:00:00 2001 From: Zaxar163 <35835496+Zaxar163@users.noreply.github.com> Date: Mon, 22 Oct 2018 08:13:23 +0300 Subject: [PATCH] Critical fixes NPE. --- .../ru/gravit/utils/helper/EnvHelper.java | 114 +++++++++--------- 1 file changed, 58 insertions(+), 56 deletions(-) diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java index 057d799a..9e6d20a9 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/EnvHelper.java @@ -6,67 +6,69 @@ import java.util.StringTokenizer; import java.util.regex.Pattern; -import ru.gravit.launcher.LauncherAPI; - public class EnvHelper { - private static final boolean TST; - private static final boolean HASXM; - public static final String[] toTest; - public static final Pattern[] test; + private static final boolean TST; + private static final boolean HASXW; + public static final String[] toTest; + public static final Pattern[] test; - static { - toTest = new String[]{"_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS"}; - test = new Pattern[]{Pattern.compile("-Xm.*\\d+[KMG]")}; - TST = check0(); - HASXM = check1(); - } + static { + toTest = new String[] { "_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS" }; + test = new Pattern[] { Pattern.compile("-xm.*\\d+[KMG]") }; + TST = check0(); + HASXW = check1(); + } - public static void addEnv(ProcessBuilder builder) { - if (hasOptsVar()) { - Map repl = new HashMap<>(); - for (String str : toTest) { - repl.put(str, ""); - repl.put(str.toLowerCase(Locale.ENGLISH), ""); - } - JVMHelper.appendVars(builder, repl); - } - } - public static void checkDangerousParametrs() - { - for(String t : toTest) - { - String env = System.getenv(t); - if(env.contains("-javaagent") || env.contains("-agentpath") || env.contains("-agentlib")) throw new SecurityException("JavaAgent in global optings not allow"); - } - } + public static void addEnv(ProcessBuilder builder) { + if (hasOptsEnv()) { + Map repl = new HashMap<>(); + for (String str : toTest) { + repl.put(str, ""); + repl.put(str.toLowerCase(Locale.US), ""); + } + JVMHelper.appendVars(builder, repl); + } + } - private static boolean check0() { - for (String test : toTest) if (System.getProperty(test) != null) return true; - return false; - } + private static boolean check0() { + for (String test : toTest) + if (System.getProperty(test) != null) + return true; + return false; + } - /** - * Вынужденное решение ибо тест на наличие -Xm* этакой нужен. - */ - private static boolean check1() { - if (hasOptsVar()) - for (String testStr : toTest) - if (System.getProperty(testStr) != null) { - String str = System.getenv(testStr); - StringTokenizer st = new StringTokenizer(str); - while (st.hasMoreTokens()) - if (CommonHelper.multiMatches(test, st.nextToken())) return true; - } - return false; - } + /** + * Вынужденное решение ибо тест на наличие -Xm* этакой нужен. + */ + private static boolean check1() { + if (hasOptsEnv()) + for (String testStr : toTest) + if (System.getenv(testStr) != null) { + String str = System.getenv(testStr).toLowerCase(Locale.US); + StringTokenizer st = new StringTokenizer(str); + while (st.hasMoreTokens()) + if (CommonHelper.multiMatches(test, st.nextToken())) + return true; + } + return false; + } - @LauncherAPI - public static boolean hasMemPreDef() { - return HASXM; - } + public static void checkDangerousParametrs() { + if (hasOptsEnv()) + for (String t : toTest) + if (System.getenv(t) != null) { + String env = System.getenv(t).toLowerCase(Locale.US); + if (env.contains("-cp") || env.contains("-classpath") || env.contains("-javaagent") + || env.contains("-agentpath") || env.contains("-agentlib")) + throw new SecurityException("JavaAgent in global optings not allow"); + } + } - @LauncherAPI - public static boolean hasOptsVar() { - return TST; - } + public static boolean hasOptsEnv() { + return TST; + } + + public static boolean hasWarnPreDef() { + return HASXW; + } }