From 494d112bdbcf18cda70c3b3198bb8dfc7c7924e6 Mon Sep 17 00:00:00 2001 From: jilles Date: Sun, 15 Mar 2015 11:43:51 +0000 Subject: [PATCH] MFC r279779: env: Fix crash when -S string is not empty but no operand follows. split_spaces() set argc in main() incorrectly, which caused trouble for getopt(). Examples: env -S '\c' env -S -i PR: 197769 git-svn-id: svn://svn.freebsd.org/base/stable/10@280027 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- .../regression/usr.bin/env/regress-env.rgdata | 33 +++++++++++++++++++ tools/regression/usr.bin/env/regress-sb.rb | 1 + usr.bin/env/envopts.c | 4 +-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/tools/regression/usr.bin/env/regress-env.rgdata b/tools/regression/usr.bin/env/regress-env.rgdata index e060eac6f..90c296e5c 100644 --- a/tools/regression/usr.bin/env/regress-env.rgdata +++ b/tools/regression/usr.bin/env/regress-env.rgdata @@ -382,3 +382,36 @@ gblenv=OUTSIDEVAR=OutsideValue setenv:D=D_ThisisAlongstring_D1 stdout:A_ThisisAlongstring_A1 B_ThisisAlongstring_B1 C_ThisisAlongstring_C1 D_ThisisAlongstring_D1 ScriptName: [%-script.pathname-%] [run] + +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -S '\c' >/dev/null +[run] +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -S'\c' >/dev/null +[run] +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -u foo -S '\c' >/dev/null +[run] +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -u foo -S'\c' >/dev/null +[run] +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -S '-u bar \c' >/dev/null +[run] +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -S'-u bar \c' >/dev/null +[run] +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -u foo -S '-u bar \c' >/dev/null +[run] +[test] + sb_args:sh + script:[%-testpgm.pathname-%] -u foo -S'-u bar \c' >/dev/null +[run] diff --git a/tools/regression/usr.bin/env/regress-sb.rb b/tools/regression/usr.bin/env/regress-sb.rb index f6333aa67..b43ddd3b6 100644 --- a/tools/regression/usr.bin/env/regress-sb.rb +++ b/tools/regression/usr.bin/env/regress-sb.rb @@ -346,6 +346,7 @@ class RGTestOptions # "just anything" that matches the general pattern. There are # no blanks in the recognized values, but I use an x-tended # regexp and then add blanks to make it more readable. + optval.gsub!(/\[%- testpgm\.pathname -%\]/x, $testpgm) optval.gsub!(/\[%- testpgm\.basename -%\]/x, File.basename($testpgm)) optval.gsub!(/\[%- script\.pathname -%\]/x, $scriptfile) diff --git a/usr.bin/env/envopts.c b/usr.bin/env/envopts.c index f8214305c..574006229 100644 --- a/usr.bin/env/envopts.c +++ b/usr.bin/env/envopts.c @@ -372,9 +372,9 @@ split_spaces(const char *str, int *origind, int *origc, char ***origv) *nextarg = NULL; /* Update optind/argc/argv in the calling routine */ - *origind = 1; - *origc += addcount; + *origc += addcount - *origind + 1; *origv = newargv; + *origind = 1; } /** -- 2.45.0