summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMasaki Muranaka <[email protected]>2013-02-23 10:47:13 +0900
committerMasaki Muranaka <[email protected]>2013-02-23 11:42:27 +0900
commit9ea0b4b7d16e85c666bc9ce2ed16cc868a196aba (patch)
treeb6213582495135a54f37f7076b0b0541739c0dc5
parent82313b6ec7ebb89c5993d3359d5844df7e230f9d (diff)
downloadmruby-9ea0b4b7d16e85c666bc9ce2ed16cc868a196aba.tar.gz
mruby-9ea0b4b7d16e85c666bc9ce2ed16cc868a196aba.zip
Fix an underlying bug. flodivmod() will be crashed in case ((y == 0) && ((divp == NULL) || (modp == NULL))).
-rw-r--r--src/numeric.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/src/numeric.c b/src/numeric.c
index 6a4e7ca57..a4f2ed6e0 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -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;
}