-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Resolves: #285
- Loading branch information
Showing
10 changed files
with
139 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters