Skip to content

fix(pitchstaircase): correct stair ratio corruption, undefined crash, and broken recursion termination#7469

Open
netram75 wants to merge 1 commit into
sugarlabs:masterfrom
netram75:fix/pitchstaircase-playback-bugs
Open

fix(pitchstaircase): correct stair ratio corruption, undefined crash, and broken recursion termination#7469
netram75 wants to merge 1 commit into
sugarlabs:masterfrom
netram75:fix/pitchstaircase-playback-bugs

Conversation

@netram75

@netram75 netram75 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Description:

Fixes #7468

PR Category

  • Bug Fix
  • Feature
  • Performance
  • Tests
  • Documentation
  • Chore/Refactor

Description

Three bugs in js/widgets/pitchstaircase.js affecting the dissect button and playback functions.

Bug 1 - Stair ratio corruption in _dissectStair

After finding the source stair at index n, the loop calls splice(i, 0, [...]) to insert the new stair at position i. When i < n, this shifts every element right by one so this.Stairs[n] no longer points to the original source stair. The numerator, denominator, base frequency, and octave were being read from the wrong stair, silently storing incorrect ratio metadata on every new step created below the source.

Fix: snapshot this.Stairs[n][3], [4], [2], and [6] into local constants before the loop starts, so all three splice call sites use the correct values regardless of where insertion happens.

Bug 2 - TypeError crash when previousRowNumber is negative in _playNext

On the first descending step (index = 0, next = 1), previousRowNumber becomes -1. JavaScript returns undefined for negative array indices, not null. The guard was pscTableCell !== null which passed undefined through. The next line .rows[0].cells[1] then threw a TypeError, crashing Play Up-and-Down before the first note highlighted.

Fix: guard previousRowNumber >= 0 before the array access and assign null explicitly when negative, then check !== null && !== undefined to safely skip the cell reset.

Bug 3 - Recursion never terminates due to || instead of &&

The recursive call at the bottom of _playNext was gated by:

if (index < this.Stairs.length || index > -1)

This is always true for any reachable index. The boundary exit handlers at the top of the function (index === -1 and index === this.Stairs.length) were meant to stop the recursion but they were effectively unreachable via normal flow.

Fix: changed to && so the condition correctly bounds the index on both sides.

Changes Made

  • js/widgets/pitchstaircase.js - three targeted fixes, no other files touched

Testing Performed

  • npm run lint: 0 errors
  • npx prettier --check js/widgets/pitchstaircase.js: pass
  • npm test: all suites pass

… and broken recursion termination

- snapshot Stairs[n] metadata before splice loop in _dissectStair so
  inserting at i < n does not shift the source index and corrupt ratio
  values on the new stair
- guard previousRowNumber >= 0 before _stepTables access in _playNext
  to prevent TypeError when index is undefined on negative array lookup
- fix || to && in recursion termination guard so playback stops cleanly
  at both boundaries (-1 and Stairs.length)

Fixes sugarlabs#7468
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

🧪 Jest Test Results

❌ Some Jest tests failed. Please check the logs and fix the issues before merging.

Coverage: Statements: 48.29% | Branches: 39.82% | Functions: 52.98% | Lines: 48.68%
Master Coverage: Statements: 48.29% | Branches: 39.83% | Functions: 52.98% | Lines: 48.69%

Note: These failures may be introduced by this PR or may already exist in the master branch.
Tip: Update your branch with the latest master and rerun tests.
If the same failures are present on master, they are likely not introduced by this PR.

Failed Tests:

aidebugger.test.js

@github-actions github-actions Bot added bug fix Fixes a bug or incorrect behavior size/S Small: 10-49 lines changed area/javascript Changes to JS source files labels Jun 3, 2026
@netram75

netram75 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

@walterbender Please have a look when you get time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/javascript Changes to JS source files bug fix Fixes a bug or incorrect behavior size/S Small: 10-49 lines changed

Projects

Development

Successfully merging this pull request may close these issues.

[Bug] PitchStaircase: stair ratio corruption, TypeError on playback start, and broken recursion termination in _dissectStair / _playNext

1 participant