Critical fixes NPE.

This commit is contained in:
Zaxar163 2018-10-22 08:13:23 +03:00 committed by zaxar163
parent 01fd6d2689
commit 6091be7606

View file

@ -6,67 +6,69 @@
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import ru.gravit.launcher.LauncherAPI;
public class EnvHelper { public class EnvHelper {
private static final boolean TST; private static final boolean TST;
private static final boolean HASXM; private static final boolean HASXW;
public static final String[] toTest; public static final String[] toTest;
public static final Pattern[] test; public static final Pattern[] test;
static { static {
toTest = new String[]{"_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS"}; toTest = new String[] { "_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS" };
test = new Pattern[]{Pattern.compile("-Xm.*\\d+[KMG]")}; test = new Pattern[] { Pattern.compile("-xm.*\\d+[KMG]") };
TST = check0(); TST = check0();
HASXM = check1(); HASXW = check1();
} }
public static void addEnv(ProcessBuilder builder) { public static void addEnv(ProcessBuilder builder) {
if (hasOptsVar()) { if (hasOptsEnv()) {
Map<String, String> repl = new HashMap<>(); Map<String, String> repl = new HashMap<>();
for (String str : toTest) { for (String str : toTest) {
repl.put(str, ""); repl.put(str, "");
repl.put(str.toLowerCase(Locale.ENGLISH), ""); repl.put(str.toLowerCase(Locale.US), "");
} }
JVMHelper.appendVars(builder, repl); 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");
}
}
private static boolean check0() { private static boolean check0() {
for (String test : toTest) if (System.getProperty(test) != null) return true; for (String test : toTest)
return false; if (System.getProperty(test) != null)
} return true;
return false;
}
/** /**
* Вынужденное решение ибо тест на наличие -Xm* этакой нужен. * Вынужденное решение ибо тест на наличие -Xm* этакой нужен.
*/ */
private static boolean check1() { private static boolean check1() {
if (hasOptsVar()) if (hasOptsEnv())
for (String testStr : toTest) for (String testStr : toTest)
if (System.getProperty(testStr) != null) { if (System.getenv(testStr) != null) {
String str = System.getenv(testStr); String str = System.getenv(testStr).toLowerCase(Locale.US);
StringTokenizer st = new StringTokenizer(str); StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) while (st.hasMoreTokens())
if (CommonHelper.multiMatches(test, st.nextToken())) return true; if (CommonHelper.multiMatches(test, st.nextToken()))
} return true;
return false; }
} return false;
}
@LauncherAPI public static void checkDangerousParametrs() {
public static boolean hasMemPreDef() { if (hasOptsEnv())
return HASXM; 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 hasOptsEnv() {
public static boolean hasOptsVar() { return TST;
return TST; }
}
public static boolean hasWarnPreDef() {
return HASXW;
}
} }