From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1676236-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id ABDD91581D0
	for <garchives@archives.gentoo.org>; Sat,  5 Oct 2024 04:15:26 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id D3B37E29E3;
	Sat,  5 Oct 2024 04:15:25 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id BD343E29E3
	for <gentoo-commits@lists.gentoo.org>; Sat,  5 Oct 2024 04:15:25 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 0B1F7340C77
	for <gentoo-commits@lists.gentoo.org>; Sat,  5 Oct 2024 04:15:25 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 30A3C22DA
	for <gentoo-commits@lists.gentoo.org>; Sat,  5 Oct 2024 04:15:22 +0000 (UTC)
From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" <sam@gentoo.org>
Message-ID: <1724243787.d51d103cb345d3ba96af3c4b5bba9eeb13fe55f7.sam@gentoo>
Subject: [gentoo-commits] proj/gentoo-functions:master commit in: /
X-VCS-Repository: proj/gentoo-functions
X-VCS-Files: functions.sh
X-VCS-Directories: /
X-VCS-Committer: sam
X-VCS-Committer-Name: Sam James
X-VCS-Revision: d51d103cb345d3ba96af3c4b5bba9eeb13fe55f7
X-VCS-Branch: master
Date: Sat,  5 Oct 2024 04:15:22 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 4c436112-3e6c-4bf5-bc57-ec7c7381bf6d
X-Archives-Hash: 3aa96dfc42a7b5c673714efdc26a2947

commit:     d51d103cb345d3ba96af3c4b5bba9eeb13fe55f7
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue Aug 13 03:36:37 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Aug 21 12:36:27 2024 +0000
URL:        https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=d51d103c

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 <AT> plushkava.net>

 functions.sh | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

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
 }