From eca7560cf19f77cded0cf1f434487e48008d2a65 Mon Sep 17 00:00:00 2001 From: Chris Roos Date: Mon, 24 Oct 2016 18:27:05 +0100 Subject: [PATCH] Reduce risk of hitting bug 12832 in Ruby v2.3 Fixes #277. This mirrors the change to `ClassMethod#hide_original_method` in 6f1c8b9b6aa4253c8a990befcb89648f09512242. This serves two purposes: 1. Make the implementation of `AnyInstanceMethod#hide_original_method` closer to `ClassMethod#hide_original_method`. 2. Avoid the possibility of hitting Ruby bug 12832[1]. While I'm fairly confident that calling `#instance_method` can trigger the bug in Ruby I'm not convinced that it can be triggered by the code in `AnyInstanceMethod#hide_original_method`. The bug appears to be triggered when calling class methods and I'm not aware of a way to use `any_instance_of` to stub class methods. [1]: https://bugs.ruby-lang.org/issues/12832 --- lib/mocha/any_instance_method.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mocha/any_instance_method.rb b/lib/mocha/any_instance_method.rb index 305ee2e14..8a3afb3c4 100644 --- a/lib/mocha/any_instance_method.rb +++ b/lib/mocha/any_instance_method.rb @@ -16,11 +16,11 @@ def reset_mocha def hide_original_method if @original_visibility = method_visibility(method) begin - @original_method = stubbee.instance_method(method) if RUBY_V2_PLUS @definition_target = PrependedModule.new stubbee.__send__ :prepend, @definition_target else + @original_method = stubbee.instance_method(method) if @original_method && @original_method.owner == stubbee stubbee.send(:remove_method, method) end