sh(1): Do not interpret chdir to "" as equivalent to chdir with no argument

A script that does the following:

    cd "${dir}" || exit 1

would incorrectly remain in the current directory when `${dir}` is
an empty string under the current implementation. This behavior,
while historical, is potentially dangerous, as it is likely not
what the script author intended.

Change the command to treat an empty string as an error and emit a
diagnostic message to standard error, as required by
IEEE Std 1003.1-2024.

PR:		standards/287440
Test Plan:	kyua test bin/sh
Relnotes:	yes
Differential Revision: https://reviews.freebsd.org/D50974
This commit is contained in:
Xin LI
2025-07-01 22:09:29 -07:00
parent 65bc8e9192
commit 119fb2a288
4 changed files with 5 additions and 2 deletions
-2
View File
@@ -99,8 +99,6 @@ cdcmd(int argc __unused, char **argv __unused)
if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
error("HOME not set");
if (*dest == '\0')
dest = ".";
if (dest[0] == '-' && dest[1] == '\0') {
dest = bltinlookup("OLDPWD", 1);
if (dest == NULL)
+1
View File
@@ -52,6 +52,7 @@ ${PACKAGE}FILES+= cd8.0
${PACKAGE}FILES+= cd9.0 cd9.0.stdout
${PACKAGE}FILES+= cd10.0
${PACKAGE}FILES+= cd11.0
${PACKAGE}FILES+= cd12.0 cd12.0.stderr
${PACKAGE}FILES+= command1.0
${PACKAGE}FILES+= command2.0
${PACKAGE}FILES+= command3.0
+3
View File
@@ -0,0 +1,3 @@
(cd /bin) || exit 1
cd "" && exit 1
exit 0
+1
View File
@@ -0,0 +1 @@
cd: : No such file or directory