Project

General

Profile

Bug #10167

Prime#include?(mod) hangs up

Added by Kenichi Saita almost 2 years ago. Updated over 1 year ago.

Status:
Open
Priority:
Normal
Assignee:
ruby -v:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
[ruby-core:64546]

Description

We expect Prime class implements Module#include?(mod). But Prime#include?(mod) hangs up, because it is overwritten by Enumerable#include?(obj) and tries to search mod linearly from infinite sequence of prime numbers.

Reproducible script:

$ ruby -e 'require "prime"; puts Prime.include?(Enumerable)'
  • Expected: returns true
  • Actual: hangs up

I found this bug by the following code (hangs up if prime is required)

enumerables = []
ObjectSpace.each_object(Class){|klass|
  if klass.include?(Enumerable) then
    enumerables.push klass
  end
}

prime-include.diff View - fix Prime.include? and Prime.instance.include? (1.21 KB) Kenichi Saita, 08/29/2014 05:05 PM

History

#1 [ruby-core:64557] Updated by Kazuhiro NISHIYAMA almost 2 years ago

workaround:

enumerables = []
module_include_p = Module.method(:include?).unbind
ObjectSpace.each_object(Class) do |klass|
  if module_include_p.bind(klass).call(Enumerable)
    enumerables.push klass
  end
end

#2 [ruby-core:64558] Updated by Nobuyoshi Nakada almost 2 years ago

  • Description updated (diff)

Even Prime.include?(4) hangs up too.
This method seems useless.

#3 [ruby-core:64659] Updated by Kenichi Saita almost 2 years ago

It's difficult to delete Prime.inlude? method because this method is included by include Enumerable.

So, I overrided Prime.include? and Prime.instance.include?. Please review my patch.

#4 [ruby-core:65270] Updated by Kenichi Saita almost 2 years ago

Review my patch, please!

#5 [ruby-core:66583] Updated by Marc-Andre Lafortune over 1 year ago

  • Assignee set to Yuki Sonoda

Instead of aliasing include? to prime?, I'd recommend instead to restore it:

 Prime.define_singleton_method(:include?, Module.instance_method(:include?))
 Prime.include?(Enumerable) # => true

Probably won't be super useful, but still beats an infinite loop :-)

Assigning to yugui, although I have not seen any activity from her in over a year on the mailing list?

Also available in: Atom PDF