From 119fb2a288a48b566f5b2c195edfdb980f876d76 Mon Sep 17 00:00:00 2001 From: Xin LI Date: Tue, 1 Jul 2025 22:09:29 -0700 Subject: [PATCH] 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(-) create mode 100644 bin/sh/tests/builtins/cd12.0 create mode 100644 bin/sh/tests/builtins/cd12.0.stderr diff --git a/bin/sh/cd.c b/bin/sh/cd.c index b908c4320c0..6f97bff3c9f 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 ac959b17c63..7fdecb23c81 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 00000000000..34bc053afd1 --- /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 00000000000..cce88588b90 --- /dev/null +++ b/bin/sh/tests/builtins/cd12.0.stderr @@ -0,0 +1 @@ +cd: : No such file or directory