Skip to content

fix(gamepad-demo): avoid OOB when device name fills the buffer - #273

Merged
apotema merged 1 commit into
mainfrom
fix/gamepad-name-oob
Jun 11, 2026
Merged

apotema merged 1 commit into
mainfrom
fix/gamepad-name-oob

Conversation

@apotema

@apotema apotema commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #272. record() capped names at NAME_CAP, so name_len could equal NAME_CAP (64) and nameFor's sentinel slice e.name[0..name_len :0] read index 64 of a [64]u8 → out-of-bounds panic. Caps record at NAME_CAP-1 (reserve the NUL) and clamps in nameFor; adds a buffer-filling regression test. Both host tests pass.

(Caught in #272's review but lost to the same detached-worktree push no-op; re-applying properly.)

record() capped names at NAME_CAP, so name_len could equal NAME_CAP (64)
and nameFor's sentinel slice e.name[0..name_len :0] read index 64 of a
[64]u8 -> out-of-bounds panic. Cap record at NAME_CAP-1 (reserve the NUL)
and clamp in nameFor; add a buffer-filling regression test. (Re-do of a
#272-review fix that was lost to a detached-worktree push no-op.)
@cursor

cursor Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Localized memory-safety fix in example demo code with defensive clamping and a unit test; no auth, persistence, or engine API changes.

Overview
Fixes an out-of-bounds panic in the gamepad-android demo’s connected_pads registry when a device name is long enough to fill the fixed 64-byte name buffer.

record() now truncates names to NAME_CAP - 1 so one byte remains for a NUL terminator. nameFor() clamps name_len the same way, writes the terminator in bounds, and returns a valid sentinel-terminated slice instead of using name_len == NAME_CAP, which previously made e.name[0..name_len :0] read past the buffer.

Adds a regression test that records a name longer than NAME_CAP and asserts truncated length and an in-bounds NUL (issue #270).

Reviewed by Cursor Bugbot for commit 584c2cc. Bugbot is set up for automated code reviews on this repo. Configure here.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request fixes an out-of-bounds panic bug in examples/gamepad-android/scripts/connected_pads.zig by capping the gamepad name length at NAME_CAP - 1 in record and defensively clamping and terminating the name in nameFor. A test case has also been added to verify this behavior. The reviewer notes that the same bug exists in examples/gamepad/scripts/connected_pads.zig and recommends applying the same fix there to prevent crashes on non-Android platforms.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +53 to +56
// Cap at NAME_CAP-1 so a terminator always fits within the fixed buffer
// (nameFor returns a sentinel-terminated slice; a name filling all
// NAME_CAP bytes would leave no room for the NUL → OOB).
const n = @min(name.len, NAME_CAP - 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The same out-of-bounds panic bug exists in examples/gamepad/scripts/connected_pads.zig (which has a nearly identical implementation of record and nameFor). To prevent the same crash on non-Android platforms, please apply this fix to that file as well.

@apotema
apotema merged commit baba13d into main Jun 11, 2026
4 checks passed
@apotema apotema mentioned this pull request Jun 12, 2026
@apotema
apotema deleted the fix/gamepad-name-oob branch June 12, 2026 23:00
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