Skip to content

Instantly share code, notes, and snippets.

@nobu
Created January 22, 2013 06:34
Show Gist options
  • Save nobu/4592599 to your computer and use it in GitHub Desktop.
Save nobu/4592599 to your computer and use it in GitHub Desktop.
wrong number argument in rubyspec/core/marshal/dump_spec.rb
From dca0ef157f59f199ded8ab836e7ea20247c1a2f5 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <[email protected]>
Date: Tue, 22 Jan 2013 15:25:02 +0900
Subject: [PATCH 1/2] vm_method.c: drop include_all flag
* vm_method.c (rb_obj_respond_to): drop optional include_all flag if
respond_to? method is defined in old style.
---
test/ruby/test_marshal.rb | 12 ++++++++++++
vm_method.c | 8 +++++++-
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb
index bc5ee62..ee8c5bd 100644
--- a/test/ruby/test_marshal.rb
+++ b/test/ruby/test_marshal.rb
@@ -517,4 +517,16 @@ class TestMarshal < Test::Unit::TestCase
assert(!c.untrusted?, bug7325)
end
end
+
+ class TestForRespondToFalse
+ def respond_to?(a)
+ false
+ end
+ end
+
+ def test_marshal_respond_to_arity
+ assert_nothing_raised(ArgumentError) do
+ Marshal.dump(TestForRespondToFalse.new)
+ end
+ end
end
diff --git a/vm_method.c b/vm_method.c
index 2adc91d..4ee194d 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -1521,7 +1521,13 @@ rb_obj_respond_to(VALUE obj, ID id, int priv)
return basic_obj_respond_to(obj, id, !RTEST(priv));
}
else {
- return RTEST(rb_funcall(obj, idRespond_to, priv ? 2 : 1, ID2SYM(id), Qtrue));
+ int argc = 1;
+ VALUE args[2];
+ args[0] = ID2SYM(id);
+ args[1] = Qtrue;
+ if (priv && rb_obj_method_arity(obj, idRespond_to) != 1)
+ argc = 2;
+ return RTEST(rb_funcall2(obj, idRespond_to, argc, args));
}
}
--
1.8.1.1
From 7f00c6a0cc7f3b8c91b594e53b6c4c7a0f7e1c91 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <[email protected]>
Date: Tue, 22 Jan 2013 15:30:12 +0900
Subject: [PATCH 2/2] proc.c: original arity
* proc.c (rb_mod_method_arity): return original arity of the method if
aliased because of visibility change, like as Method#arity.
---
proc.c | 16 +++++++++++++++-
test/ruby/test_marshal.rb | 7 +++++++
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/proc.c b/proc.c
index 07592c1..537d6b4 100644
--- a/proc.c
+++ b/proc.c
@@ -1743,10 +1743,24 @@ method_arity(VALUE method)
return rb_method_entry_arity(data->me);
}
+static rb_method_entry_t *
+original_method_entry(VALUE mod, ID id)
+{
+ VALUE rclass;
+ rb_method_entry_t *me;
+ rb_method_definition_t *def;
+ while ((def = (me = rb_method_entry(mod, id, &rclass))->def) != 0 &&
+ me->def->type == VM_METHOD_TYPE_ZSUPER) {
+ mod = RCLASS_SUPER(rclass);
+ id = me->def->original_id;
+ }
+ return me;
+}
+
int
rb_mod_method_arity(VALUE mod, ID id)
{
- rb_method_entry_t *me = rb_method_entry(mod, id, 0);
+ rb_method_entry_t *me = original_method_entry(mod, id);
return rb_method_entry_arity(me);
}
diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb
index ee8c5bd..2582920 100644
--- a/test/ruby/test_marshal.rb
+++ b/test/ruby/test_marshal.rb
@@ -524,9 +524,16 @@ class TestMarshal < Test::Unit::TestCase
end
end
+ class TestForRespondToZSuper < TestForRespondToFalse
+ private :respond_to?
+ end
+
def test_marshal_respond_to_arity
assert_nothing_raised(ArgumentError) do
Marshal.dump(TestForRespondToFalse.new)
end
+ assert_nothing_raised(ArgumentError) do
+ Marshal.dump(TestForRespondToZSuper.new)
+ end
end
end
--
1.8.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment