aboutsummaryrefslogtreecommitdiffstats
path: root/lib/securerandom.rb
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-11 12:44:51 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-11 12:44:51 +0000
commit39ab580fc5f49e6140caf8c7914f98974bcef0bd (patch)
tree87de6ec738768636d507c0a31eae09a1b47037a6 /lib/securerandom.rb
parentab27946a00392ce6f05cc3ac092f3f21c2b8111d (diff)
downloadruby-39ab580fc5f49e6140caf8c7914f98974bcef0bd.tar.gz
lib/securerandom.rb: test one byte to determine urandom or openssl
`SecureRandom#gen_random` determines whether urandom is available or not by trying `Random.urandom(n)`. But, when n = 0, `Random.urandom(0)` always succeeds even if urandom is not available, which leads to a wrong decision. When failed, `Random.urandom` returns nil instead of returning a shorter string than required. So the check for `ret.length != n` is not needed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59840 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/securerandom.rb')
-rw-r--r--lib/securerandom.rb6
1 files changed, 1 insertions, 5 deletions
diff --git a/lib/securerandom.rb b/lib/securerandom.rb
index dc7584a277..2140a7e1fc 100644
--- a/lib/securerandom.rb
+++ b/lib/securerandom.rb
@@ -52,7 +52,7 @@ module SecureRandom
end
def gen_random(n)
- ret = Random.urandom(n)
+ ret = Random.urandom(1)
if ret.nil?
begin
require 'openssl'
@@ -67,10 +67,6 @@ module SecureRandom
end
return gen_random(n)
end
- elsif ret.length != n
- raise NotImplementedError, \
- "Unexpected partial read from random device: " \
- "only #{ret.length} for #{n} bytes"
else
@rng_chooser.synchronize do
class << self