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

Reject empty operand in cd built-in #126

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
of mode.
- The `true`, `false`, and `pwd` built-ins are now substitutive
built-ins.
- The `cd` built-in now exits with status code 5, when an empty
operand is supplied.
- [line-editing] Updated the completion scripts to support Git
2.48.1.
- [line-editing] Added the completion script for `git-mv`.
Expand Down
3 changes: 2 additions & 1 deletion doc/_cd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ specified either, the default is the link:params.html#sv-home[home directory].
* If {{directory}} cannot be processed because of an unset or empty
link:params.html#sv-home[+HOME+] or link:params.html#sv-oldpwd[+OLDPWD+],
the exit status is 4.
* If the command arguments are invalid, the exit status is 5.
* If the command arguments are invalid or if an empty operand is supplied
for {{directory}}, the exit status is 5.

If and only if the exit status is 2 or larger, the working directory is not
changed.
Expand Down
5 changes: 5 additions & 0 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,11 @@ int change_directory(

assert(!logical || !ensure_pwd);

if (newpwd[0] == L'\0') {
xerror(0, Ngt("empty directory name"));
return 5;
}

/* get the current value of $PWD as `origpwd' */
origpwd = getvar(L VAR_PWD);
if (origpwd == NULL || origpwd[0] != L'/') {
Expand Down
11 changes: 3 additions & 8 deletions tests/cd-y.tst
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ __IN__
cd: $HOME is not set
__ERR__

testcase "$LINENO" 'empty operand is like "."' \
3<<\__IN__ 5</dev/null 4<<__OUT__
unset CDPATH
test_Oe -e 5 'empty operand'
cd ''
echo ---
pwd
__IN__
---
$ORIGPWD
__OUT__
cd: empty directory name
__ERR__

testcase "$LINENO" 'unset PWD' \
3<<\__IN__ 5</dev/null 4<<__OUT__
Expand Down