Merge pull request #3552 from ksss/time

Fix infinity loop ref #3546
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-03-28 14:47:23 +09:00
committed by GitHub
2 changed files with 31 additions and 3 deletions
+13
View File
@@ -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;
}
+18 -3
View File
@@ -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