# [Bug #3136] reuse of singleton method definition causes SEGV

e\$B%A%1%C%He(B #3136 e\$B\$,99?7\$5\$l\$^\$7\$?!#e(B (by _ wanabe)

e\$B%o%J%Y\$H?=\$7\$^\$9!#e(B
e\$B2r7hJ}K!e(B 1. e\$B\$O\$-\$D\$=\$&\$@\$C\$?\$N\$G!"e(B2.
e\$B\$NJ}8~\$G;n\$7\$F\$\$^\$7\$?!#e(B
e\$B\$3\$l\$GK%A%1%C%H\$*\$h\$Se(B 2502 e\$B\$OH/>I\$7\$J\$/\$J\$j\$^\$7\$?!#e(B
e\$BJ#@=\$9\$k\$3\$H\$K\$h\$kI{:nMQ\$K;W\$\$Ev\$?\$i\$J\$\$\$N\$G!"e(B
e\$BH?BP\$,\$J\$1\$l\$P%3%
%C%H\$7\$F\$7\$^\$*\$&\$H;W\$C\$F\$\$\$^\$9!#e(B

diff --git a/vm.c b/vm.c
index e62c9a4…a381d3c 100644
— a/vm.c
+++ b/vm.c
@@ -1844,6 +1844,11 @@ vm_define_method(rb_thread_t *th, VALUE obj, ID
id, VALUE iseqval,
rb_iseq_t *miseq;
GetISeqPtr(iseqval, miseq);

• if (miseq->klass) {
• ``````  iseqval = rb_iseq_clone(iseqval, 0);
``````
• ``````  GetISeqPtr(iseqval, miseq);
``````
• }
• if (NIL_P(klass)) {
rb_raise(rb_eTypeError, “no class/module to add method”);
}

http://redmine.ruby-lang.org/issues/show/3136

e\$B!!\$5\$5\$@\$G\$9!%e(B

(2010/08/12 18:31), _ wanabe wrote:

e\$B2r7hJ}K!e(B 1. e\$B\$O\$-\$D\$=\$&\$@\$C\$?\$N\$G!"e(B2. e\$B\$NJ}8~\$G;n\$7\$F\$\$^\$7\$?!#e(B
e\$B\$3\$l\$GK%A%1%C%H\$*\$h\$Se(B 2502 e\$B\$OH/>I\$7\$J\$/\$J\$j\$^\$7\$?!#e(B
e\$BJ#@=\$9\$k\$3\$H\$K\$h\$kI{:nMQ\$K;W\$\$Ev\$?\$i\$J\$\$\$N\$G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
Yusuke, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

http://redmine.ruby-lang.org/issues/show/3136