time overflow check; ref #2337

parent ad502775
...@@ -201,6 +201,13 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) ...@@ -201,6 +201,13 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time)); tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
tm->sec = (time_t)sec; tm->sec = (time_t)sec;
if (sizeof(time_t) == 4 && (sec > (double)INT32_MAX || (double)INT32_MIN > sec)) {
goto out_of_range;
}
else if ((sec > 0 && tm->sec < 0) || (sec < 0 && (double)tm->sec > sec)) {
out_of_range:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec));
}
tm->usec = (time_t)((sec - tm->sec) * 1.0e6 + usec); tm->usec = (time_t)((sec - tm->sec) * 1.0e6 + usec);
while (tm->usec < 0) { while (tm->usec < 0) {
tm->sec--; tm->sec--;
......
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