Skip to content

Commit

Permalink
[JENKINS-41266] Fix ArrayIndexOutOfBoundsException (#41)
Browse files Browse the repository at this point in the history
* 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 a89fc08
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
39 changes: 9 additions & 30 deletions src/main/java/hudson/plugins/accurev/AccurevLauncher.java
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/accurev/cmd/Login.java
Expand Up @@ -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);
Expand Down

0 comments on commit a89fc08

Please sign in to comment.