mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-02-15 04:32:24 +00:00
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 |
||
---|---|---|
.. | ||
builtin | ||
INIT | ||
ksh93 | ||
Mamfile |