Skip to content

Commit

Permalink
Avoid infinite loop on negative exponent; fix #3677
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed May 31, 2017
1 parent acd3ac6 commit 877f9d0
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 877f9d0

Please sign in to comment.