Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOME=/dest cd does not honour the HOME environment #602

Closed
McDutchie opened this issue Feb 16, 2023 · 2 comments
Closed

HOME=/dest cd does not honour the HOME environment #602

McDutchie opened this issue Feb 16, 2023 · 2 comments
Labels
bug Something is not working

Comments

@McDutchie
Copy link

In ksh-community/ksh#19 (comment), @Earnestly wrote:

The builtin cd does not appear to check its environment for HOME when determining where to change the working directory when given no arguments.

# HOME=/bin cd; pwd
/root # Should be /bin

This otherwise works in every shell-including bourne-and is specified by POSIX:

  1. If no directory operand is given and the HOME environment variable is set to a non-empty value, the cd utility shall behave as if the directory named in the HOME environment variable was specified as the directory operand.

I agree this should work, and it's a bug that it doesn't. cd is a regular built-in command, so the preceding assignment to HOME should be in effect while cd is executed.

Confirmed in ksh 93u+ 2012-08-01 and in current ksh 93u+m.

@McDutchie
Copy link
Author

The fix is simple: there was a missing sh_scoped() call.

diff --git a/src/cmd/ksh93/bltins/cd_pwd.c b/src/cmd/ksh93/bltins/cd_pwd.c
index 977be2bcf..9a7d25fb8 100644
--- a/src/cmd/ksh93/bltins/cd_pwd.c
+++ b/src/cmd/ksh93/bltins/cd_pwd.c
@@ -107,7 +107,7 @@ int	b_cd(int argc, char *argv[],Shbltin_t *context)
 	if(argc==2)
 		dir = sh_substitute(oldpwd,dir,argv[1]);
 	else if(!dir)
-		dir = nv_getval(HOME);
+		dir = nv_getval(sh_scoped(HOME));
 	else if(*dir == '-' && dir[1]==0)
 		dir = nv_getval(opwdnod);
 	if(!dir || *dir==0)

@McDutchie
Copy link
Author

McDutchie commented Feb 16, 2023

This didn't work either, and is also fixed by the patch above:

$ ksh -c 'function f { typeset HOME=/bin; cd; }; f; pwd' 
/Users/martijn
$ arch/darwin.arm64-64/bin/ksh -c 'function f { typeset HOME=/bin; cd; }; f; pwd' 
/bin

McDutchie added a commit that referenced this issue Feb 16, 2023
When using the default value ($HOME) in the absence of an argument,
'cd' incorrectly used the global scope of HOME regardless of local
scope. Example reproducers:

    # HOME=/bin cd; pwd
    /root				# Should be /bin

    $ function fn { typeset HOME=/tmp; cd; }
    $ fn
    $ pwd
    /Users/martijn			# Should be /tmp

src/cmd/ksh93/bltins/cd_pwd.c: b_cd():
- Add missing sh_scoped() call when retrieving the value of HOME.

Thanks to @Earnestly for the report.
Resolves: #602
@McDutchie McDutchie reopened this Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working
Projects
None yet
Development

No branches or pull requests

1 participant