Project

General

Profile

Actions

Backport #7903

closed

Array#sampleの引数に渡した乱数生成オブジェクトが負の数を返した場合のエラーメッセージの修正

Added by sho-h (Sho Hashimoto) about 11 years ago. Updated about 11 years ago.


Description

=begin
Array#sample に渡す乱数生成オブジェクトが負の数を返す場合、必ず「random number too big」というメッセージの RangeError が発生するようです。

gen = proc { -1 }
class << gen
alias rand call
end
[2,4,6,8,10].sample(random: gen) # => test.rb:5:in `sample': random number too big -1 (RangeError)

RAND_UPTO が呼び出している rb_random_ulong_limited の以下の処理がいずれも unsigned long のため、必ず「too big」になるのではないかと思いました。上記の例だと r に -1 を指定、limit に 4 が指定されると想像しています。

unsigned long r = NUM2ULONG(v);
if (r > limit) {
rb_raise(rb_eRangeError, "random number too big %ld", r);
}

rb_random_real 側ではチェックされているため、わかってる事なのかもしれませんが、念のためチケットを発行してみました。

パッチも作りましたが、適用される場合も NUM2LONG(v)してる行は修正していただいた方がよさそうです。
=end


Files

random.c.patch (834 Bytes) random.c.patch sho-h (Sho Hashimoto), 02/21/2013 10:23 PM

Updated by ko1 (Koichi Sasada) about 11 years ago

  • Category set to core
  • Assignee set to nobu (Nobuyoshi Nakada)
  • Target version set to 2.1.0
Actions #2

Updated by nobu (Nobuyoshi Nakada) about 11 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r39376.
Sho, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


random.c: fix error message

Actions #3

Updated by nagachika (Tomoyuki Chikanaga) about 11 years ago

  • Tracker changed from Bug to Backport
  • Project changed from Ruby master to Backport200
  • Category deleted (core)
  • Status changed from Closed to Assigned
  • Assignee changed from nobu (Nobuyoshi Nakada) to nagachika (Tomoyuki Chikanaga)
  • Target version deleted (2.1.0)
Actions #4

Updated by nagachika (Tomoyuki Chikanaga) about 11 years ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r39674.
Sho, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


merge revision(s) 39376: [Backport #7903]

* random.c (rb_random_ulong_limited): fix error message for negative
  value.  [ruby-dev:47061] [Bug #7903]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0