From 129e1ce72c28ec14aee459cceffc25ed9d5cae85 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 11 Aug 2017 17:00:39 +0200 Subject: hush: add a test which fails due to uclibc bug in getopt() Signed-off-by: Denys Vlasenko --- .../ash-getopts/getopt_test_libc_bug.tests | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 shell/ash_test/ash-getopts/getopt_test_libc_bug.tests (limited to 'shell/ash_test/ash-getopts/getopt_test_libc_bug.tests') diff --git a/shell/ash_test/ash-getopts/getopt_test_libc_bug.tests b/shell/ash_test/ash-getopts/getopt_test_libc_bug.tests new file mode 100755 index 000000000..6c0781f20 --- /dev/null +++ b/shell/ash_test/ash-getopts/getopt_test_libc_bug.tests @@ -0,0 +1,38 @@ +# This test can fail with libc with buggy getopt() implementation. +# If getopt() wants to parse multi-option args (-abc), +# it needs to remember a position withit current arg. +# +# If this position is kept as a POINTER, not an offset, +# and if argv[] ADDRESSES (not contents!) change, it blows up. + +echo "*** optstring:'ac' args:-a -b -c -d e" +getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" + +# Above: args are (usually) in the same locations in memory. +# Below: variable allocations change the location. + +echo +echo "*** optstring:'ac' args:-a -b -c -d e" +unset OPTIND +OPTARG=QWERTY; getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +NEWVAR=NEWVAL; getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +VAR111=NEWVAL; getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +VAR222=NEWVAL; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +VAR333=NEWVAL; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" + +# Sligntly different attempts to force reallocations + +echo +echo "*** optstring:'ac' args:-a -b -c -d e" +unset OPTIND +export OPTARG; getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +export NEWVAR; getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +export VAR111; getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +export VAR222; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" +export VAR333; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" + +# All copies of code above should generate identical output -- cgit v1.2.3