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

ruby-changes:45912

From: nobu <ko1@a...>
Date: Thu, 16 Mar 2017 10:53:56 +0900 (JST)
Subject: [ruby-changes:45912] nobu:r57985 (trunk): rational.c: zero division

nobu	2017-03-16 10:53:52 +0900 (Thu, 16 Mar 2017)

  New Revision: 57985

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

  Log:
    rational.c: zero division
    
    * rational.c (read_rat_nos): denominator cannot be 0, raise zero
      division in that case.

  Modified files:
    trunk/rational.c
    trunk/test/ruby/test_rational.rb
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 57984)
+++ test/ruby/test_rational.rb	(revision 57985)
@@ -776,6 +776,11 @@ class Rational_Test < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L776
     ng[5, 3, '5/3x']
   end
 
+  def test_parse_zero_denominator
+    assert_raise(ZeroDivisionError) {"1/0".to_r}
+    assert_raise(ZeroDivisionError) {Rational("1/0")}
+  end
+
   def test_to_i
     assert_equal(1, Rational(3,2).to_i)
     assert_equal(1, Integer(Rational(3,2)))
Index: rational.c
===================================================================
--- rational.c	(revision 57984)
+++ rational.c	(revision 57985)
@@ -2396,6 +2396,7 @@ read_rat_nos(const char **s, int sign, i https://github.com/ruby/ruby/blob/trunk/rational.c#L2396
     if (**s == '/') {
 	(*s)++;
 	if (!read_digits(s, strict, &den, NULL)) goto failed;
+	if (den == ZERO) rb_num_zerodiv();
 	nurat_reduce(num, &den);
 	if (div != ONE && den != ONE)
 	    den = rb_int_mul(den, div);

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

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