The branch main has been updated by delphij: URL: https://cgit.FreeBSD.org/src/commit/?id=119fb2a288a48b566f5b2c195edfdb980f876d76
commit 119fb2a288a48b566f5b2c195edfdb980f876d76 Author: Xin LI <delp...@freebsd.org> AuthorDate: 2025-07-02 05:09:29 +0000 Commit: Xin LI <delp...@freebsd.org> CommitDate: 2025-07-02 05:09:29 +0000 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 --- bin/sh/cd.c | 2 -- bin/sh/tests/builtins/Makefile | 1 + bin/sh/tests/builtins/cd12.0 | 3 +++ bin/sh/tests/builtins/cd12.0.stderr | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/sh/cd.c b/bin/sh/cd.c index b908c4320c04..6f97bff3c9f0 100644 --- a/bin/sh/cd.c +++ b/bin/sh/cd.c @@ -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) diff --git a/bin/sh/tests/builtins/Makefile b/bin/sh/tests/builtins/Makefile index ac959b17c639..7fdecb23c817 100644 --- a/bin/sh/tests/builtins/Makefile +++ b/bin/sh/tests/builtins/Makefile @@ -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 diff --git a/bin/sh/tests/builtins/cd12.0 b/bin/sh/tests/builtins/cd12.0 new file mode 100644 index 000000000000..34bc053afd1e --- /dev/null +++ b/bin/sh/tests/builtins/cd12.0 @@ -0,0 +1,3 @@ +(cd /bin) || exit 1 +cd "" && exit 1 +exit 0 diff --git a/bin/sh/tests/builtins/cd12.0.stderr b/bin/sh/tests/builtins/cd12.0.stderr new file mode 100644 index 000000000000..cce88588b909 --- /dev/null +++ b/bin/sh/tests/builtins/cd12.0.stderr @@ -0,0 +1 @@ +cd: : No such file or directory