Skip to content
Permalink
Browse files
[JENKINS-41266] Fix ArrayIndexOutOfBoundsException (#41)
* Avoid sending empty string to process starter

* Add CheckForNull

* Use Launcher to check isUnix
  • Loading branch information
Casz committed Jan 20, 2017
1 parent 0cdebc7 commit a89fc085eccce375eb0c343a5b7f16060bf752f4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
@@ -1,9 +1,6 @@
package hudson.plugins.accurev;

import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.*;
import hudson.Launcher.ProcStarter;
import hudson.model.Computer;
import hudson.model.Node;
@@ -21,7 +18,6 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -264,7 +260,7 @@ private static ProcStarter createProcess(
@Nonnull final OutputStream stdoutStream,
@Nonnull final OutputStream stderrStream) throws IOException, InterruptedException {
String accurevPath = findAccurevExe(directoryToRunCommandFrom, environmentVariables, launcher);
if (accurevPath == null) accurevPath = "accurev";
if (StringUtils.isBlank(accurevPath)) accurevPath = "accurev";
if (!machineReadableCommand.toString().contains(accurevPath)) machineReadableCommand.prepend(accurevPath);
ProcStarter starter = launcher.launch().cmds(machineReadableCommand);
Node n = workspaceToNode(directoryToRunCommandFrom);
@@ -407,10 +403,11 @@ public static EnvVars buildEnvironment(Node node, TaskListener listener) throws
return env;
}

private static String separator(FilePath workspace) {
return isUnix(workspace) ? "/" : "\\";
private static String separator(Launcher launcher) {
return launcher.isUnix() ? "/" : "\\";
}

@CheckForNull
private static synchronized String findAccurevExe(FilePath workspace, EnvVars e, Launcher launcher) {
Computer computer = workspace.toComputer();
String name = null;
@@ -421,7 +418,7 @@ private static synchronized String findAccurevExe(FilePath workspace, EnvVars e,
return executables.get(name);
}
if (e.containsKey("ACCUREV_BIN")) {
exe = e.get("ACCUREV_BIN") + separator(workspace) + binName;
exe = e.get("ACCUREV_BIN") + separator(launcher) + binName;
if (justAccurev(launcher, exe)) {
executables.put(name, exe);
return exe;
@@ -434,7 +431,7 @@ private static synchronized String findAccurevExe(FilePath workspace, EnvVars e,
return exe;
}
}
if (isUnix(workspace)) {
if (launcher.isUnix()) {
exe = getExistingPath(workspace, DEFAULT_NIX_CMD_LOCATIONS);
if (justAccurev(launcher, exe)) {
executables.put(name, exe);
@@ -460,15 +457,15 @@ private static boolean justAccurev(Launcher launcher, String exe) {
}

private static String getExistingPath(FilePath p, List<String> paths) {
for (final String path : paths) {
for (final String path : Util.fixNull(paths)) {
try {
if (new FilePath(p.getChannel(), path).exists()) {
return path;
}
} catch (IOException | InterruptedException ignored) {
}
}
return "";
return "accurev";
}

@CheckForNull
@@ -479,24 +476,6 @@ public static Node workspaceToNode(FilePath workspace) {
return null != node ? node : Jenkins.getInstance();
}

public static boolean isUnix(FilePath workspace) {
// if the path represents a local path, there' no need to guess.
if (!workspace.isRemote())
return File.pathSeparatorChar != ';';

String remote = workspace.getRemote();

// note that we can't use the usual File.pathSeparator and etc., as the OS of
// the machine where this code runs and the OS that this FilePath refers to may be different.

// Windows absolute path is 'X:\...', so this is usually a good indication of Windows path
if (remote.length() > 3 && remote.charAt(1) == ':' && remote.charAt(2) == '\\')
return false;
// Windows can handle '\' as a path separator but Unix can't,
// so err on Unix side
return !remote.contains("\\");
}

/**
* Interface implemented by code that interprets the output of AccuRev
* commands.
@@ -90,7 +90,7 @@ private static boolean accurevLogin(//
}
cmd.add(server.getUsername());
if (StringUtils.isEmpty(server.getPassword())) {
if (AccurevLauncher.isUnix(workspace)) {
if (launcher.isUnix()) {
cmd.add("", true);
} else {
cmd.addQuoted("", true);

0 comments on commit a89fc08

Please sign in to comment.