mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
compar.c: inversed comarison without infinite recursion
* compar.c (rb_invcmp): compare by inversed comarison, with preventing from infinite recursion. [ruby-core:52305] [Bug #7870] * string.c (rb_str_cmp_m), time.c (time_cmp): get rid of infinite recursion. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39292 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c9283b5c5d
commit
44c5c2a312
6 changed files with 40 additions and 12 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Sun Feb 17 20:55:44 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* compar.c (rb_invcmp): compare by inversed comarison, with preventing
|
||||||
|
from infinite recursion. [ruby-core:52305] [Bug #7870]
|
||||||
|
|
||||||
|
* string.c (rb_str_cmp_m), time.c (time_cmp): get rid of infinite
|
||||||
|
recursion.
|
||||||
|
|
||||||
Sun Feb 17 17:23:22 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sun Feb 17 17:23:22 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* lib/mkmf.rb: remove extra topdir in VPATH, which was in
|
* lib/mkmf.rb: remove extra topdir in VPATH, which was in
|
||||||
|
|
20
compar.c
20
compar.c
|
@ -31,6 +31,26 @@ rb_cmperr(VALUE x, VALUE y)
|
||||||
rb_obj_classname(x), classname);
|
rb_obj_classname(x), classname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
invcmp_recursive(VALUE x, VALUE y, int recursive)
|
||||||
|
{
|
||||||
|
if (recursive) return Qnil;
|
||||||
|
return rb_check_funcall(y, cmp, 1, &x);
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_invcmp(VALUE x, VALUE y)
|
||||||
|
{
|
||||||
|
VALUE invcmp = rb_exec_recursive(invcmp_recursive, x, y);
|
||||||
|
if (invcmp == Qundef || NIL_P(invcmp)) {
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int result = -rb_cmpint(invcmp, x, y);
|
||||||
|
return INT2FIX(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
cmp_eq(VALUE *a)
|
cmp_eq(VALUE *a)
|
||||||
{
|
{
|
||||||
|
|
|
@ -66,6 +66,9 @@ VALUE rb_special_singleton_class(VALUE);
|
||||||
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
|
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
|
||||||
void Init_class_hierarchy(void);
|
void Init_class_hierarchy(void);
|
||||||
|
|
||||||
|
/* compar.c */
|
||||||
|
VALUE rb_invcmp(VALUE, VALUE);
|
||||||
|
|
||||||
/* compile.c */
|
/* compile.c */
|
||||||
int rb_dvar_defined(ID);
|
int rb_dvar_defined(ID);
|
||||||
int rb_local_defined(ID);
|
int rb_local_defined(ID);
|
||||||
|
|
7
string.c
7
string.c
|
@ -2389,13 +2389,8 @@ rb_str_cmp_m(VALUE str1, VALUE str2)
|
||||||
if (RB_TYPE_P(tmp, T_STRING)) {
|
if (RB_TYPE_P(tmp, T_STRING)) {
|
||||||
result = rb_str_cmp(str1, tmp);
|
result = rb_str_cmp(str1, tmp);
|
||||||
}
|
}
|
||||||
else if ((tmp = rb_check_funcall(str2, rb_intern("<=>"), 1, &str1)) ==
|
|
||||||
Qundef) {
|
|
||||||
return Qnil;
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
if (NIL_P(tmp)) return Qnil;
|
return rb_invcmp(str1, str2);
|
||||||
result = -rb_cmpint(tmp, str1, str2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -69,4 +69,11 @@ class TestComparable < Test::Unit::TestCase
|
||||||
assert_raise(ArgumentError) { 1.0 < nil }
|
assert_raise(ArgumentError) { 1.0 < nil }
|
||||||
assert_raise(ArgumentError) { 1.0 < Object.new }
|
assert_raise(ArgumentError) { 1.0 < Object.new }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_inversed_compare
|
||||||
|
bug7870 = '[ruby-core:52305] [Bug #7870]'
|
||||||
|
assert_nothing_raised(SystemStackError, bug7870) {
|
||||||
|
assert_nil(Time.new <=> "")
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
7
time.c
7
time.c
|
@ -3365,12 +3365,7 @@ time_cmp(VALUE time1, VALUE time2)
|
||||||
n = wcmp(tobj1->timew, tobj2->timew);
|
n = wcmp(tobj1->timew, tobj2->timew);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
VALUE tmp;
|
return rb_invcmp(time1, time2);
|
||||||
|
|
||||||
tmp = rb_funcall(time2, rb_intern("<=>"), 1, time1);
|
|
||||||
if (NIL_P(tmp)) return Qnil;
|
|
||||||
|
|
||||||
n = -rb_cmpint(tmp, time1, time2);
|
|
||||||
}
|
}
|
||||||
if (n == 0) return INT2FIX(0);
|
if (n == 0) return INT2FIX(0);
|
||||||
if (n > 0) return INT2FIX(1);
|
if (n > 0) return INT2FIX(1);
|
||||||
|
|
Loading…
Add table
Reference in a new issue