-
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.
Since POSIX removed rand_r(3), provide fallback (re: af6a32d)
As of the referenced commit, the rand_r(3) function is used for $RANDOM instead of rand(3). This is necessary to keep repeatable seeded $RANDOM sequences working in the shell -- particularly in combination with virtual subshells, which require us to save and restore the intermediate seed value. Only rand_r will allow us to access and control that seed value so we can save it. However, POSIX removed rand_r in the 2024 edition, so we cannot count on operating systems continuing to provide it forever. src/lib/libast/features/{lib,sys}: - Add lib test for rand_r, checking if it exists in OS libraries. Defines _lib_rand_r as 1 if found. - Add extern test for rand_r, checking if the extern declaration is found in the system headers and emitting one if not. This is needed because some operating systems (e.g., NetBSD) keep deprecated functions in their libraries for ABI compatibility purposes while hiding or removing their header declarations. src/cmd/ksh93/sh/{init,subshell}.c: - Add rand_r fallback implementation (nicked from FreeBSD libc) if !_lib_rand_r (i.e., rand_r is not found in system libraries). - Since this is a static function (no need for extern here as it's only used in init.c), use a #define to rename it to _ksh_rand_r to avoid a conflict with the extern declaration. - Eliminate pointless srand(3) calls. It seeds rand(3) sequences; it's irrelevant to rand_r(3) because we give it a pointer to our own seed variable! All we need to do is assign to that. - Eliminate an init-time loop that calls rand(3) to check whether it returns large values. This is not inefficient, and now invalid in case we provide our own rand_r whose range may differ from the OS's rand. Determine this at compile time instead, using the RAND_MAX macro (which we redefine when providing our own rand_r). Resolves: #806
- Loading branch information
Showing
4 changed files
with
46 additions
and
16 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