Skip to content

Commit

Permalink
Fix infinity loop
Browse files Browse the repository at this point in the history
And some cases should raise FloatDomainError
  • Loading branch information
ksss committed Mar 28, 2017
1 parent 6698049 commit 397f1fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 13 additions & 0 deletions mrbgems/mruby-time/src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand All @@ -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;
}
Expand Down
21 changes: 18 additions & 3 deletions mrbgems/mruby-time/test/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 397f1fc

Please sign in to comment.