From 420a5c994c747806b45b337f9d1cd13bf1c83ddb Mon Sep 17 00:00:00 2001 From: tadf Date: Mon, 28 Apr 2008 11:28:55 +0000 Subject: [PATCH] * rational (nurat_marshal_load): checks the given denominator. [ruby-dev:34536] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ rational.c | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index c388b28e9e..e4c5510bde 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Apr 28 20:24:27 2008 Tadayoshi Funaba + + * rational (nurat_marshal_load): checks the given + denominator. [ruby-dev:34536] + Mon Apr 28 14:21:18 2008 Tanaka Akira * include/ruby/ruby.h (POSFIXABLE): use FIXNUM_MAX+1 instead of diff --git a/rational.c b/rational.c index 05541a81ff..82cb0298a5 100644 --- a/rational.c +++ b/rational.c @@ -407,9 +407,14 @@ nurat_s_new_bang(int argc, VALUE *argv, VALUE klass) if (!k_integer_p(den)) den = f_to_i(den); - if (f_negative_p(den)) { + switch (FIX2INT(f_cmp(den, ZERO))) { + case -1: num = f_negate(num); den = f_negate(den); + break; + case 0: + rb_raise(rb_eZeroDivError, "devided by zero"); + break; } break; } @@ -452,10 +457,8 @@ nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den) switch (FIX2INT(f_cmp(den, ZERO))) { case -1: - if (f_negative_p(den)) { - num = f_negate(num); - den = f_negate(den); - } + num = f_negate(num); + den = f_negate(den); break; case 0: rb_raise(rb_eZeroDivError, "devided by zero"); @@ -477,10 +480,8 @@ nurat_s_canonicalize_internal_no_reduce(VALUE klass, VALUE num, VALUE den) { switch (FIX2INT(f_cmp(den, ZERO))) { case -1: - if (f_negative_p(den)) { - num = f_negate(num); - den = f_negate(den); - } + num = f_negate(num); + den = f_negate(den); break; case 0: rb_raise(rb_eZeroDivError, "devided by zero"); @@ -1190,6 +1191,10 @@ nurat_marshal_load(VALUE self, VALUE a) get_dat1(self); dat->num = RARRAY_PTR(a)[0]; dat->den = RARRAY_PTR(a)[1]; + + if (f_zero_p(dat->den)) + rb_raise(rb_eZeroDivError, "devided by zero"); + return self; }