Commit 9ea0b4b7 authored by Masaki Muranaka's avatar Masaki Muranaka

Fix an underlying bug. flodivmod() will be crashed in case ((y == 0) && ((divp...

Fix an underlying bug. flodivmod() will be crashed in case ((y == 0) && ((divp == NULL) || (modp == NULL))).
parent 82313b6e
...@@ -244,22 +244,25 @@ flo_mul(mrb_state *mrb, mrb_value x) ...@@ -244,22 +244,25 @@ flo_mul(mrb_state *mrb, mrb_value x)
static void static void
flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float *modp) 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) { if (y == 0.0) {
*divp = str_to_mrb_float("inf"); div = str_to_mrb_float("inf");
*modp = str_to_mrb_float("nan"); mod = str_to_mrb_float("nan");
return;
} }
mod = fmod(x, y); else {
if (isinf(x) && !isinf(y) && !isnan(y)) mod = fmod(x, y);
div = x; if (isinf(x) && !isinf(y) && !isnan(y))
else div = x;
div = (x - mod) / y; else
if (y*mod < 0) { div = (x - mod) / y;
mod += y; if (y*mod < 0) {
div -= 1.0; mod += y;
div -= 1.0;
}
} }
if (modp) *modp = mod; if (modp) *modp = mod;
if (divp) *divp = div; if (divp) *divp = div;
} }
......
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