Skip to content

Commit

Permalink
run-command: always set failed_errno in start_command
Browse files Browse the repository at this point in the history
When we fail to fork, we set the failed_errno variable to
the value of errno so it is not clobbered by later syscalls.
However, we do so in a conditional, and it is hard to see
later under what conditions the variable has a valid value.

Instead of setting it only when fork fails, let's just
always set it after forking. This is more obvious for human
readers (as we are no longer setting it as a side effect of
a strerror call), and it is more obvious to gcc, which no
longer generates a spurious -Wuninitialized warning. It also
happens to match what the WIN32 half of the #ifdef does.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Mar 21, 2013
1 parent c5d5c9a commit 25043d8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions run-command.c
Expand Up @@ -273,7 +273,7 @@ int start_command(struct child_process *cmd)
{ {
int need_in, need_out, need_err; int need_in, need_out, need_err;
int fdin[2], fdout[2], fderr[2]; int fdin[2], fdout[2], fderr[2];
int failed_errno = failed_errno; int failed_errno;
char *str; char *str;


/* /*
Expand Down Expand Up @@ -341,6 +341,7 @@ int start_command(struct child_process *cmd)
notify_pipe[0] = notify_pipe[1] = -1; notify_pipe[0] = notify_pipe[1] = -1;


cmd->pid = fork(); cmd->pid = fork();
failed_errno = errno;
if (!cmd->pid) { if (!cmd->pid) {
/* /*
* Redirect the channel to write syscall error messages to * Redirect the channel to write syscall error messages to
Expand Down Expand Up @@ -420,7 +421,7 @@ int start_command(struct child_process *cmd)
} }
if (cmd->pid < 0) if (cmd->pid < 0)
error("cannot fork() for %s: %s", cmd->argv[0], error("cannot fork() for %s: %s", cmd->argv[0],
strerror(failed_errno = errno)); strerror(errno));
else if (cmd->clean_on_exit) else if (cmd->clean_on_exit)
mark_child_for_cleanup(cmd->pid); mark_child_for_cleanup(cmd->pid);


Expand Down

0 comments on commit 25043d8

Please sign in to comment.