From: Brett Carswell Date: 2009-07-31T15:16:43+09:00 Subject: [ruby-core:24640] [Bug #1847] Exception in SecureRandom.random_number (Win32) Bug #1847: Exception in SecureRandom.random_number (Win32) http://redmine.ruby-lang.org/issues/show/1847 Author: Brett Carswell Status: Open, Priority: Normal ruby -v: 1.9.2dev (2009-07-31 trunk 24332) Script to reproduce: # require 'securerandom' puts SecureRandom.random_number(100000) Exception is thrown randomly based on data generated in the random_number function. It may take a couple of attempts to get the fault. Exception: C:/usr/lib/ruby/1.9.1/securerandom.rb:218:in `ord': invalid byte sequence in US- ASCII (ArgumentError) from C:/usr/lib/ruby/1.9.1/securerandom.rb:218:in `random_number' from c:/Program Files/MineOPS/bin/test_rand.rb:3:in `
' Faulting code from securerandom.rb: begin rnd = SecureRandom.random_bytes(bin.length) rnd[0] = (rnd[0].ord & mask).chr end until rnd < bin It seems like on Linux the string encoding defaults to ASCII-8BIT whereas on Windows it is US-ASCII and not all char values are valid. It could be fixed by making a change: rnd = SecureRandom.random_bytes(bin.length).force_encoding("ASCII-8BIT") ---------------------------------------- http://redmine.ruby-lang.org