Skip to content

Commit

Permalink
Explain rationale for deleting the superfluous check from the if stat…
Browse files Browse the repository at this point in the history
…ement
  • Loading branch information
JohnoKing committed Dec 18, 2024
1 parent 51f027c commit b54e906
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/cmd/ksh93/sh/subshell.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,18 @@ Sfio_t *sh_subshell(Shnode_t *t, volatile int flags, int comsub)
free(savsig);
}
sh.options = sp->options;
/* restore the present working directory */
if(sh.pwdfd != sp->pwdfd)
{
if(sp->pwdfd > 0 && fchdir(sp->pwdfd) < 0)
/*
* Restore the parent shell's present working directory.
* Note: cd will always fork if sp->pwdfd is -1 (by calling sh_validate_subpwdfd()),
* which only occurs when a subshell is started with sh.pwdfd == -1. As such, in this
* if block sp->pwdfd is always > 0 (whilst sh.pwdfd is guaranteed to differ, and
* might not be valid).
*/
if(fchdir(sp->pwdfd) < 0)
{
/* Couldn't fchdir back; close the fd and cope with the error */
sh_close(sp->pwdfd);
saveerrno = errno;
fatalerror = 2;
Expand Down

0 comments on commit b54e906

Please sign in to comment.