summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-08-13 04:36:37 +0100
committerKerin Millar <kfm@plushkava.net>2024-08-21 13:36:27 +0100
commitd51d103cb345d3ba96af3c4b5bba9eeb13fe55f7 (patch)
tree203d2a66d55178c04715460d5759204a897e844c
parentReduce the two non-bash srandom() implementations to just one (diff)
downloadgentoo-functions-d51d103cb345d3ba96af3c4b5bba9eeb13fe55f7.tar.gz
gentoo-functions-d51d103cb345d3ba96af3c4b5bba9eeb13fe55f7.tar.bz2
gentoo-functions-d51d103cb345d3ba96af3c4b5bba9eeb13fe55f7.zip
Check for SRANDOM support in a safer manner
Given that the SRANDOM variable loses its special properties if unset, to compare two expansions of it to one another ought to be more robust. Do so up to three times, so as not to be foiled by the unlikely event of the RNG repeating the same number. Further, the prior check was defective because it incorrectly presumed the minimum required version of bash to be 5.0 rather than 5.1. Fixes: 5ee035a364bea8d12bc8abfe769014e230a212a6 Signed-off-by: Kerin Millar <kfm@plushkava.net>
-rw-r--r--functions.sh16
1 files changed, 15 insertions, 1 deletions
diff --git a/functions.sh b/functions.sh
index 37ac8c2..a47bb2b 100644
--- a/functions.sh
+++ b/functions.sh
@@ -575,7 +575,20 @@ quote_args()
srandom()
{
# shellcheck disable=3028
- if [ "${BASH_VERSINFO:-0}" -ge 5 ]; then
+ _has_srandom()
+ {
+ # The SRANDOM variable was introduced by bash 5.1. Check for at
+ # least 5.0, letting the alternate branch confirm its efficacy.
+ if [ "${BASH_VERSINFO-0}" -lt 5 ]; then
+ false
+ else
+ for _ in 1 2 3; do
+ test "${SRANDOM}" != "${SRANDOM}" && break
+ done
+ fi
+ }
+
+ if _has_srandom; then
srandom()
{
printf '%d\n' "$(( SRANDOM >> 1 ))"
@@ -625,6 +638,7 @@ srandom()
return 1
fi
+ unset -f _has_srandom
srandom
}