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
cf8df563
Commit
cf8df563
authored
May 05, 2014
by
cremno
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add function for checked mrb_int subtraction
parent
0f393042
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
12 deletions
+14
-12
include/mruby/numeric.h
include/mruby/numeric.h
+10
-0
src/numeric.c
src/numeric.c
+1
-3
src/vm.c
src/vm.c
+3
-9
No files found.
include/mruby/numeric.h
View file @
cf8df563
...
@@ -45,6 +45,16 @@ mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
...
@@ -45,6 +45,16 @@ mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
return
!!
(((
x
^
z
)
&
(
y
^
z
))
&
MRB_INT_OVERFLOW_MASK
);
return
!!
(((
x
^
z
)
&
(
y
^
z
))
&
MRB_INT_OVERFLOW_MASK
);
}
}
static
inline
mrb_bool
mrb_int_sub_overflow
(
mrb_int
minuend
,
mrb_int
subtrahend
,
mrb_int
*
difference
)
{
mrb_uint
x
=
(
mrb_uint
)
minuend
;
mrb_uint
y
=
(
mrb_uint
)
subtrahend
;
mrb_uint
z
=
(
mrb_uint
)(
x
-
y
);
*
difference
=
(
mrb_int
)
z
;
return
!!
(((
x
^
z
)
&
(
~
y
^
z
))
&
MRB_INT_OVERFLOW_MASK
);
}
#undef MRB_INT_OVERFLOW_MASK
#undef MRB_INT_OVERFLOW_MASK
#undef mrb_uint
#undef mrb_uint
#undef MRB_UINT_MAKE
#undef MRB_UINT_MAKE
...
...
src/numeric.c
View file @
cf8df563
...
@@ -1145,9 +1145,7 @@ mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y)
...
@@ -1145,9 +1145,7 @@ mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y)
mrb_int
b
,
c
;
mrb_int
b
,
c
;
b
=
mrb_fixnum
(
y
);
b
=
mrb_fixnum
(
y
);
c
=
a
-
b
;
if
(
mrb_int_sub_overflow
(
a
,
b
,
&
c
))
{
if
(((
a
<
0
)
^
(
b
<
0
))
!=
0
&&
(
a
<
0
)
!=
(
c
<
0
))
{
/* integer overflow */
return
mrb_float_value
(
mrb
,
(
mrb_float
)
a
-
(
mrb_float
)
b
);
return
mrb_float_value
(
mrb
,
(
mrb_float
)
a
-
(
mrb_float
)
b
);
}
}
return
mrb_fixnum_value
(
c
);
return
mrb_fixnum_value
(
c
);
...
...
src/vm.c
View file @
cf8df563
...
@@ -1669,12 +1669,7 @@ RETRY_TRY_BLOCK:
...
@@ -1669,12 +1669,7 @@ RETRY_TRY_BLOCK:
x
=
mrb_fixnum
(
regs
[
a
]);
x
=
mrb_fixnum
(
regs
[
a
]);
y
=
mrb_fixnum
(
regs
[
a
+
1
]);
y
=
mrb_fixnum
(
regs
[
a
+
1
]);
z
=
x
-
y
;
if
(
mrb_int_sub_overflow
(
x
,
y
,
&
z
))
{
#ifdef MRB_WORD_BOXING
z
=
(
z
<<
MRB_FIXNUM_SHIFT
)
/
(
1
<<
MRB_FIXNUM_SHIFT
);
#endif
if
(((
x
<
0
)
^
(
y
<
0
))
!=
0
&&
(
x
<
0
)
!=
(
z
<
0
))
{
/* integer overflow */
SET_FLT_VALUE
(
mrb
,
regs
[
a
],
(
mrb_float
)
x
-
(
mrb_float
)
y
);
SET_FLT_VALUE
(
mrb
,
regs
[
a
],
(
mrb_float
)
x
-
(
mrb_float
)
y
);
break
;
break
;
}
}
...
@@ -1876,10 +1871,9 @@ RETRY_TRY_BLOCK:
...
@@ -1876,10 +1871,9 @@ RETRY_TRY_BLOCK:
{
{
mrb_int
x
=
regs_a
[
0
].
attr_i
;
mrb_int
x
=
regs_a
[
0
].
attr_i
;
mrb_int
y
=
GETARG_C
(
i
);
mrb_int
y
=
GETARG_C
(
i
);
mrb_int
z
=
x
-
y
;
mrb_int
z
;
if
((
x
<
0
)
!=
(
z
<
0
)
&&
((
x
<
0
)
^
(
y
<
0
))
!=
0
)
{
if
(
mrb_int_sub_overflow
(
x
,
y
,
&
z
))
{
/* integer overflow */
SET_FLT_VALUE
(
mrb
,
regs_a
[
0
],
(
mrb_float
)
x
-
(
mrb_float
)
y
);
SET_FLT_VALUE
(
mrb
,
regs_a
[
0
],
(
mrb_float
)
x
-
(
mrb_float
)
y
);
}
}
else
{
else
{
...
...
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