Skip to content

Commit

Permalink
Fix an underlying bug. flodivmod() will be crashed in case ((y == 0) …
Browse files Browse the repository at this point in the history
…&& ((divp == NULL) || (modp == NULL))).
  • Loading branch information
monaka committed Feb 23, 2013
1 parent 82313b6 commit 9ea0b4b
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/numeric.c
Expand Up @@ -244,22 +244,25 @@ flo_mul(mrb_state *mrb, mrb_value x)
static void
flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float *modp)
{
mrb_float div, mod;
mrb_float div;
mrb_float mod;

if (y == 0.0) {
*divp = str_to_mrb_float("inf");
*modp = str_to_mrb_float("nan");
return;
div = str_to_mrb_float("inf");
mod = str_to_mrb_float("nan");
}
mod = fmod(x, y);
if (isinf(x) && !isinf(y) && !isnan(y))
div = x;
else
div = (x - mod) / y;
if (y*mod < 0) {
mod += y;
div -= 1.0;
else {
mod = fmod(x, y);
if (isinf(x) && !isinf(y) && !isnan(y))
div = x;
else
div = (x - mod) / y;
if (y*mod < 0) {
mod += y;
div -= 1.0;
}
}

if (modp) *modp = mod;
if (divp) *divp = div;
}
Expand Down

0 comments on commit 9ea0b4b

Please sign in to comment.