Anonymous avatar Anonymous committed 2affea4

start_command: do not clobber cmd->env on Windows code path

Previously, it would not be possible to call start_command twice for the
same struct child_process that has env set.

The fix is achieved by moving the loop that modifies the environment block
into a helper function. This also allows us to make two other helper
functions static.

Signed-off-by: Johannes Sixt <>;
Signed-off-by: Junio C Hamano <>;

Comments (0)

Files changed (3)

-char **copy_environ()
+static char **copy_environ(void)
 	char **env;
 	int i = 0;
  * If name contains '=', then sets the variable, otherwise it unsets it
-char **env_setenv(char **env, const char *name)
+static char **env_setenv(char **env, const char *name)
 	char *eq = strchrnul(name, '=');
 	int i = lookup_env(env, name, eq-name);
 	return env;
+ * Copies global environ and adjusts variables as specified by vars.
+ */
+char **make_augmented_environ(const char *const *vars)
+	char **env = copy_environ();
+	while (*vars)
+		env = env_setenv(env, *vars++);
+	return env;
 /* this is the first function to call into WS_32; initialize it */
 #undef gethostbyname
 struct hostent *mingw_gethostbyname(const char *host)
  * helpers
-char **copy_environ(void);
+char **make_augmented_environ(const char *const *vars);
 void free_environ(char **env);
-char **env_setenv(char **env, const char *name);
  * A replacement of main() that ensures that argv[0] has a path
 	if (cmd->dir)
 		die("chdir in start_command() not implemented");
-	if (cmd->env) {
-		env = copy_environ();
-		for (; *cmd->env; cmd->env++)
-			env = env_setenv(env, *cmd->env);
-	}
+	if (cmd->env)
+		env = make_augmented_environ(cmd->env);
 	if (cmd->git_cmd) {
 		cmd->argv = prepare_git_cmd(cmd->argv);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.