Skip to content

fix(gui): prevent stack-overflow crash when emptying pattern/skip fields - #164

Merged
CameronBrooks11 merged 1 commit into
mainfrom
fix/helical-coprime-nan-crash
Jun 25, 2026
Merged

CameronBrooks11 merged 1 commit into
mainfrom
fix/helical-coprime-nan-crash

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

Symptom

A user hit "Something went wrong — RangeError: Maximum call stack size exceeded" (React error boundary) right after backspacing the Skip Index (or Pattern Number) value in a helical layer editor — no Enter needed. The stack was a single function recursing into Math.abs.

Root cause

Emptying a numeric input produces NaN. The editor's useEffect re-validates every helical field, so validating the sibling field runs the cross-field coprime check:

validateCoprime(pattern, helical.skip_index /* = NaN */) → gcd(pattern, NaN)

gcd is recursive Euclid, and gcd(x, NaN) never terminates — Math.abs(NaN) is NaN, NaN === 0 is false, and NaN % NaN is NaN — so it recurses until the stack overflows.

Fix

Guard validateCoprime to no-op unless both pattern and skip are integers (Number.isInteger also rejects NaN, Infinity, and non-integers). Coprimality is only defined for two positive integers anyway, and the field that's mid-edit still surfaces its own "must be a valid number" range error — so user-facing validation feedback is unchanged; only the crash is gone.

Tests

Added regression cases to helicalValidation.test.ts:

  • NaN skip index returns a range error instead of throwing
  • validating pattern_number with skip_index = NaN doesn't throw (and reports no spurious coprime error)
  • the symmetric case (pattern_number = NaN)
  • non-integer (2.5) sibling doesn't throw

vitest 24 passed (was 20), tsc --noEmit clean.

…mptied

Emptying the Pattern Number or Skip Index input yields NaN. The editor
re-validates all helical fields, so the sibling field's coprime check called
gcd() with the NaN value. gcd is recursive Euclid; gcd(x, NaN) never
terminates (Math.abs(NaN) is NaN, NaN === 0 is false, NaN % NaN is NaN), so
it blew the call stack and tripped the error boundary with
'RangeError: Maximum call stack size exceeded'.

Guard validateCoprime to no-op unless both pattern and skip are integers
(Number.isInteger also rejects NaN, Infinity, and non-integers). The
mid-edit field still surfaces its own range error, so validation feedback is
unchanged. Adds regression tests for NaN and non-integer siblings.
@CameronBrooks11
CameronBrooks11 merged commit ef53b44 into main Jun 25, 2026
15 checks passed
@CameronBrooks11
CameronBrooks11 deleted the fix/helical-coprime-nan-crash branch June 25, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant