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:
- Unhandled
OverflowError/ValueError→ Printer Emergency Shutdown on finite largeI/J/Kvalues. - Catastrophic
float64cancellation + hundreds of bogus physical moves executed before soft-endstop rejection on largeI/J/Kvalues. - 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)
-
No overflow / finiteness / segment-bound guard before
math.floor()andint(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
IandJare finite floats,r_P * rt_Beta,math.hypot(r_P, r_Q), orradius * angular_travelcan overflow toinfor evaluate tonan, causingmath.floor()/int(segments)to raise an uncaughtOverflowErrororValueError. Inklippy/gcode.py:229-233, any exception other thanCommandErrortriggersInternal error on command:"G2"andprinter.invoke_shutdown(). -
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
currentPosto a hugeIoffset and later subtractingI * cos(theta)destroys low-order bits ofcurrentPos(e.g.,20.0 + 1e17 - 1e17 == 16.0;20.0 + 1e30 - 1e30 == 0.0). Becausecmd_G1()is called inside the loop on every segment without upfront validation, the toolhead physically executes dozens or hundreds of off-course segments beforecheck_move()eventually rejects an out-of-range segment. -
No start-radius vs. end-radius validation (lines 169–170):
if i == segments: c = targetPosplanArc()never checks whetherhypot(r_P, r_Q)matcheshypot(rt_Alpha, rt_Beta). Any wildly inconsistent arc silently traces segments around the start radius and then snaps in a straight line totargetPoson 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_traveloverflows to-inf;math.floor()raises uncaughtOverflowError: cannot convert float infinity to integer→!! Internal error on command:"G2"→ full printer Shutdown. - Same with both
IandJset to1e308:atan2()producesnan, raising uncaughtValueError: 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
float64cancellation (center_P = 20.0 + 1e17 = 100000000000000016.0), segmenti=1immediately jumps fromX=20.0toX=16.0, Y=21.0. - Klipper executes 180 bogus moves all the way to
(16.0, 200.0)before segmenti=181finally aborts with!! Move out of range: 16.000 201.000 0.000 [0.000]. - With
I = 10^30,Ximmediately jumps all the way to0.0and 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
-
Guard against overflow / non-finite / excessive arc length & eliminate
float64cancellation (backwards-compatible with all existing tests):- Reject non-finite
radius,angular_travel,mm_of_travel, or excessivesegments/I/J/Kmagnitudes upfront withraise gcmd.error(...)instead of crashing withInternal erroror dispatching bogus moves. - Compute intermediate coordinates relative to
currentPosusingdelta_alpha = offset[0] * (1. - cos_Ti) + offset[1] * sin_Ti(or2. * sin(c_theta / 2.)**2) anddelta_beta = -offset[0] * sin_Ti + offset[1] * (1. - cos_Ti)socurrentPosis never added to and subtracted from a largeoffset. - Use
math.isnan/math.isinf(Python 2 & 3 compatible).
- Reject non-finite
-
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 Z20followed byG2 X125 Y32 Z20 E1 I10.5 J10.5, wherer_start ≈ 14.85 mmvsr_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.testuntouched), or - (b) Also enforcing a start/end radius tolerance (similar to RS274NGC / Marlin / GRBL) and updating
test/klippy/gcode_arcs.testto geometrically valid arcs?
- (a) Fixing only overflow/finiteness/bounds + relative coordinate interpolation first (keeping existing
- Notably, Klipper’s existing regression test