Avoid infinite loop on negative exponent; fix #3677

parent acd3ac62
...@@ -60,26 +60,23 @@ num_pow(mrb_state *mrb, mrb_value x) ...@@ -60,26 +60,23 @@ num_pow(mrb_state *mrb, mrb_value x)
mrb_int base = mrb_fixnum(x); mrb_int base = mrb_fixnum(x);
mrb_int exp = mrb_fixnum(y); mrb_int exp = mrb_fixnum(y);
mrb_int result = 1; mrb_int result = 1;
mrb_bool ok = TRUE;
if (exp < 0) goto float_pow;
for (;;) { for (;;) {
if (exp & 1) { if (exp & 1) {
if (mrb_int_mul_overflow(result, base, &result)) { if (mrb_int_mul_overflow(result, base, &result)) {
ok = FALSE; goto float_pow;
break;
} }
} }
exp >>= 1; exp >>= 1;
if (exp == 0) break; if (exp == 0) break;
if (mrb_int_mul_overflow(base, base, &base)) { if (mrb_int_mul_overflow(base, base, &base)) {
ok = FALSE; goto float_pow;
break;
} }
} }
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)); d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y));
return mrb_float_value(mrb, d); return mrb_float_value(mrb, d);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment