Skip to content

Commit

Permalink
avoid Building a command line with string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and jukzi committed Sep 20, 2023
1 parent a5a8208 commit fd300e8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -30,7 +29,6 @@
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
Expand All @@ -44,7 +42,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;

Expand Down Expand Up @@ -501,8 +498,8 @@ public ILaunchDelegate getDelegate() {
* The collection of native environment variables on the user's system. Cached
* after being computed once as the environment cannot change.
*/
private static HashMap<String, String> fgNativeEnv = null;
private static HashMap<String, String> fgNativeEnvCasePreserved = null;
private static HashMap<String, String> fgNativeEnv;
private static Map<String, String> fgNativeEnvCasePreserved;

/**
* Path to the local directory where local launch configurations
Expand Down Expand Up @@ -726,129 +723,6 @@ public void addLaunchListener(ILaunchListener listener) {
fListeners.add(listener);
}

/**
* Computes and caches the native system environment variables as a map of
* variable names and values (Strings) in the given map.
* <p>
* Note that WIN32 system environment preserves
* the case of variable names but is otherwise case insensitive.
* Depending on what you intend to do with the environment, the
* lack of normalization may or may not be create problems. This
* method preserves mixed-case keys using the variable names
* recorded by the OS.
* </p>
* @param cache the map
* @since 3.1
*/
private void cacheNativeEnvironment(Map<String, String> cache) {
try {
String nativeCommand= null;
boolean isWin9xME= false; //see bug 50567
String fileName= null;
if (Platform.getOS().equals(Constants.OS_WIN32)) {
String osName= System.getProperty("os.name"); //$NON-NLS-1$
isWin9xME= osName != null && (osName.startsWith("Windows 9") || osName.startsWith("Windows ME")); //$NON-NLS-1$ //$NON-NLS-2$
if (isWin9xME) {
// Win 95, 98, and ME
// SET might not return therefore we pipe into a file
IPath stateLocation= DebugPlugin.getDefault().getStateLocation();
fileName= stateLocation.toOSString() + File.separator + "env.txt"; //$NON-NLS-1$
nativeCommand= "command.com /C set > " + fileName; //$NON-NLS-1$
} else {
// Win NT, 2K, XP
nativeCommand= "cmd.exe /C set"; //$NON-NLS-1$
}
} else if (!Platform.getOS().equals(Constants.OS_UNKNOWN)){
nativeCommand= "env"; //$NON-NLS-1$
}
if (nativeCommand == null) {
return;
}
Process process= Runtime.getRuntime().exec(nativeCommand);
if (isWin9xME) {
//read piped data on Win 95, 98, and ME
Properties p= new Properties();
File file= new File(fileName);
try(InputStream stream = new BufferedInputStream(new FileInputStream(file))){
p.load(stream);
if (!file.delete()) {
file.deleteOnExit(); // if delete() fails try again on VM close
}
for (Entry<Object, Object> entry : p.entrySet()) {
// Win32's environment variables are case insensitive. Put everything
// to uppercase so that (for example) the "PATH" variable will match
// "pAtH" correctly on Windows.
String key = (String) entry.getKey();
//no need to cast value
cache.put(key, (String) p.get(key));
}
}
} else {
//read process directly on other platforms
//we need to parse out matching '{' and '}' for function declarations in .bash environments
// pattern is [func name]=() { and we must find the '}' on its own line with no trailing ';'
try (InputStream stream = process.getInputStream();
InputStreamReader isreader = new InputStreamReader(stream);
BufferedReader reader = new BufferedReader(isreader)) {
String line = reader.readLine();
String key = null;
String value = null;
String newLine = System.lineSeparator();
while (line != null) {
int func = line.indexOf("=()"); //$NON-NLS-1$
if (func > 0) {
key = line.substring(0, func);
// scan until we find the closing '}' with no
// following chars
value = line.substring(func + 1);
while (line != null && !line.equals("}")) { //$NON-NLS-1$
line = reader.readLine();
if (line != null) {
value += newLine + line;
}
}
line = reader.readLine();
}
else {
int separator = line.indexOf('=');
if (separator > 0) {
key = line.substring(0, separator);
value = line.substring(separator + 1);
line = reader.readLine();
if (line != null) {
// this line has a '=' read ahead to check
// next line for '=', might be broken on
// more than one line
// also if line starts with non-identifier -
// it is remainder of previous variable
while (line.indexOf('=') < 0 || (line.length() > 0 && !Character.isJavaIdentifierStart(line.charAt(0)))) {
value += newLine + line;
line = reader.readLine();
if (line == null) {
// if next line read is the end of
// the file quit the loop
break;
}
}
}
}
}
if (key != null) {
cache.put(key, value);
key = null;
value = null;
} else {
line = reader.readLine();
}
}
}
}
} catch (IOException e) {
// Native environment-fetching code failed.
// This can easily happen and is not useful to log.
}
}

/**
* Clears all launch configurations (if any have been accessed)
*/
Expand Down Expand Up @@ -1815,8 +1689,7 @@ public synchronized Map<String, String> getNativeEnvironment() {
@Override
public synchronized Map<String, String> getNativeEnvironmentCasePreserved() {
if (fgNativeEnvCasePreserved == null) {
fgNativeEnvCasePreserved = new HashMap<>();
cacheNativeEnvironment(fgNativeEnvCasePreserved);
fgNativeEnvCasePreserved = System.getenv();
}
return new HashMap<>(fgNativeEnvCasePreserved);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Bug_044106 extends ResourceTest {

private void createSymLink(String target, String local) {
try {
Process p = Runtime.getRuntime().exec("/bin/ln -s " + target + " " + local);
Process p = Runtime.getRuntime().exec(new String[] { "/bin/ln", "-s", target, local });
p.waitFor();
} catch (IOException e) {
fail("1.0", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public boolean isAvailable() {
return false;
}
try {
Process pr = Runtime.getRuntime().exec("which " + executable); //$NON-NLS-1$
Process pr = Runtime.getRuntime().exec(new String[] { "which", executable }); //$NON-NLS-1$
StreamConsumer outputs = new StreamConsumer(pr.getInputStream());
(outputs).start();
StreamConsumer errors = new StreamConsumer(pr.getErrorStream());
Expand Down

0 comments on commit fd300e8

Please sign in to comment.