From 56913f8c2ae26c625c4e02b91912dda4c0ddee24 Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Sun, 4 Apr 2021 14:18:43 -0700 Subject: [PATCH] Fix bugs related to 'uname -d' in the 'uname' builtin (#251) This commit fixes a bug in the ksh uname builtin's -d option that could change the output of -o (I was only able to reproduce this on Linux): $ builtin uname $ uname -o GNU/Linux $ uname -d (none) $ uname -o (none) I identified this patch from ksh2020 as a fix for this bug: The linked patch was meant to fix a crash in 'uname -d', although I've had no luck reproducing it: src/lib/libcmd/uname.c: - Pass correct buffer to getdomainname() while executing uname -d. src/cmd/ksh93/tests/builtins.sh: - Add a regression test for the reported 'uname -d' crash. - Add a regression test for the output of 'uname -o' after 'uname -d'. - To handle potential crashes when running the regression tests in older versions of ksh, fork the command substitutions that run 'uname -d'. --- NEWS | 8 ++++++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/tests/builtins.sh | 17 +++++++++++++++++ src/lib/libcmd/uname.c | 5 ++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index dc1f643b3..5a095c2a7 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,14 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2021-04-03: + +- Fixed a bug that caused the uname builtin's -d option to change the output + of the -o option. + +- Fixed a possible crash that could occur when showing the domain name + with the uname builtin's -d option. + 2021-03-31: - Fixed a bug that caused 'cd -' to ignore the current value of $OLDPWD diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 8f65a6c10..3780e731f 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -20,7 +20,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2021-03-31" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2021-04-03" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2021 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/tests/builtins.sh b/src/cmd/ksh93/tests/builtins.sh index 8d5ca5004..b25e37893 100755 --- a/src/cmd/ksh93/tests/builtins.sh +++ b/src/cmd/ksh93/tests/builtins.sh @@ -1139,5 +1139,22 @@ got=$( err_exit "OLDPWD and/or PWD fail to survive subshare" \ "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +# ====== +# Test for bugs related to 'uname -d' +# https://github.com/att/ast/pull/1187 +builtin uname +exp=$(uname -o) + +# Test for a possible crash (to avoid crashing the script, fork the subshell) +( + ulimit -t unlimited + uname -d > /dev/null +) || err_exit "'uname -d' crashes" + +# 'uname -d' shouldn't change the output of 'uname -o' +got=$(ulimit -t unlimited; uname -d > /dev/null; uname -o) +[[ $exp == $got ]] || err_exit "'uname -d' changes the output of 'uname -o'" \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" + # ====== exit $((Errors<125?Errors:125)) diff --git a/src/lib/libcmd/uname.c b/src/lib/libcmd/uname.c index bd9c09253..931f7ba38 100644 --- a/src/lib/libcmd/uname.c +++ b/src/lib/libcmd/uname.c @@ -493,11 +493,14 @@ b_uname(int argc, char** argv, Shbltin_t* context) if (flags & OPT_domain) { if (!*(s = astconf("SRPC_DOMAIN", NiL, NiL))) + { #if _lib_getdomainname - getdomainname(s, sizeof(buf)); + getdomainname(buf, sizeof(buf)); + s = buf; #else /*NOP*/; #endif + } output(OPT_domain, s, "domain"); } #if _mem_m_type_utsname