summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-numeric-ext/src/numeric_ext.c
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2018-09-19 20:53:32 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2018-11-19 11:28:51 +0900
commitafca99a40b8a3415b3a9a0e8fc41c93ddcbb11d8 (patch)
tree6ecbeb3c8a562ce64713ccd4d2d6b1d12e6b5fa2 /mrbgems/mruby-numeric-ext/src/numeric_ext.c
parent426c1f9e0b77a27d5384ccdee7f7a49eef0e2ed0 (diff)
downloadmruby-afca99a40b8a3415b3a9a0e8fc41c93ddcbb11d8.tar.gz
mruby-afca99a40b8a3415b3a9a0e8fc41c93ddcbb11d8.zip
Remove implicit conversion using `to_int` method.
The ISO standard does not include implicit type conversion using `to_int`. This implicit conversion often causes vulnerability. There will be no more attacks like #4120. In addition, we have added internal convenience method `__to_int` which does type check and conversion (from floats).
Diffstat (limited to 'mrbgems/mruby-numeric-ext/src/numeric_ext.c')
-rw-r--r--mrbgems/mruby-numeric-ext/src/numeric_ext.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/mrbgems/mruby-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c
index 1d6a07769..cd8bbf187 100644
--- a/mrbgems/mruby-numeric-ext/src/numeric_ext.c
+++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c
@@ -2,13 +2,10 @@
#include <mruby.h>
static inline mrb_int
-to_int(mrb_value x)
+to_int(mrb_state *mrb, mrb_value x)
{
- double f;
-
- if (mrb_fixnum_p(x)) return mrb_fixnum(x);
- f = mrb_float(x);
- return (mrb_int)f;
+ x = mrb_to_int(mrb, x);
+ return mrb_fixnum(x);
}
/*
@@ -28,7 +25,7 @@ mrb_int_chr(mrb_state *mrb, mrb_value x)
mrb_int chr;
char c;
- chr = to_int(x);
+ chr = to_int(mrb, x);
if (chr >= (1 << CHAR_BIT)) {
mrb_raisef(mrb, E_RANGE_ERROR, "%S out of char range", x);
}
@@ -48,8 +45,8 @@ mrb_int_allbits(mrb_state *mrb, mrb_value self)
{
mrb_int n, m;
- n = to_int(self);
mrb_get_args(mrb, "i", &m);
+ n = to_int(mrb, self);
return mrb_bool_value((n & m) == m);
}
@@ -64,8 +61,8 @@ mrb_int_anybits(mrb_state *mrb, mrb_value self)
{
mrb_int n, m;
- n = to_int(self);
mrb_get_args(mrb, "i", &m);
+ n = to_int(mrb, self);
return mrb_bool_value((n & m) != 0);
}
@@ -80,8 +77,8 @@ mrb_int_nobits(mrb_state *mrb, mrb_value self)
{
mrb_int n, m;
- n = to_int(self);
mrb_get_args(mrb, "i", &m);
+ n = to_int(mrb, self);
return mrb_bool_value((n & m) == 0);
}