Skip to content

Commit 70b55ac

Browse files
authored
Reject empty operand in cd built-in (#126)
2 parents f036450 + 5d4565b commit 70b55ac

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
of mode.
2020
- The `true`, `false`, and `pwd` built-ins are now substitutive
2121
built-ins.
22+
- The `cd` built-in now exits with status code 5, when an empty
23+
operand is supplied.
2224
- [line-editing] Updated the completion scripts to support Git
2325
2.48.1.
2426
- [line-editing] Added the completion script for `git-mv`.

doc/_cd.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ specified either, the default is the link:params.html#sv-home[home directory].
106106
* If {{directory}} cannot be processed because of an unset or empty
107107
link:params.html#sv-home[+HOME+] or link:params.html#sv-oldpwd[+OLDPWD+],
108108
the exit status is 4.
109-
* If the command arguments are invalid, the exit status is 5.
109+
* If the command arguments are invalid or if an empty operand is supplied
110+
for {{directory}}, the exit status is 5.
110111

111112
If and only if the exit status is 2 or larger, the working directory is not
112113
changed.

path.c

+5
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,11 @@ int change_directory(
12321232

12331233
assert(!logical || !ensure_pwd);
12341234

1235+
if (newpwd[0] == L'\0') {
1236+
xerror(0, Ngt("empty directory name"));
1237+
return 5;
1238+
}
1239+
12351240
/* get the current value of $PWD as `origpwd' */
12361241
origpwd = getvar(L VAR_PWD);
12371242
if (origpwd == NULL || origpwd[0] != L'/') {

tests/cd-y.tst

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@ __IN__
1818
cd: $HOME is not set
1919
__ERR__
2020

21-
testcase "$LINENO" 'empty operand is like "."' \
22-
3<<\__IN__ 5</dev/null 4<<__OUT__
23-
unset CDPATH
21+
test_Oe -e 5 'empty operand'
2422
cd ''
25-
echo ---
26-
pwd
2723
__IN__
28-
---
29-
$ORIGPWD
30-
__OUT__
24+
cd: empty directory name
25+
__ERR__
3126

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

0 commit comments

Comments
 (0)