Skip to content

Commit

Permalink
Raise an exception in time_update_datetime().
Browse files Browse the repository at this point in the history
The function used to return NULL on error, but not checked in the
caller site.
  • Loading branch information
matz committed Dec 6, 2016
1 parent 6ec14a2 commit 9a962ed
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions mrbgems/mruby-time/src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static const struct mrb_data_type mrb_time_type = { "Time", mrb_free };
/** Updates the datetime of a mrb_time based on it's timezone and
seconds setting. Returns self on success, NULL of failure. */
static struct mrb_time*
mrb_time_update_datetime(struct mrb_time *self)
time_update_datetime(mrb_state *mrb, struct mrb_time *self)
{
struct tm *aid;

Expand All @@ -193,7 +193,11 @@ mrb_time_update_datetime(struct mrb_time *self)
else {
aid = localtime_r(&self->sec, &self->datetime);
}
if (!aid) return NULL;
if (!aid) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, self->sec));
/* not reached */
return NULL;
}
#ifdef NO_GMTIME_R
self->datetime = *aid; /* copy data */
#endif
Expand Down Expand Up @@ -238,7 +242,7 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
tm->usec -= 1000000;
}
tm->timezone = timezone;
mrb_time_update_datetime(tm);
time_update_datetime(mrb, tm);

return tm;
}
Expand Down Expand Up @@ -290,7 +294,7 @@ current_mrb_time(mrb_state *mrb)
}
#endif
tm->timezone = MRB_TIMEZONE_LOCAL;
mrb_time_update_datetime(tm);
time_update_datetime(mrb, tm);

return tm;
}
Expand Down Expand Up @@ -556,7 +560,7 @@ mrb_time_getutc(mrb_state *mrb, mrb_value self)
tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
*tm2 = *tm;
tm2->timezone = MRB_TIMEZONE_UTC;
mrb_time_update_datetime(tm2);
time_update_datetime(mrb, tm2);
return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}

Expand All @@ -571,7 +575,7 @@ mrb_time_getlocal(mrb_state *mrb, mrb_value self)
tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
*tm2 = *tm;
tm2->timezone = MRB_TIMEZONE_LOCAL;
mrb_time_update_datetime(tm2);
time_update_datetime(mrb, tm2);
return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}

Expand Down Expand Up @@ -649,7 +653,7 @@ mrb_time_localtime(mrb_state *mrb, mrb_value self)

tm = time_get_ptr(mrb, self);
tm->timezone = MRB_TIMEZONE_LOCAL;
mrb_time_update_datetime(tm);
time_update_datetime(mrb, tm);
return self;
}

Expand Down Expand Up @@ -746,7 +750,7 @@ mrb_time_utc(mrb_state *mrb, mrb_value self)

tm = time_get_ptr(mrb, self);
tm->timezone = MRB_TIMEZONE_UTC;
mrb_time_update_datetime(tm);
time_update_datetime(mrb, tm);
return self;
}

Expand Down

0 comments on commit 9a962ed

Please sign in to comment.