util.random: Test whether util.crand works before using it (fix #1734) 0.12
authorKim Alvefur <zash@zash.se>
Sat, 02 Apr 2022 16:33:27 +0200
branch0.12
changeset 12450 e54b8a5e35ad
parent 12448 b33558969b3e
child 12451 07d25714c40c
child 12452 fb7e76c1ad1c
util.random: Test whether util.crand works before using it (fix #1734) util.crand can be configured at compile time to use the Linux getrandom() system call, available from Linux 3.17, but it is still possible to load it with an older kernel lacking that system call, where attempting to use it throws an ENOSYS error. By testing for this on load we can fall back to /dev/urandom in this case.
util/random.lua
--- a/util/random.lua	Mon Mar 28 14:53:24 2022 +0100
+++ b/util/random.lua	Sat Apr 02 16:33:27 2022 +0200
@@ -7,7 +7,7 @@
 --
 
 local ok, crand = pcall(require, "util.crand");
-if ok then return crand; end
+if ok and pcall(crand.bytes, 1) then return crand; end
 
 local urandom, urandom_err = io.open("/dev/urandom", "r");