From 98f6c74b429f9e8afccb000da4a50920479dffd6 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 29 Jan 2020 10:12:35 +0900 Subject: [PATCH] Isolate the PRNG for tmpdir/tempfile To get rid of conflicts affected by `srand`. --- lib/tmpdir.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb index ea1d380ef1..c61365577e 100644 --- a/lib/tmpdir.rb +++ b/lib/tmpdir.rb @@ -110,6 +110,14 @@ class Dir UNUSABLE_CHARS = [File::SEPARATOR, File::ALT_SEPARATOR, File::PATH_SEPARATOR, ":"].uniq.join("").freeze + class << (RANDOM = Random.new) + MAX = 36**6 # < 0x100000000 + def next + rand(MAX).to_s(36) + end + end + private_constant :RANDOM + def create(basename, tmpdir=nil, max_try: nil, **opts) origdir = tmpdir tmpdir ||= tmpdir() @@ -123,7 +131,7 @@ class Dir suffix &&= suffix.delete(UNUSABLE_CHARS) begin t = Time.now.strftime("%Y%m%d") - path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"\ + path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\ "#{n ? %[-#{n}] : ''}#{suffix||''}" path = File.join(tmpdir, path) yield(path, n, opts, origdir)