mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
fix overflow error like #2244 on MRB_WORD_BOXING
This commit is contained in:
+17
-4
@@ -27,8 +27,13 @@
|
||||
# else
|
||||
typedef int64_t mrb_int;
|
||||
# define MRB_INT_BIT 64
|
||||
# define MRB_INT_MIN INT64_MIN
|
||||
# define MRB_INT_MAX INT64_MAX
|
||||
# ifdef MRB_WORD_BOXING
|
||||
# define MRB_INT_MIN (INT64_MIN>>MRB_FIXNUM_SHIFT)
|
||||
# define MRB_INT_MAX (INT64_MAX>>MRB_FIXNUM_SHIFT)
|
||||
# else
|
||||
# define MRB_INT_MIN INT64_MIN
|
||||
# define MRB_INT_MAX INT64_MAX
|
||||
# endif
|
||||
# define PRIdMRB_INT PRId64
|
||||
# define PRIiMRB_INT PRIi64
|
||||
# define PRIoMRB_INT PRIo64
|
||||
@@ -36,6 +41,9 @@
|
||||
# define PRIXMRB_INT PRIX64
|
||||
# endif
|
||||
#elif defined(MRB_INT16)
|
||||
# ifdef MRB_WORD_BOXING
|
||||
# error "MRB_INT16 is too small for MRB_WORD_BOXING."
|
||||
# endif
|
||||
typedef int16_t mrb_int;
|
||||
# define MRB_INT_BIT 16
|
||||
# define MRB_INT_MIN INT16_MIN
|
||||
@@ -43,8 +51,13 @@
|
||||
#else
|
||||
typedef int32_t mrb_int;
|
||||
# define MRB_INT_BIT 32
|
||||
# define MRB_INT_MIN INT32_MIN
|
||||
# define MRB_INT_MAX INT32_MAX
|
||||
# ifdef MRB_WORD_BOXING
|
||||
# define MRB_INT_MIN (INT32_MIN>>MRB_FIXNUM_SHIFT)
|
||||
# define MRB_INT_MAX (INT32_MAX>>MRB_FIXNUM_SHIFT)
|
||||
# else
|
||||
# define MRB_INT_MIN INT32_MIN
|
||||
# define MRB_INT_MAX INT32_MAX
|
||||
# endif
|
||||
# define PRIdMRB_INT PRId32
|
||||
# define PRIiMRB_INT PRIi32
|
||||
# define PRIoMRB_INT PRIo32
|
||||
|
||||
+5
-1
@@ -687,7 +687,11 @@ int_to_i(mrb_state *mrb, mrb_value num)
|
||||
return num;
|
||||
}
|
||||
|
||||
#ifdef MRB_FIXNUM_SHIFT
|
||||
#define SQRT_INT_MAX ((mrb_int)1<<((MRB_INT_BIT-1-MRB_FIXNUM_SHIFT)/2))
|
||||
#else
|
||||
#define SQRT_INT_MAX ((mrb_int)1<<((MRB_INT_BIT-1)/2))
|
||||
#endif
|
||||
/*tests if N*N would overflow*/
|
||||
#define FIT_SQRT_INT(n) (((n)<SQRT_INT_MAX)&&((n)>=-SQRT_INT_MAX))
|
||||
|
||||
@@ -705,7 +709,7 @@ mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y)
|
||||
if (FIT_SQRT_INT(a) && FIT_SQRT_INT(b))
|
||||
return mrb_fixnum_value(a*b);
|
||||
c = a * b;
|
||||
if (a != 0 && c/a != b) {
|
||||
if ((a != 0 && c/a != b) || !FIXABLE(c)) {
|
||||
return mrb_float_value(mrb, (mrb_float)a*(mrb_float)b);
|
||||
}
|
||||
return mrb_fixnum_value(c);
|
||||
|
||||
@@ -109,15 +109,9 @@ main(int argc, char **argv)
|
||||
|
||||
mrbtest = mrb_define_module(mrb, "Mrbtest");
|
||||
|
||||
#ifdef MRB_WORD_BOXING
|
||||
mrb_define_const(mrb, mrbtest, "FIXNUM_MAX", mrb_fixnum_value(MRB_INT_MAX >> MRB_FIXNUM_SHIFT));
|
||||
mrb_define_const(mrb, mrbtest, "FIXNUM_MIN", mrb_fixnum_value(MRB_INT_MIN >> MRB_FIXNUM_SHIFT));
|
||||
mrb_define_const(mrb, mrbtest, "FIXNUM_BIT", mrb_fixnum_value(MRB_INT_BIT >> MRB_FIXNUM_SHIFT));
|
||||
#else
|
||||
mrb_define_const(mrb, mrbtest, "FIXNUM_MAX", mrb_fixnum_value(MRB_INT_MAX));
|
||||
mrb_define_const(mrb, mrbtest, "FIXNUM_MIN", mrb_fixnum_value(MRB_INT_MIN));
|
||||
mrb_define_const(mrb, mrbtest, "FIXNUM_BIT", mrb_fixnum_value(MRB_INT_BIT));
|
||||
#endif
|
||||
|
||||
mrb_init_mrbtest(mrb);
|
||||
ret = eval_test(mrb);
|
||||
|
||||
Reference in New Issue
Block a user