[前][次][番号順一覧][スレッド一覧]

ruby-changes:45633

From: nobu <ko1@a...>
Date: Fri, 24 Feb 2017 18:39:21 +0900 (JST)
Subject: [ruby-changes:45633] nobu:r57706 (trunk): Integer.sqrt argument check

nobu	2017-02-24 18:39:17 +0900 (Fri, 24 Feb 2017)

  New Revision: 57706

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57706

  Log:
    Integer.sqrt argument check
    
    * numeric.c (rb_int_s_isqrt): check if the argument is an integer.
      [Feature #13219]

  Modified files:
    trunk/numeric.c
    trunk/test/ruby/test_integer.rb
Index: numeric.c
===================================================================
--- numeric.c	(revision 57705)
+++ numeric.c	(revision 57706)
@@ -5158,6 +5158,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/numeric.c#L5158
 rb_int_s_isqrt(VALUE self, VALUE num)
 {
     unsigned long n, sq;
+    num = rb_to_int(num);
     if (FIXNUM_P(num)) {
 	if (FIXNUM_NEGATIVE_P(num)) {
 	    domain_error("isqrt");
@@ -5166,7 +5167,7 @@ rb_int_s_isqrt(VALUE self, VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5167
 	sq = rb_ulong_isqrt(n);
 	return LONG2FIX(sq);
     }
-    if (RB_TYPE_P(num, T_BIGNUM)) {
+    else {
 	size_t biglen;
 	if (RBIGNUM_NEGATIVE_P(num)) {
 	    domain_error("isqrt");
@@ -5183,7 +5184,6 @@ rb_int_s_isqrt(VALUE self, VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5184
 #endif
 	return rb_big_isqrt(num);
     }
-    return Qnil;
 }
 
 /*
Index: test/ruby/test_integer.rb
===================================================================
--- test/ruby/test_integer.rb	(revision 57705)
+++ test/ruby/test_integer.rb	(revision 57706)
@@ -466,6 +466,7 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L466
   end
 
   def test_square_root
+    assert_raise(TypeError) {Integer.sqrt("x")}
     assert_raise(Math::DomainError) {Integer.sqrt(-1)}
     assert_equal(0, Integer.sqrt(0))
     (1...4).each {|i| assert_equal(1, Integer.sqrt(i))}

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]