Skip to content

Commit 25043d8

Browse files
peffgitster
authored andcommitted
run-command: always set failed_errno in start_command
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c5d5c9a commit 25043d8

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

run-command.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ int start_command(struct child_process *cmd)
273273
{
274274
int need_in, need_out, need_err;
275275
int fdin[2], fdout[2], fderr[2];
276-
int failed_errno = failed_errno;
276+
int failed_errno;
277277
char *str;
278278

279279
/*
@@ -341,6 +341,7 @@ int start_command(struct child_process *cmd)
341341
notify_pipe[0] = notify_pipe[1] = -1;
342342

343343
cmd->pid = fork();
344+
failed_errno = errno;
344345
if (!cmd->pid) {
345346
/*
346347
* Redirect the channel to write syscall error messages to
@@ -420,7 +421,7 @@ int start_command(struct child_process *cmd)
420421
}
421422
if (cmd->pid < 0)
422423
error("cannot fork() for %s: %s", cmd->argv[0],
423-
strerror(failed_errno = errno));
424+
strerror(errno));
424425
else if (cmd->clean_on_exit)
425426
mark_child_for_cleanup(cmd->pid);
426427

0 commit comments

Comments
 (0)