mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
Fix $RANDOM to act consistently in subshells (#294)
This fixes the following: 1. Using $RANDOM in a virtual/non-forked subshell no longer influences the reproducible $RANDOM sequence in the parent environment. 2. When invoking a subshell $RANDOM is now re-seeded (as mksh and bash do) so that invocations in repeated subshells (including forked subshells) longer produce identical sequences by default. 3. Program flow corruption that occurred in scripts on executing ( ( simple_command & ) ). src/cmd/ksh93/include/variables.h: - Move 'struct rand' here as it will be needed in subshell.c. Add rand_seed member to save the pseudorandom generator seed. Remove the pointer to the shell state as it's redundant. src/cmd/ksh93/sh/init.c: - put_rand(): Store given seed in rand_seed while calling srand(). No longer pointlessly limit the number of possible seeds with the RANDMASK bitmask (that mask is to limit the values to 0-32767, it should not limit the number of possible sequences to 32768). - nget_rand(): Instead of using rand(), use rand_r() to update the random_seed value. This makes it possible to save/restore the current seed of the pseudorandom generator. - Add sh_reseed_rand() function that reseeds the pseudorandom generator by calling srand() with a bitwise-xor combination of the current PID, the current time with a granularity of 1/10000 seconds, and a sequence number that is increased on each invocation. - nv_init(): Set the initial seed using sh_reseed_rand() here instead of in sh_main(), as this is where the other struct rand members are initialised. src/cmd/ksh93/sh/main.c: sh_main(): - Remove the srand() call that was replaced by the sh_reseed_rand() call in init.c. src/cmd/ksh93/sh/subshell.c: sh_subshell(): - Upon entering a virtual subshell, save the current $RANDOM seed and state, then reseed $RANDOM for the subshell. - Upon exiting a virtual subshell, restore $RANDOM seed and state and reseed the generator using srand() with the restored seed. src/cmd/ksh93/sh/xec.c: sh_exec(): - When optimizing out a subshell that is the last command, still act like a subshell: reseed $RANDOM and increase ${.sh.subshell}. - Fix a separate bug discovered while implementing this. Do not optimize '( simple_command & )' when in a virtual subshell; doing this causes program flow corruption. - When optimizing '( simple_command & )', also reseed $RANDOM and increment ${.sh.subshell}. src/cmd/ksh93/tests/subshell.sh, src/cmd/ksh93/tests/variables.sh: - Add various tests for all of the above. Co-authored-by: Johnothan King <johnothanking@protonmail.com> Resolves: https://github.com/ksh93/ksh/issues/285
This commit is contained in:
parent
f31e368795
commit
af6a32d14f
10 changed files with 139 additions and 17 deletions
|
@ -26,10 +26,55 @@ unset ss
|
|||
[[ ${@ss} ]] && err_exit '${@ss} should be empty string when ss is unset'
|
||||
[[ ${!ss} == ss ]] || err_exit '${!ss} should be ss when ss is unset'
|
||||
[[ ${#ss} == 0 ]] || err_exit '${#ss} should be 0 when ss is unset'
|
||||
|
||||
# RANDOM
|
||||
if (( RANDOM==RANDOM || $RANDOM==$RANDOM ))
|
||||
then err_exit RANDOM variable not working
|
||||
fi
|
||||
# When the $RANDOM variable is used in a forked subshell, it shouldn't
|
||||
# use the same pseudorandom seed as the main shell.
|
||||
# https://github.com/ksh93/ksh/issues/285
|
||||
RANDOM=123
|
||||
function rand_print {
|
||||
ulimit -t unlimited 2> /dev/null
|
||||
print $RANDOM
|
||||
}
|
||||
integer rand1=$(rand_print)
|
||||
integer rand2=$(rand_print)
|
||||
(( rand1 == rand2 )) && err_exit "Test 1: \$RANDOM seed in subshell doesn't change" \
|
||||
"(both results are $rand1)"
|
||||
# Make sure we're actually using a different pseudorandom seed
|
||||
integer rand1=$(
|
||||
ulimit -t unlimited 2> /dev/null
|
||||
test $RANDOM
|
||||
print $RANDOM
|
||||
)
|
||||
integer rand2=${ print $RANDOM ;}
|
||||
(( rand1 == rand2 )) && err_exit "Test 2: \$RANDOM seed in subshell doesn't change" \
|
||||
"(both results are $rand1)"
|
||||
# $RANDOM should be reseeded when the final command is inside of a subshell
|
||||
rand1=$($SHELL -c 'RANDOM=1; (echo $RANDOM)')
|
||||
rand2=$($SHELL -c 'RANDOM=1; (echo $RANDOM)')
|
||||
(( rand1 == rand2 )) && err_exit "Test 3: \$RANDOM seed in subshell doesn't change" \
|
||||
"(both results are $rand1)"
|
||||
# $RANDOM should be reseeded for the ( simple_command & ) optimization
|
||||
( echo $RANDOM & ) >r1
|
||||
( echo $RANDOM & ) >r2
|
||||
sleep .01
|
||||
(( $(<r1) == $(<r2) )) && err_exit "Test 4: \$RANDOM seed in ( simple_command & ) doesn't change" \
|
||||
"(both results are $(<r1))"
|
||||
# Virtual subshells should not influence the parent shell's RANDOM sequence
|
||||
RANDOM=456
|
||||
exp="$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM"
|
||||
RANDOM=456
|
||||
got=
|
||||
for((i=0; i<5; i++))
|
||||
do : $( : $RANDOM $RANDOM $RANDOM )
|
||||
got+=${got:+ }$RANDOM
|
||||
done
|
||||
[[ $got == "$exp" ]] || err_exit 'Using $RANDOM in subshell influences reproducible sequence in parent environment' \
|
||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
|
||||
# SECONDS
|
||||
float secElapsed=0.0 secSleep=0.001
|
||||
let SECONDS=$secElapsed
|
||||
|
@ -165,7 +210,6 @@ if [[ $LANG != "$save_LANG" ]]
|
|||
then err_exit "$save_LANG locale not working"
|
||||
fi
|
||||
|
||||
unset RANDOM
|
||||
unset -n foo
|
||||
foo=junk
|
||||
function foo.get
|
||||
|
@ -703,6 +747,12 @@ actual=$(
|
|||
expect=$'4\n3\n3\n2\n1'
|
||||
[[ $actual == "$expect" ]] || err_exit "\${.sh.subshell} failure (expected $(printf %q "$expect"), got $(printf %q "$actual"))"
|
||||
|
||||
# ${.sh.subshell} should increment when the final command is inside of a subshell
|
||||
exp=1
|
||||
got=$($SHELL -c '(echo ${.sh.subshell})')
|
||||
[[ $exp == $got ]] || err_exit '${.sh.subshell} fails to increment when the final command is inside of a subshell' \
|
||||
"(expected '$exp', got '$got')"
|
||||
|
||||
unset IFS
|
||||
if ((SHOPT_BRACEPAT)) && command set -o braceexpand
|
||||
then set -- {1..32768}
|
||||
|
@ -1067,6 +1117,23 @@ $SHELL -c '
|
|||
(((e = $?) == 1)) || err_exit "typeset -l/-u doesn't work on special variables" \
|
||||
"(exit status $e$( ((e>128)) && print -n / && kill -l "$e"))"
|
||||
|
||||
# ... unset followed by launching a forked subshell
|
||||
$SHELL -c '
|
||||
errors=0
|
||||
unset -v "$@" || let errors++
|
||||
(
|
||||
ulimit -t unlimited 2>/dev/null
|
||||
for var do
|
||||
[[ $var == _ ]] && continue # only makes sense that $_ is immediately set again
|
||||
[[ -v $var ]] && let errors++
|
||||
done
|
||||
exit $((errors + 1))
|
||||
)
|
||||
exit $?
|
||||
' unset_to_fork_test "$@"
|
||||
(((e = $?) == 1)) || err_exit "Failure in unsetting one or more special variables followed by launching forked subshell" \
|
||||
"(exit status $e$( ((e>128)) && print -n / && kill -l "$e"))"
|
||||
|
||||
# ======
|
||||
# ${.sh.pid} should be the forked subshell's PID
|
||||
(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue