Commit 825d51aa
825d51aa5375Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #479 from ryanseys/rs/float-divmod-zerodiv
fix(codegen): Float#divmod(0) raises ZeroDivisionError
Details
- Commit
- 825d51aa5375
- Parent
- b88833151a0d
- Merge parent
- b9127617f4a8
spinel_codegen.rbmodifiedView file
@@ -14946,9 +14946,24 @@
1494614946
tname = tuple_c_name(tt)1494714947
arg = compile_arg0(nid)1494814948
tmp = new_temp14949+
@needs_setjmp = 114950+
# CRuby raises:14951+
# ZeroDivisionError on `1.0.divmod(0)` / `divmod(0.0)`.14952+
# FloatDomainError("NaN") on NaN dividend or NaN divisor.14953+
# Bind both operands to float locals before the raise checks so14954+
# they're each evaluated exactly once and so gcc can't constant-14955+
# fold `1.0 / 0LL` and trip -Wdiv-by-zero on the literal-zero14956+
# path below. The (mrb_int)floor cast on a NaN result would also14957+
# be C undefined behaviour; raise before reaching it.14958+
recv_tmp = new_temp14959+
arg_tmp = new_temp14960+
emit(" mrb_float " + recv_tmp + " = (mrb_float)(" + rc + ");")14961+
emit(" mrb_float " + arg_tmp + " = (mrb_float)(" + arg + ");")14962+
emit(" if (" + arg_tmp + " == 0.0) sp_raise_cls(\"ZeroDivisionError\", \"divided by 0\");")14963+
emit(" if (" + arg_tmp + " != " + arg_tmp + " || " + recv_tmp + " != " + recv_tmp + ") sp_raise_cls(\"FloatDomainError\", \"NaN\");")1494914964
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")14950-
emit(" " + tmp + "->_0 = (mrb_int)floor(" + rc + " / " + arg + ");")14951-
emit(" " + tmp + "->_1 = " + rc + " - " + tmp + "->_0 * " + arg + ";")14965+
emit(" " + tmp + "->_0 = (mrb_int)floor(" + recv_tmp + " / " + arg_tmp + ");")14966+
emit(" " + tmp + "->_1 = " + recv_tmp + " - " + tmp + "->_0 * " + arg_tmp + ";")1495214967
return tmp1495314968
end1495414969
""test/float_divmod_zerodiv.rbaddedView file
Loading diff…
test/float_divmod_zerodiv.rb.expectedaddedView file
Loading diff…