diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-05-31 09:53:19 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-05-31 09:53:19 +0900 |
| commit | 877f9d02e6df8d21de7a53586155f757caab58d0 (patch) | |
| tree | 75122f08714ff085949d960921ff8bf2225d03b6 | |
| parent | acd3ac62a832e15c8681fa83f94f108757b51bda (diff) | |
| download | mruby-877f9d02e6df8d21de7a53586155f757caab58d0.tar.gz mruby-877f9d02e6df8d21de7a53586155f757caab58d0.zip | |
Avoid infinite loop on negative exponent; fix #3677
| -rw-r--r-- | src/numeric.c | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/src/numeric.c b/src/numeric.c index 11995cac1..7d7814e43 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -60,26 +60,23 @@ num_pow(mrb_state *mrb, mrb_value x) mrb_int base = mrb_fixnum(x); mrb_int exp = mrb_fixnum(y); mrb_int result = 1; - mrb_bool ok = TRUE; + if (exp < 0) goto float_pow; for (;;) { if (exp & 1) { if (mrb_int_mul_overflow(result, base, &result)) { - ok = FALSE; - break; + goto float_pow; } } exp >>= 1; if (exp == 0) break; if (mrb_int_mul_overflow(base, base, &base)) { - ok = FALSE; - break; + goto float_pow; } } - if (ok) { - return mrb_fixnum_value(result); - } + return mrb_fixnum_value(result); } + float_pow: d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y)); return mrb_float_value(mrb, d); } |
