Issue #6460 has been reported by ktsj (Kazuki Tsujimoto).

----------------------------------------
Bug #6460: `unexpected return' occurs when a proc is called in ensure
https://bugs.ruby-lang.org/issues/6460

Author: ktsj (Kazuki Tsujimoto)
Status: Assigned
Priority: Normal
Assignee: ko1 (Koichi Sasada)
Category: YARV
Target version: 
ruby -v: ruby 2.0.0dev (2012-05-19 trunk 35711) [x86_64-linux]


=begin
辻本です。

Bug #2729, #5234 の続きのような話になりますが
以下のコードでunexpected returnとなります。

 class C
   def each
     begin
       yield :foo
     ensure
       1.times { Proc.new }
     end
   end
 
   def detect
     each{|e|
       r = yield(e)
       return true if r
     }
     false
   end
 end
 
 p C.new.detect{|e|
   true
 }

結局のところmake_envの際にはすべてのcontrol frameの
dfpの値をチェックしないといけないようです。

 diff --git a/proc.c b/proc.c
 index d44e8d8..07a904b 100644
 --- a/proc.c
 +++ b/proc.c
 @@ -418,7 +418,6 @@ proc_new(VALUE klass, int is_lambda)
      }
  
      procval = rb_vm_make_proc(th, block, klass);
 -    rb_vm_rewrite_dfp_in_errinfo(th, cfp);
  
      if (is_lambda) {
  	rb_proc_t *proc;
 diff --git a/vm.c b/vm.c
 index 6fbd3ad..abfe993 100644
 --- a/vm.c
 +++ b/vm.c
 @@ -406,8 +406,6 @@ vm_make_env_each(rb_thread_t * const th, rb_control_frame_t * const cfp,
      if (!RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
  	/* TODO */
  	env->block.iseq = 0;
 -    } else {
 -	rb_vm_rewrite_dfp_in_errinfo(th, cfp);
      }
      return envval;
  }
 @@ -462,6 +460,7 @@ rb_vm_make_env_object(rb_thread_t * th, rb_control_frame_t *cfp)
      }
  
      envval = vm_make_env_each(th, cfp, cfp->dfp, cfp->lfp);
 +    rb_vm_rewrite_dfp_in_errinfo(th, th->cfp);
  
      if (PROCDEBUG) {
  	check_env_value(envval);
 @@ -473,23 +472,26 @@ rb_vm_make_env_object(rb_thread_t * th, rb_control_frame_t *cfp)
  void
  rb_vm_rewrite_dfp_in_errinfo(rb_thread_t *th, rb_control_frame_t *cfp)
  {
 -    /* rewrite dfp in errinfo to point to heap */
 -    if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) &&
 -	(cfp->iseq->type == ISEQ_TYPE_RESCUE ||
 -	 cfp->iseq->type == ISEQ_TYPE_ENSURE)) {
 -	VALUE errinfo = cfp->dfp[-2]; /* #$! */
 -	if (RB_TYPE_P(errinfo, T_NODE)) {
 -	    VALUE *escape_dfp = GET_THROWOBJ_CATCH_POINT(errinfo);
 -	    if (! ENV_IN_HEAP_P(th, escape_dfp)) {
 -		VALUE dfpval = *escape_dfp;
 -		if (CLASS_OF(dfpval) == rb_cEnv) {
 -		    rb_env_t *dfpenv;
 -		    GetEnvPtr(dfpval, dfpenv);
 -		    SET_THROWOBJ_CATCH_POINT(errinfo, (VALUE)(dfpenv->env + dfpenv->local_size));
 +    while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)) {
 +	/* rewrite dfp in errinfo to point to heap */
 +	if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) &&
 +	    (cfp->iseq->type == ISEQ_TYPE_RESCUE ||
 +	     cfp->iseq->type == ISEQ_TYPE_ENSURE)) {
 +	    VALUE errinfo = cfp->dfp[-2]; /* #$! */
 +	    if (RB_TYPE_P(errinfo, T_NODE)) {
 +		VALUE *escape_dfp = GET_THROWOBJ_CATCH_POINT(errinfo);
 +		if (! ENV_IN_HEAP_P(th, escape_dfp)) {
 +		    VALUE dfpval = *escape_dfp;
 +		    if (CLASS_OF(dfpval) == rb_cEnv) {
 +			rb_env_t *dfpenv;
 +			GetEnvPtr(dfpval, dfpenv);
 +			SET_THROWOBJ_CATCH_POINT(errinfo, (VALUE)(dfpenv->env + dfpenv->local_size));
 +		    }
  		}
  	    }
  	}
 -    }
 +	cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
 +    };
  }
  
  void

近々この辺の構造が大きく変わるようなので
trunkにこのパッチを取り込む意義はあまり感じませんが、
1.9.3へのバックポートはしておいてもいいのではと思います。
どうでしょうか。
=end



-- 
http://bugs.ruby-lang.org/