ruby-changes:47724
From: mame <ko1@a...>
Date: Mon, 11 Sep 2017 21:44:58 +0900 (JST)
Subject: [ruby-changes:47724] mame:r59840 (trunk): lib/securerandom.rb: test one byte to determine urandom or openssl
mame 2017-09-11 21:44:51 +0900 (Mon, 11 Sep 2017) New Revision: 59840 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59840 Log: 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. Modified files: trunk/lib/securerandom.rb Index: lib/securerandom.rb =================================================================== --- lib/securerandom.rb (revision 59839) +++ lib/securerandom.rb (revision 59840) @@ -52,7 +52,7 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L52 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 https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L67 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/