ruby-changes:59873
From: Nobuyoshi <ko1@a...>
Date: Wed, 29 Jan 2020 10:39:05 +0900 (JST)
Subject: [ruby-changes:59873] 98f6c74b42 (master): Isolate the PRNG for tmpdir/tempfile
https://git.ruby-lang.org/ruby.git/commit/?id=98f6c74b42 From 98f6c74b429f9e8afccb000da4a50920479dffd6 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Wed, 29 Jan 2020 10:12:35 +0900 Subject: Isolate the PRNG for tmpdir/tempfile To get rid of conflicts affected by `srand`. diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb index ea1d380..c613655 100644 --- a/lib/tmpdir.rb +++ b/lib/tmpdir.rb @@ -110,6 +110,14 @@ class Dir https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L110 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 https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L131 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) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/