Forum: Ruby-dev [ruby-trunk - Bug #6460][Assigned] `unexpected return' occurs when a proc is called in ensure

303dd57f37d64288bb4f0336332a8882?d=identicon&s=25 ktsj (Kazuki Tsujimoto) (Guest)
on 2012-05-19 10:10
(Received via mailing list)
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
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 ko1 (Koichi Sasada) (Guest)
on 2012-06-25 22:50
(Received via mailing list)
Issue #6460 has been updated by ko1 (Koichi Sasada).


こちらも遅くなって済みません.

テストを trunk に,
パッチを 1.9.3 にバックポート,

が良いのではないかと思います.

テストは btest の KNOWNBUGS.rb かなぁ.
----------------------------------------
Bug #6460: `unexpected return' occurs when a proc is called in ensure
https://bugs.ruby-lang.org/issues/6460#change-27447

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
303dd57f37d64288bb4f0336332a8882?d=identicon&s=25 ktsj (Kazuki Tsujimoto) (Guest)
on 2012-07-01 12:14
(Received via mailing list)
Issue #6460 has been updated by ktsj (Kazuki Tsujimoto).

File 6460-1_9_3.patch added

パッチを添付します。
----------------------------------------
Backport #6460: `unexpected return' occurs when a proc is called in
ensure
https://bugs.ruby-lang.org/issues/6460#change-27658

Author: ktsj (Kazuki Tsujimoto)
Status: Assigned
Priority: Normal
Assignee: naruse (Yui NARUSE)
Category:
Target version:


=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
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2012-11-02 02:41
(Received via mailing list)
Issue #6460 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Closed to Open

=begin
これどうしましょうか。
最低限のパッチはこんな感じみたいです。

 index d4d4ebb..3f31558 100644
 --- i/vm.c
 +++ w/vm.c
 @@ -493,9 +494,20 @@ rb_vm_make_env_object(rb_thread_t * th,
rb_control_frame_t *cfp)
      return envval;
  }

 +static int vm_rewrite_ep_in_errinfo(rb_thread_t *th,
rb_control_frame_t *cfp);
 +
  void
  rb_vm_rewrite_ep_in_errinfo(rb_thread_t *th, rb_control_frame_t *cfp)
  {
 +    while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)) {
 +  if (vm_rewrite_ep_in_errinfo(th, cfp)) break;
 +  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
 +    }
 +}
 +
 +static int
 +vm_rewrite_ep_in_errinfo(rb_thread_t *th, rb_control_frame_t *cfp)
 +{
      /* rewrite ep in errinfo to point to heap */
      if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) &&
    (cfp->iseq->type == ISEQ_TYPE_RESCUE ||
 @@ -509,10 +521,12 @@ rb_vm_rewrite_ep_in_errinfo(rb_thread_t *th,
rb_control_frame_t *cfp)
          rb_env_t *epenv;
          GetEnvPtr(epval, epenv);
          SET_THROWOBJ_CATCH_POINT(errinfo, (VALUE)(epenv->env +
epenv->local_size));
 +        return 1;
      }
        }
    }
      }
 +    return 0;
  }

  void
=end

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

Author: ktsj (Kazuki Tsujimoto)
Status: Open
Priority: Normal
Assignee: naruse (Yui NARUSE)
Category:
Target version:


=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
303dd57f37d64288bb4f0336332a8882?d=identicon&s=25 ktsj (Kazuki Tsujimoto) (Guest)
on 2012-11-02 17:14
(Received via mailing list)
Issue #6460 has been updated by ktsj (Kazuki Tsujimoto).


=begin
辻本です。

なかださんのパッチだと、例えば以下のようなコードでunexpected returnとなってしまいます。

 class C
   def m1
     m2{|e|
       return
     }
   end

   def m2
     begin
       yield :foo
     ensure
       begin
         begin
           yield :foo
         ensure
           Proc.new
           raise ''
         end
       rescue
       end
     end
   end
 end

 C.new.m1
=end

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

Author: ktsj (Kazuki Tsujimoto)
Status: Open
Priority: Normal
Assignee: naruse (Yui NARUSE)
Category:
Target version:


=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
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2012-11-02 20:24
(Received via mailing list)
Issue #6460 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Open to Closed
ruby -v set to trunk

やっぱり全部辿らないとまずいですね。
r37430でコミットしました。
----------------------------------------
Bug #6460: `unexpected return' occurs when a proc is called in ensure
https://bugs.ruby-lang.org/issues/6460#change-32251

Author: ktsj (Kazuki Tsujimoto)
Status: Closed
Priority: Normal
Assignee: naruse (Yui NARUSE)
Category:
Target version:
ruby -v: trunk


=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
This topic is locked and can not be replied to.