Skip to content

Commit e43daa4

Browse files
nobuhsbt
authored andcommitted
Isolate the PRNG for tmpdir/tempfile
To get rid of conflicts affected by `srand`.
1 parent 82ba4c9 commit e43daa4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/tmpdir.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ def tmpdir
110110

111111
UNUSABLE_CHARS = [File::SEPARATOR, File::ALT_SEPARATOR, File::PATH_SEPARATOR, ":"].uniq.join("").freeze
112112

113+
class << (RANDOM = Random.new)
114+
MAX = 36**6 # < 0x100000000
115+
def next
116+
rand(MAX).to_s(36)
117+
end
118+
end
119+
private_constant :RANDOM
120+
113121
def create(basename, tmpdir=nil, max_try: nil, **opts)
114122
origdir = tmpdir
115123
tmpdir ||= tmpdir()
@@ -123,7 +131,7 @@ def create(basename, tmpdir=nil, max_try: nil, **opts)
123131
suffix &&= suffix.delete(UNUSABLE_CHARS)
124132
begin
125133
t = Time.now.strftime("%Y%m%d")
126-
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"\
134+
path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\
127135
"#{n ? %[-#{n}] : ''}#{suffix||''}"
128136
path = File.join(tmpdir, path)
129137
yield(path, n, opts, origdir)

0 commit comments

Comments
 (0)