From ccbebcd9aeb9c9d465a3e1204596cc6d0e145278 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Sun, 21 Aug 2011 13:38:33 -0700 Subject: [PATCH] sh: Do parameter expansion on ENV before using it. This is required by POSIX, and allows things like ENV=\$HOME/.shrc. Obtained-from: FreeBSD 222957 --- bin/sh/main.c | 12 +++++++++--- bin/sh/sh.1 | 6 +++--- tools/regression/bin/sh/parameters/env1.0 | 11 +++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 tools/regression/bin/sh/parameters/env1.0 diff --git a/bin/sh/main.c b/bin/sh/main.c index 327335d441..1a8ffdb4df 100644 --- a/bin/sh/main.c +++ b/bin/sh/main.c @@ -35,7 +35,7 @@ * * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. * @(#)main.c 8.6 (Berkeley) 5/28/95 - * $FreeBSD: src/bin/sh/main.c,v 1.49 2011/06/04 15:05:52 jilles Exp $ + * $FreeBSD: src/bin/sh/main.c,v 1.50 2011/06/10 22:42:00 jilles Exp $ */ #include @@ -86,7 +86,7 @@ static const char *find_dot_file(const char *); int main(int argc, char *argv[]) { - struct stackmark smark; + struct stackmark smark, smark2; volatile int state; char *shinit; @@ -133,6 +133,7 @@ main(int argc, char *argv[]) rootshell = 1; init(); setstackmark(&smark); + setstackmark(&smark2); procargs(argc, argv); pwd_init(iflag); if (iflag) @@ -157,6 +158,7 @@ state2: } state3: state = 4; + popstackmark(&smark2); if (minusc) { evalstring(minusc, sflag ? 0 : EV_EXIT); } @@ -232,9 +234,13 @@ static void read_profile(const char *name) { int fd; + const char *expandedname; + expandedname = expandstr(__DECONST(char *, 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 05e9ecf15b..6a5b05195c 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -34,7 +34,7 @@ .\" SUCH DAMAGE. .\" .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 -.\" $FreeBSD: src/bin/sh/sh.1,v 1.166 2011/06/09 23:12:23 jilles Exp $ +.\" $FreeBSD: src/bin/sh/sh.1,v 1.167 2011/06/10 22:42:00 jilles Exp $ .\" .Dd August 21, 2011 .Dt SH 1 @@ -128,8 +128,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 0000000000..7181bfd2d5 --- /dev/null +++ b/tools/regression/bin/sh/parameters/env1.0 @@ -0,0 +1,11 @@ +# $FreeBSD: src/tools/regression/bin/sh/parameters/env1.0,v 1.1 2011/06/10 22:42:00 jilles Exp $ + +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 -- 2.41.0