Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a check for 64 bit time_t overflow #2836

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions mrbgems/mruby-time/src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,22 @@ 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));
}


/* Allocates a mrb_time object and initializes it. */
static struct mrb_time*
time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
{
struct mrb_time *tm;

tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
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:
tm->sec = (time_t)sec;
if (
/* Check for overflow of 32 bit time_t */
(sizeof(time_t) == 4 && (sec > (double)INT32_MAX || (double)INT32_MIN > sec)) ||
/* Check for overflow of 64 bit time_t */
(sizeof(time_t) == 8 && (sec > (double)INT64_MAX || (double)INT64_MIN > sec)) ||
/* Check for wrapping of tm->sec */
((sec > 0 && tm->sec < 0) || (sec < 0 && (double)tm->sec > sec))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but this line might be a no-op, because signed overflow is undefined behavior.

) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec));
}
tm->usec = (time_t)llround((sec - tm->sec) * 1.0e6 + usec);
Expand Down