Project

General

Profile

Bug #3341

Random#randにTimeのRangeを渡すとSEGVする

Added by ryoqun (Ryo Onodera) over 10 years ago. Updated over 9 years ago.

Status:
Closed
Priority:
Normal
Target version:
ruby -v:
ruby 1.9.3dev (2010-05-25 trunk 28010) [x86_64-linux]
Backport:
[ruby-dev:41410]

Description

=begin
下の様なコマンドでSEGVします。Time.nowである必要はなくて、とにかくTimeなら何でも落ちます。

$ ruby -e 'Random.new.rand(Time.now..Time.now)'
-e:1: [BUG] Segmentation fault
ruby 1.9.3dev (2010-05-25 trunk 28010) [x86_64-linux]

-- control frame ----------
c:0004 p:---- s:0010 b:0010 l:000009 d:000009 CFUNC :(null)
c:0003 p:0053 s:0006 b:0006 l:000978 d:001638 EVAL -e:1
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:000978 d:000978 TOP


-- Ruby level backtrace information ----------------------------------------
-e:1:in `'
Segmentation fault
=end

#1

Updated by znz (Kazuhiro NISHIYAMA) over 10 years ago

=begin
range.end - range.begin が Float なら落ちるようで
ruby-trunk -e ';x=Object.new; def x.-(o);1.1;end; Random.new.rand(x..x)'
でも落ちました。

random_rand で

  • v = range_values(vmax, &beg, &excl) が Time.now-Time.now で Float になる
  • (vがgenrand_real2(&rnd->mt)をかけたものになる)
  • RFLOAT_VALUE(rb_check_to_float(beg)); で beg が Time なので RFLOAT_VALUE(nil) になって落ちる という流れのようなので、以下のパッチのように単純に beg + v にした方が Random.new.rand(Time.at(0)..Time.now) のような使い方も出来て良いのではないでしょうか?

diff --git a/random.c b/random.c
index b2abca7..17cdf49 100644
--- a/random.c
+++ b/random.c
@@ -1074,15 +1074,7 @@ random_rand(int argc, VALUE *argv, VALUE obj)
long x = FIX2LONG(beg) + FIX2LONG(v);
return LONG2NUM(x);
}

  • switch (TYPE(v)) {
  • case T_BIGNUM:
  • return rb_big_plus(v, beg);
  • case T_FLOAT:
  • RFLOAT_VALUE(v) += RFLOAT_VALUE(rb_check_to_float(beg));
  • return v;
  • default:
  • return rb_funcall2(v, id_plus, 1, &beg);
  • }
  • return rb_funcall2(beg, id_plus, 1, &v); }

/*

=end

#2

Updated by mrkn (Kenta Murata) over 10 years ago

  • Status changed from Open to Assigned
  • Assignee set to nobu (Nobuyoshi Nakada)

=begin

=end

#3

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Status changed from Assigned to Closed

=begin
This issue was solved with changeset r28018.
Ryo, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

#4

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Category set to core
  • Target version set to 1.9.2

=begin

=end

Also available in: Atom PDF