diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-07-19 09:49:41 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-07-19 09:49:41 +0900 |
| commit | aa3cb0466e39d8a0f548f569461efec1ca07bb6b (patch) | |
| tree | bdbb47b66c30c77a943cca9784aa1cec8760838f /src | |
| parent | ca2cfc6fe91c60c7e8014dec1f36dda719c67ba1 (diff) | |
| download | mruby-aa3cb0466e39d8a0f548f569461efec1ca07bb6b.tar.gz mruby-aa3cb0466e39d8a0f548f569461efec1ca07bb6b.zip | |
Avoid C undefined behavior of division by zero; close #3745
Diffstat (limited to 'src')
| -rw-r--r-- | src/vm.c | 18 |
1 files changed, 16 insertions, 2 deletions
@@ -2376,7 +2376,14 @@ RETRY_TRY_BLOCK: { mrb_int x = mrb_fixnum(regs[a]); mrb_int y = mrb_fixnum(regs[a+1]); - SET_FLOAT_VALUE(mrb, regs[a], (mrb_float)x / (mrb_float)y); + double f; + if (y == 0) { + f = INFINITY; + } + else { + f = (mrb_float)x / (mrb_float)y; + } + SET_FLOAT_VALUE(mrb, regs[a], f); } break; case TYPES2(MRB_TT_FIXNUM,MRB_TT_FLOAT): @@ -2391,7 +2398,14 @@ RETRY_TRY_BLOCK: { mrb_float x = mrb_float(regs[a]); mrb_int y = mrb_fixnum(regs[a+1]); - SET_FLOAT_VALUE(mrb, regs[a], x / y); + double f; + if (y == 0) { + f = INFINITY; + } + else { + f = x / y; + } + SET_FLOAT_VALUE(mrb, regs[a], f); } #else OP_MATH_BODY(/,mrb_float,mrb_fixnum); |
