Commit 0f528932 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #3552 from ksss/time

Fix infinity loop ref #3546
parents 66980493 397f1fc9
......@@ -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 @@ assert('Time', '15.2.19') do
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 @@ assert('Time#+', '15.2.19.7.1') do
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment