diff options
author | jilles <jilles@FreeBSD.org> | 2014-10-26 17:50:33 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2014-10-26 17:50:33 +0000 |
commit | bc5702addf6a68dc471c6dd3884ca164587b8271 (patch) | |
tree | 8484fb30e43bfd82dc96d44ef76a402bd991793c /bin/sh/tests | |
parent | 0963d8042b3129788a04f3b99596ffeef457dc3f (diff) | |
download | FreeBSD-src-bc5702addf6a68dc471c6dd3884ca164587b8271.zip FreeBSD-src-bc5702addf6a68dc471c6dd3884ca164587b8271.tar.gz |
sh: Make getopts memory-safe if with changing arguments.
POSIX does not permit to continuing a getopts loop with different
arguments. For parsing the positional parameters, we handle this case by
resetting the getopts state when the positional parameters are changed in
any way (and the getopts state is local to a function). However, in the
syntax getopts <optstring> <var> <arg...>, changes could lead to invalid
memory access.
In the syntax getopts <optstring> <var> <arg...>, store a copy of the
arguments and continue to use them until getopts is reset.
Diffstat (limited to 'bin/sh/tests')
-rw-r--r-- | bin/sh/tests/builtins/getopts9.0 | 9 | ||||
-rw-r--r-- | bin/sh/tests/builtins/getopts9.0.stdout | 3 |
2 files changed, 12 insertions, 0 deletions
diff --git a/bin/sh/tests/builtins/getopts9.0 b/bin/sh/tests/builtins/getopts9.0 new file mode 100644 index 0000000..d23fc43 --- /dev/null +++ b/bin/sh/tests/builtins/getopts9.0 @@ -0,0 +1,9 @@ +# $FreeBSD$ + +args='-ab' +getopts ab opt $args +echo $?:$opt:$OPTARG +for dummy in dummy1 dummy2; do + getopts ab opt $args + echo $?:$opt:$OPTARG +done diff --git a/bin/sh/tests/builtins/getopts9.0.stdout b/bin/sh/tests/builtins/getopts9.0.stdout new file mode 100644 index 0000000..4d32063 --- /dev/null +++ b/bin/sh/tests/builtins/getopts9.0.stdout @@ -0,0 +1,3 @@ +0:a: +0:b: +1:?: |