Gcode_arcs: Unhandled OverflowError/ValueError shutdown, float64 cancellation, and missing geometry validation in planArc()

Summary

ArcSupport.planArc() in klippy/extras/gcode_arcs.py computes arc segments directly from raw I/J/K offsets without checking for arithmetic overflow, extreme radii/segment counts, or geometric validity, and dispatches G1 sub-moves inside the loop before validating the trajectory. This causes three distinct failures on finite, syntactically valid G2/G3 commands:

  1. Unhandled OverflowError / ValueError → Printer Emergency Shutdown on finite large I/J/K values.
  2. Catastrophic float64 cancellation + hundreds of bogus physical moves executed before soft-endstop rejection on large I/J/K values.
  3. Silent execution of geometrically impossible arcs (large start/end radius mismatch) with a straight-line jump on the final segment.

Root Cause (klippy/extras/gcode_arcs.py:104-180)

  1. No overflow / finiteness / segment-bound guard before math.floor() and int(segments) (lines 118–140, 155):

    angular_travel = math.atan2(r_P * rt_Beta - r_Q * rt_Alpha,
                                r_P * rt_Alpha + r_Q * rt_Beta)
    ...
    radius = math.hypot(r_P, r_Q)
    flat_mm = radius * angular_travel
    ...
    segments = max(1., math.floor(mm_of_travel / self.mm_per_arc_segment))
    ...
    for i in range(1, int(segments) + 1):
    

    Even when I and J are finite floats, r_P * rt_Beta, math.hypot(r_P, r_Q), or radius * angular_travel can overflow to inf or evaluate to nan, causing math.floor() / int(segments) to raise an uncaught OverflowError or ValueError. In klippy/gcode.py:229-233, any exception other than CommandError triggers Internal error on command:"G2" and printer.invoke_shutdown().

  2. Catastrophic floating-point cancellation + mid-arc abort after physical travel (lines 114–115, 164–180):

    center_P = currentPos[alpha_axis] - r_P
    ...
    c[alpha_axis] = center_P + r_P
    ...
    self.gcode_move.cmd_G1(g1_gcmd)
    

    Adding currentPos to a huge I offset and later subtracting I * cos(theta) destroys low-order bits of currentPos (e.g., 20.0 + 1e17 - 1e17 == 16.0; 20.0 + 1e30 - 1e30 == 0.0). Because cmd_G1() is called inside the loop on every segment without upfront validation, the toolhead physically executes dozens or hundreds of off-course segments before check_move() eventually rejects an out-of-range segment.

  3. No start-radius vs. end-radius validation (lines 169–170):

    if i == segments:
        c = targetPos
    

    planArc() never checks whether hypot(r_P, r_Q) matches hypot(rt_Alpha, rt_Beta). Any wildly inconsistent arc silently traces segments around the start radius and then snaps in a straight line to targetPos on the final iteration.


Minimal Reproductions (Verified on master / 7558e8c, gcode_arcs.cfg [0, 200] limits)

(All inputs use plain decimal numbers — no scientific notation — so standard G-code tokenization passes them directly to I/J.)

Case 1: Finite I/J → Unhandled OverflowError / ValueError → Printer Shutdown

G28
G90
G1 X20 Y20 F100
; I = 1e308 (1 followed by 308 zeros, finite float)
G2 X20 Y20 I1000...<308 zeros>...0 J0 F100
  • Observed: radius * angular_travel overflows to -inf; math.floor() raises uncaught OverflowError: cannot convert float infinity to integer → !! Internal error on command:"G2" → full printer Shutdown.
  • Same with both I and J set to 1e308: atan2() produces nan, raising uncaught ValueError: cannot convert float NaN to integer → !! Internal error on command:"G2" → Shutdown.

Case 2: Large I → Catastrophic X Jump & 180 Bogus Physical Moves Before Move out of range

G28
G90
G1 X20 Y20 F100
G2 X20 Y20 I100000000000000000 J0 F100
  • Observed:
    • segments = 628318530717958656.
    • Due to float64 cancellation (center_P = 20.0 + 1e17 = 100000000000000016.0), segment i=1 immediately jumps from X=20.0 to X=16.0, Y=21.0.
    • Klipper executes 180 bogus moves all the way to (16.0, 200.0) before segment i=181 finally aborts with !! Move out of range: 16.000 201.000 0.000 [0.000].
    • With I = 10^30, X immediately jumps all the way to 0.0 and runs 180 bogus moves to (0.0, 200.0) before aborting.

Case 3: Impossible Arc (8 mm Radius Mismatch) Silently Executed

G28
G90
G1 X10 Y10 F100
G2 X20 Y10 I1 J0 F100
  • Observed: r_start = 1.0 mm, r_end = 9.0 mm. Klipper accepts the command with no error, executes 3 linear segments on a 1 mm semicircle, and then snaps straight across 8 mm to (20, 10) on the final segment.

Proposed Fix & Compatibility Note

  1. Guard against overflow / non-finite / excessive arc length & eliminate float64 cancellation (backwards-compatible with all existing tests):

    • Reject non-finite radius, angular_travel, mm_of_travel, or excessive segments / I/J/K magnitudes upfront with raise gcmd.error(...) instead of crashing with Internal error or dispatching bogus moves.
    • Compute intermediate coordinates relative to currentPos using delta_alpha = offset[0] * (1. - cos_Ti) + offset[1] * sin_Ti (or 2. * sin(c_theta / 2.)**2) and delta_beta = -offset[0] * sin_Ti + offset[1] * (1. - cos_Ti) so currentPos is never added to and subtracted from a large offset.
    • Use math.isnan / math.isinf (Python 2 & 3 compatible).
  2. Note on start/end radius consistency validation (test/klippy/gcode_arcs.test):

    • Notably, Klipper’s existing regression test test/klippy/gcode_arcs.test (lines 9, 12, 25, 28, 41, 44, e.g. G1 X20 Y20 Z20 followed by G2 X125 Y32 Z20 E1 I10.5 J10.5, where r_start ≈ 14.85 mm vs r_end ≈ 94.51 mm) currently contains synthetic arcs with large radius mismatches.
    • Would maintainers prefer:
      • (a) Fixing only overflow/finiteness/bounds + relative coordinate interpolation first (keeping existing gcode_arcs.test untouched), or
      • (b) Also enforcing a start/end radius tolerance (similar to RS274NGC / Marlin / GRBL) and updating test/klippy/gcode_arcs.test to geometrically valid arcs?