Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
65add8c6
Unverified
Commit
65add8c6
authored
Mar 01, 2021
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time.c: avoid using `mrb_int_{add,sub}_overflow()`.
The size and signedness of `mrb_int` and `time_t` may differ.
parent
eb68adaf
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
4 deletions
+36
-4
mrbgems/mruby-time/src/time.c
mrbgems/mruby-time/src/time.c
+36
-4
No files found.
mrbgems/mruby-time/src/time.c
View file @
65add8c6
...
...
@@ -556,6 +556,12 @@ mrb_time_cmp(mrb_state *mrb, mrb_value self)
return
mrb_fixnum_value
(
0
);
}
static
mrb_noreturn
void
int_overflow
(
mrb_state
*
mrb
,
const
char
*
reason
)
{
mrb_raisef
(
mrb
,
E_RANGE_ERROR
,
"time_t overflow in Time %s"
,
reason
);
}
static
mrb_value
mrb_time_plus
(
mrb_state
*
mrb
,
mrb_value
self
)
{
...
...
@@ -565,9 +571,22 @@ mrb_time_plus(mrb_state *mrb, mrb_value self)
tm
=
time_get_ptr
(
mrb
,
self
);
sec
=
mrb_to_time_t
(
mrb
,
o
,
&
usec
);
if
(
mrb_int_add_overflow
(
tm
->
sec
,
sec
,
&
sec
))
{
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
"time_t overflow in Time"
);
#ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
if
(
__builtin_add_overflow
(
tm
->
sec
,
sec
,
&
sec
))
{
int_overflow
(
mrb
,
"addition"
);
}
#else
if
(
sec
>=
0
)
{
if
(
tm
->
sec
>
MRB_TIME_MAX
-
sec
)
{
int_overflow
(
mrb
,
"addition"
);
}
}
else
{
if
(
tm
->
sec
<
MRB_TIME_MIN
-
sec
)
{
int_overflow
(
mrb
,
"addition"
);
}
}
#endif
return
mrb_time_make_time
(
mrb
,
mrb_obj_class
(
mrb
,
self
),
sec
,
tm
->
usec
+
usec
,
tm
->
timezone
);
}
...
...
@@ -595,9 +614,22 @@ mrb_time_minus(mrb_state *mrb, mrb_value self)
else
{
time_t
sec
,
usec
;
sec
=
mrb_to_time_t
(
mrb
,
other
,
&
usec
);
if
(
mrb_int_sub_overflow
(
tm
->
sec
,
sec
,
&
sec
))
{
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
"time_t overflow in Time"
);
#ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
if
(
__builtin_sub_overflow
(
tm
->
sec
,
sec
,
&
sec
))
{
int_overflow
(
mrb
,
"subtraction"
);
}
#else
if
(
sec
>=
0
)
{
if
(
tm
->
sec
<
MRB_TIME_MIN
+
sec
)
{
int_overflow
(
mrb
,
"subtraction"
);
}
}
else
{
if
(
tm
->
sec
>
MRB_TIME_MAX
+
sec
)
{
int_overflow
(
mrb
,
"subtraction"
);
}
}
#endif
return
mrb_time_make_time
(
mrb
,
mrb_obj_class
(
mrb
,
self
),
sec
,
tm
->
usec
-
usec
,
tm
->
timezone
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment