From 1a62d8843d4fc88455b119368a05f763392cd8cd Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Fri, 10 Jun 2011 22:42:00 +0000 Subject: [PATCH] sh: Do parameter expansion on ENV before using it. This is required by POSIX, and allows things like ENV=\$HOME/.shrc. Note that tilde expansion is explicitly not performed. --- bin/sh/main.c | 14 ++++++++++---- bin/sh/sh.1 | 6 +++--- tools/regression/bin/sh/parameters/env1.0 | 11 +++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 tools/regression/bin/sh/parameters/env1.0 diff --git a/bin/sh/main.c b/bin/sh/main.c index 9cef1b0cde2e..2135d30af77c 100644 --- a/bin/sh/main.c +++ b/bin/sh/main.c @@ -78,7 +78,7 @@ int rootshell; struct jmploc main_handler; int localeisutf8, initial_localeisutf8; -static void read_profile(const char *); +static void read_profile(char *); static char *find_dot_file(char *); /* @@ -92,7 +92,7 @@ static char *find_dot_file(char *); int main(int argc, char *argv[]) { - struct stackmark smark; + struct stackmark smark, smark2; volatile int state; char *shinit; @@ -139,6 +139,7 @@ main(int argc, char *argv[]) rootshell = 1; init(); setstackmark(&smark); + setstackmark(&smark2); procargs(argc, argv); pwd_init(iflag); if (iflag) @@ -163,6 +164,7 @@ main(int argc, char *argv[]) } state3: state = 4; + popstackmark(&smark2); if (minusc) { evalstring(minusc, sflag ? 0 : EV_EXIT); } @@ -235,12 +237,16 @@ cmdloop(int top) */ static void -read_profile(const char *name) +read_profile(char *name) { int fd; + const char *expandedname; + expandedname = expandstr(name); + if (expandedname == NULL) + return; INTOFF; - if ((fd = open(name, O_RDONLY)) >= 0) + if ((fd = open(expandedname, O_RDONLY)) >= 0) setinputfd(fd, 1); INTON; if (fd < 0) diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index 0ce07bd9f0ce..f6682e6ff5b7 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -32,7 +32,7 @@ .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 .\" $FreeBSD$ .\" -.Dd June 9, 2011 +.Dd June 10, 2011 .Dt SH 1 .Os .Sh NAME @@ -124,8 +124,8 @@ If the environment variable .Ev ENV is set on entry to a shell, or is set in the .Pa .profile -of a login shell, the shell then reads commands from the file named in -.Ev ENV . +of a login shell, the shell then subjects its value to parameter expansion +and arithmetic expansion and reads commands from the named file. Therefore, a user should place commands that are to be executed only at login time in the .Pa .profile diff --git a/tools/regression/bin/sh/parameters/env1.0 b/tools/regression/bin/sh/parameters/env1.0 new file mode 100644 index 000000000000..c0d4a2cc9141 --- /dev/null +++ b/tools/regression/bin/sh/parameters/env1.0 @@ -0,0 +1,11 @@ +# $FreeBSD$ + +export key='must contain this' +unset x +r=$(ENV="\${x?\$key}" ${SH} -i +m 2>&1 >/dev/null <<\EOF +exit 0 +EOF +) && case $r in +*"$key"*) true ;; +*) false ;; +esac