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,42 +6,34 @@
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;
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]")};
toTest = new String[] { "_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS" };
test = new Pattern[] { Pattern.compile("-xm.*\\d+[KMG]") };
TST = check0();
HASXM = check1();
HASXW = check1();
}
public static void addEnv(ProcessBuilder builder) {
if (hasOptsVar()) {
if (hasOptsEnv()) {
Map<String, String> repl = new HashMap<>();
for (String str : toTest) {
repl.put(str, "");
repl.put(str.toLowerCase(Locale.ENGLISH), "");
repl.put(str.toLowerCase(Locale.US), "");
}
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() {
for (String test : toTest) if (System.getProperty(test) != null) return true;
for (String test : toTest)
if (System.getProperty(test) != null)
return true;
return false;
}
@ -49,24 +41,34 @@ private static boolean check0() {
* Вынужденное решение ибо тест на наличие -Xm* этакой нужен.
*/
private static boolean check1() {
if (hasOptsVar())
if (hasOptsEnv())
for (String testStr : toTest)
if (System.getProperty(testStr) != null) {
String str = System.getenv(testStr);
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;
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() {
public static boolean hasOptsEnv() {
return TST;
}
public static boolean hasWarnPreDef() {
return HASXW;
}
}