From 350b52ea4e686ba3c51fd2ec72b1eb9a69431954 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 18 Feb 2021 23:24:50 +0000 Subject: [PATCH] Update `comsub`-with-alias anti-leak hack (re: fe20311f) In the 93v- beta, they add a newline instead of a space. This has fewer side effects as final newlines get stripped. It's still a hack and it would still be nice to have a real fix, but it seems even the AT&T guys couldn't come up with one. src/cmd/ksh93/sh/macro.c: - To somehow avoid a memory leak involving alias substitution, append a linefeed instead of a space to the comsub buffer. src/cmd/ksh93/tests/subshell.sh: - Add test for minor regression caused by the RedHat version. --- src/cmd/ksh93/sh/macro.c | 2 +- src/cmd/ksh93/tests/subshell.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cmd/ksh93/sh/macro.c b/src/cmd/ksh93/sh/macro.c index 82dfeed13..9ab89b104 100644 --- a/src/cmd/ksh93/sh/macro.c +++ b/src/cmd/ksh93/sh/macro.c @@ -2118,7 +2118,7 @@ static void comsubst(Mac_t *mp,register Shnode_t* t, int type) } sfputc(stkp,c); } - sfputc(stkp,' '); /* rhbz#982142: a=`some_alias` leaked memory, a=`some_alias ` did not! TODO: non-hack fix */ + sfputc(stkp,'\n'); /* a=`some_alias` leaked memory, a=`some_alias` did not! TODO: non-hack fix */ c = stktell(stkp); str=stkfreeze(stkp,1); /* disable verbose and don't save in history file */ diff --git a/src/cmd/ksh93/tests/subshell.sh b/src/cmd/ksh93/tests/subshell.sh index 1f941e42a..450f44fa7 100755 --- a/src/cmd/ksh93/tests/subshell.sh +++ b/src/cmd/ksh93/tests/subshell.sh @@ -950,5 +950,15 @@ v=main (v=sub; (d=${ v=shared; }; [[ $v == shared ]]) ) || err_exit "shared comsub in nested subshell wrongly scoped (2)" [[ $v == main ]] || err_exit "shared comsub leaks out of subshell (7)" +# ====== +# After the memory leak patch for rhbz#982142, this minor regression was introduced: +# if a backtick command substitution expanded an alias, an extra space was inserted. +exp='word' +alias a='print -n wo\' +got=$(eval 'echo "`a`rd"') +unalias a +[[ $got == "$exp" ]] || err_exit 'backtick comsub with alias:' \ + "expected $(printf %q "$exp"), got $(printf %q "$got")" + # ====== exit $((Errors<125?Errors:125))