Commit b9127617
b9127617f4a8Merged
Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): Float#divmod(0) raises ZeroDivisionError; NaN raises FloatDomainError
Companion to the existing Integer#divmod(0) raise. Without these
guards the float arm of compile_float_method_expr's divmod path
emits (mrb_int)floor(rc / arg) directly:
- when arg is 0.0 the division produces Infinity/NaN and the cast to mrb_int triggers undefined C behaviour at runtime.
- when either operand is NaN, the floor result is NaN and the same cast is also undefined behaviour.
Bind both operands to float locals before the raise checks so
they're each evaluated exactly once and so a literal 0 arg
(1.0.divmod(0)) doesn't trip gcc's -Wdiv-by-zero at compile
time on the floor expression below; the runtime raise fires
before the division actually happens, but the static warning
fires regardless because gcc sees the literal / 0 in the
source. With the operands through temps, gcc can't constant-fold
across the assignment and the warning is suppressed.
NaN check uses x != x (canonical C idiom that's true only for
NaN) so it's portable across compilers without depending on
isnan macro availability.
@needs_setjmp = 1 set so the setjmp infrastructure is wired
into any function that uses Float#divmod.
New test cases in test/float_divmod_zerodiv.rb:
- Non-zero divisor baseline (
5.0.divmod(2.0)→[2, 1.0]) - Int divisor zero / float divisor zero / bare rescue (existing)
- NaN divisor → FloatDomainError("NaN")
- NaN dividend → FloatDomainError("NaN")
Out of scope: Infinity dividend (CRuby raises FloatDomainError "Infinity"); Infinity divisor with finite dividend (CRuby returns [0, dividend] via a special-case the spec calls out separately). Both add adjacent edge-case complexity without further preventing UB; can be addressed in a follow-up.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b9127617f4a8
- Parent
- 1d5291554707
spinel_codegen.rbmodifiedView file
tname = tuple_c_name(tt) arg = compile_arg0(nid) tmp = new_temp @needs_setjmp = 1 # CRuby raises: # ZeroDivisionError on `1.0.divmod(0)` / `divmod(0.0)`. # FloatDomainError("NaN") on NaN dividend or NaN divisor. # Bind both operands to float locals before the raise checks so # they're each evaluated exactly once and so gcc can't constant- # fold `1.0 / 0LL` and trip -Wdiv-by-zero on the literal-zero # path below. The (mrb_int)floor cast on a NaN result would also # be C undefined behaviour; raise before reaching it. recv_tmp = new_temp arg_tmp = new_temp emit(" mrb_float " + recv_tmp + " = (mrb_float)(" + rc + ");") emit(" mrb_float " + arg_tmp + " = (mrb_float)(" + arg + ");") emit(" if (" + arg_tmp + " == 0.0) sp_raise_cls(\"ZeroDivisionError\", \"divided by 0\");") emit(" if (" + arg_tmp + " != " + arg_tmp + " || " + recv_tmp + " != " + recv_tmp + ") sp_raise_cls(\"FloatDomainError\", \"NaN\");") emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");") emit(" " + tmp + "->_0 = (mrb_int)floor(" + rc + " / " + arg + ");") emit(" " + tmp + "->_1 = " + rc + " - " + tmp + "->_0 * " + arg + ";") emit(" " + tmp + "->_0 = (mrb_int)floor(" + recv_tmp + " / " + arg_tmp + ");") emit(" " + tmp + "->_1 = " + recv_tmp + " - " + tmp + "->_0 * " + arg_tmp + ";") return tmp end ""test/float_divmod_zerodiv.rbaddedView file
Loading diff…
test/float_divmod_zerodiv.rb.expectedaddedView file
Loading diff…