Permalink
Browse files

Fix infinity loop

And some cases should raise FloatDomainError
  • Loading branch information...
ksss committed Mar 28, 2017
1 parent 6698049 commit 397f1fc94631072b76c750c1be3b809ad6dbc060
Showing with 31 additions and 3 deletions.
  1. +13 −0 mrbgems/mruby-time/src/time.c
  2. +18 −3 mrbgems/mruby-time/test/time.rb
@@ -211,6 +211,16 @@ mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm)
return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm));
}
static void
check_num_exact(mrb_state *mrb, double num)
{
if (isinf(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, num < 0 ? "-Infinity" : "Infinity");
}
if (isnan(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, "NaN");
}
}
/* Allocates a mrb_time object and initializes it. */
static struct mrb_time*
@@ -219,6 +229,9 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
struct mrb_time *tm;
time_t tsec = 0;
check_num_exact(mrb, sec);
check_num_exact(mrb, usec);
if (sizeof(time_t) == 4 && (sec > (double)INT32_MAX || (double)INT32_MIN > sec)) {
goto out_of_range;
}
@@ -10,7 +10,14 @@
end
assert('Time.at', '15.2.19.6.1') do
Time.at(1300000000.0)
assert_kind_of(Time, Time.at(1300000000.0))
assert_raise(FloatDomainError) { Time.at(Float::NAN) }
assert_raise(FloatDomainError) { Time.at(Float::INFINITY) }
assert_raise(FloatDomainError) { Time.at(-Float::INFINITY) }
assert_raise(FloatDomainError) { Time.at(0, Float::NAN) }
assert_raise(FloatDomainError) { Time.at(0, Float::INFINITY) }
assert_raise(FloatDomainError) { Time.at(0, -Float::INFINITY) }
end
assert('Time.gm', '15.2.19.6.2') do
@@ -37,14 +44,22 @@
t1 = Time.at(1300000000.0)
t2 = t1.+(60)
t2.utc.asctime == "Sun Mar 13 07:07:40 UTC 2011"
assert_equal(t2.utc.asctime, "Sun Mar 13 07:07:40 UTC 2011")
assert_raise(FloatDomainError) { Time.at(0) + Float::NAN }
assert_raise(FloatDomainError) { Time.at(0) + Float::INFINITY }
assert_raise(FloatDomainError) { Time.at(0) + -Float::INFINITY }
end
assert('Time#-', '15.2.19.7.2') do
t1 = Time.at(1300000000.0)
t2 = t1.-(60)
t2.utc.asctime == "Sun Mar 13 07:05:40 UTC 2011"
assert_equal(t2.utc.asctime, "Sun Mar 13 07:05:40 UTC 2011")
assert_raise(FloatDomainError) { Time.at(0) - Float::NAN }
assert_raise(FloatDomainError) { Time.at(0) - Float::INFINITY }
assert_raise(FloatDomainError) { Time.at(0) - -Float::INFINITY }
end
assert('Time#<=>', '15.2.19.7.3') do

0 comments on commit 397f1fc

Please sign in to comment.