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

@@ -15062,9 +15062,24 @@
1506215062 tname = tuple_c_name(tt)
1506315063 arg = compile_arg0(nid)
1506415064 tmp = new_temp
15065+ @needs_setjmp = 1
15066+ # CRuby raises:
15067+ # ZeroDivisionError on `1.0.divmod(0)` / `divmod(0.0)`.
15068+ # FloatDomainError("NaN") on NaN dividend or NaN divisor.
15069+ # Bind both operands to float locals before the raise checks so
15070+ # they're each evaluated exactly once and so gcc can't constant-
15071+ # fold `1.0 / 0LL` and trip -Wdiv-by-zero on the literal-zero
15072+ # path below. The (mrb_int)floor cast on a NaN result would also
15073+ # be C undefined behaviour; raise before reaching it.
15074+ recv_tmp = new_temp
15075+ arg_tmp = new_temp
15076+ emit(" mrb_float " + recv_tmp + " = (mrb_float)(" + rc + ");")
15077+ emit(" mrb_float " + arg_tmp + " = (mrb_float)(" + arg + ");")
15078+ emit(" if (" + arg_tmp + " == 0.0) sp_raise_cls(\"ZeroDivisionError\", \"divided by 0\");")
15079+ emit(" if (" + arg_tmp + " != " + arg_tmp + " || " + recv_tmp + " != " + recv_tmp + ") sp_raise_cls(\"FloatDomainError\", \"NaN\");")
1506515080 emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
15066- emit(" " + tmp + "->_0 = (mrb_int)floor(" + rc + " / " + arg + ");")
15067- emit(" " + tmp + "->_1 = " + rc + " - " + tmp + "->_0 * " + arg + ";")
15081+ emit(" " + tmp + "->_0 = (mrb_int)floor(" + recv_tmp + " / " + arg_tmp + ");")
15082+ emit(" " + tmp + "->_1 = " + recv_tmp + " - " + tmp + "->_0 * " + arg_tmp + ";")
1506815083 return tmp
1506915084 end
1507015085 ""