Skip to content

Fix index out of bounds panic in keyboard rendering#121

Closed
Copilot wants to merge 4 commits into
masterfrom
copilot/fix-index-out-of-bounds-error
Closed

Fix index out of bounds panic in keyboard rendering#121
Copilot wants to merge 4 commits into
masterfrom
copilot/fix-index-out-of-bounds-error

Conversation

Copilot AI commented Apr 5, 2026

Copy link
Copy Markdown
  • Inspect keyboard layout/rendering code and identify why the last key is black
  • Run existing build/test/lint checks to capture baseline status (blocked by missing system ALSA library in environment)
  • Adjust keyboard black/white classification so key index 255 renders as white
  • Run targeted checks to ensure the change compiles and does not break behavior (build remains blocked by missing ALSA system library in this environment)
  • Summarize the fix and validation results

Replace direct array indexing `colors[i]` with bounds-checked
`colors.get(i).copied().flatten()` to handle the case where the
keyboard layout's visible range (0..257) exceeds the colors array
length (256).

Agent-Logs-Url: https://github.com/BlackMIDIDevs/wasabi/sessions/fd310b1e-3b5f-414b-b875-cdfeb59777b2

Co-authored-by: Kaydax <11893096+Kaydax@users.noreply.github.com>
All key/column/buffer arrays throughout the pipeline now match the
keyboard layout's 257 keys (indices 0-256). This replaces the
bounds-check workaround with properly sized data structures.

Changes:
- MIDI data: ram/parse, live/view, live/parse/notes, cake/tree_threader
- GPU rendering: note_list_system buffers, cake_system BUFFER_ARRAY_LEN
- GLSL shaders: key_positions[257], buffers[257]
- Keyboard drawing: direct indexing, fallback colors vec![None; 257]

Agent-Logs-Url: https://github.com/BlackMIDIDevs/wasabi/sessions/3254dffb-93f0-4223-8aaf-ba59ec352a99

Co-authored-by: Kaydax <11893096+Kaydax@users.noreply.github.com>
The keyboard layout arrays had an extra phantom key 256 that didn't
correspond to any MIDI key. This was the root cause of the
index-out-of-bounds issue when visible_range included index 256.

Changes (all in keyboard_layout.rs):
- Reduce keys/notes arrays from [; 257] to [; 256]
- Reduce initialization loops from 0..257 to 0..256
- Clamp visible_range to max 256 with bounds checks
- Keep load_key_numbers at 257 entries (Classic layout lookahead)

Reverts all incorrect 256->257 expansions from previous commit.

Agent-Logs-Url: https://github.com/BlackMIDIDevs/wasabi/sessions/1cf1b20a-e425-454a-95a4-5e8d0cd21326

Co-authored-by: Kaydax <11893096+Kaydax@users.noreply.github.com>

@Kaydax Kaydax left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Kaydax Kaydax marked this pull request as ready for review April 5, 2026 19:43
Copilot AI review requested due to automatic review settings April 5, 2026 19:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an index-out-of-bounds panic in keyboard rendering by aligning KeyboardLayout’s internal key/note arrays to the actual MIDI key domain (0–255) and ensuring visible ranges never exceed that bound.

Changes:

  • Shrinks KeyboardLayout storage from 257 to 256 entries and updates iteration bounds accordingly.
  • Adds guards/clamping in view range computation to prevent right_key - 1 underflow and to keep visible_range within 0..=256 (exclusive end).
  • Updates iter_all_keys / iter_all_notes to iterate 0..256.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 42 to 46
pub struct KeyboardLayout {
keys: [KeyPosition; 257],
notes: [KeyPosition; 257],
keys: [KeyPosition; 256],
notes: [KeyPosition; 256],
}

Copilot AI Apr 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple hard-coded 256 values in this module (array sizes, loop bounds, clamp/guards). To reduce the chance of future off-by-one mismatches, consider introducing a single const KEY_COUNT: usize = 256 (and KEY_COUNT_F: f32 if needed) and using it consistently for bounds, unwrap_or, and min() calls.

Copilot uses AI. Check for mistakes.
Copilot AI requested a review from Kaydax April 21, 2026 18:43
Copilot stopped work on behalf of Kaydax due to an error April 21, 2026 18:52
@Kaydax

Kaydax commented Apr 22, 2026

Copy link
Copy Markdown
Member

Yah idk this code is pretty shit. You have let me down claude

@Kaydax Kaydax closed this Apr 22, 2026
@Kaydax Kaydax deleted the copilot/fix-index-out-of-bounds-error branch April 22, 2026 19:34
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.

3 participants