Skip to content

fix(io): decode raw ioctl errno via linux.E.init, not posix.errno (libc swallows failures)#385

Merged
BANANASJIM merged 1 commit into
mainfrom
fix/raw-syscall-errno
Jun 7, 2026
Merged

fix(io): decode raw ioctl errno via linux.E.init, not posix.errno (libc swallows failures)#385
BANANASJIM merged 1 commit into
mainfrom
fix/raw-syscall-errno

Conversation

@BANANASJIM

Copy link
Copy Markdown
Owner

Problem

padctl links libc (build.zig). Under libc, std.posix.errno(rc) reads the C errno global, which is only set when a libc wrapper returns -1. But std.os.linux.ioctl is a raw syscall: it returns the negated errno encoded in a usize (never literally -1) and does NOT touch the C errno global.

So posix.errno(linux.ioctl(...)) always returns .SUCCESS under libc — silently swallowing every ioctl failure. The libc-independent decoder is std.os.linux.E.init(rc) (already proven + merged for hidraw.featureReport).

Fixed sites (all decode a raw linux.ioctl return)

  • src/io/uinput.zig ioctlInt / ioctlPtrUI_DEV_SETUP / UI_DEV_CREATE / UI_SET_* checks were silently succeeding; PERM and other failures are now surfaced.
  • src/io/hidraw.zig EVIOCGRAB — a failed grab was swallowed and the un-grabbed evdev fd was still appended. Extracted evdevGrabErrno, decode via linux.E.init; the fd is only kept on a real grab.
  • src/cli/scan.zig HIDIOCGRAWNAME — warning-only path now logs the real errno via linux.E.init.

Left unchanged (already correct): discoverWithRoot / discoverAllWithRoot compare rc != 0 directly, not via posix.errno.

Co-located cleanup

src/io/hidraw.zig discoverWithRoot allocated the matching path string twice (once for the loop, once for the return). Now returns the single existing allocation via a keep_path guard.

Test plan (Layer-0, falsifiable)

  • uinput: ioctlInt/ioctlPtr surfaces ioctl errno — drive over fd=-1 (EBADF), assert error.Unexpected. Under the old posix.errno decode they returned void (verified by reverting → both fail).
  • hidraw: evdevGrabErrno surfaces ioctl errno on bad fdevdevGrabErrno(-1) must return a non-SUCCESS errno (EBADF). Under posix.errno it returned .SUCCESS (verified by reverting → fails).
  • Updated hidraw: grabAssociatedEvdev: matches event by phys prefixEVIOCGRAB on a regular file returns ENOTTY, now correctly surfaced, so the un-grabbed fd is no longer appended (count 0; was 1 only because the error was swallowed).
  • scan HIDIOCGRAWNAME is warning-only (no error returned); covered by the uinput/hidraw tests sharing the identical decode pattern.

./scripts/padctl-docker test → EXIT=0. zig fmt --check (0.15.2) clean.

refs: codebase audit (systemic)

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Jun 7, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@BANANASJIM, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 23 minutes and 1 second. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 95787edd-bfc8-4655-a62b-c44587ceb716

📥 Commits

Reviewing files that changed from the base of the PR and between 96bd619 and fbf6c65.

📒 Files selected for processing (3)
  • src/cli/scan.zig
  • src/io/hidraw.zig
  • src/io/uinput.zig
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/raw-syscall-errno

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

…bc swallows failures)

padctl links libc. Under libc, std.posix.errno(rc) reads the C errno
global, which is only meaningful when a libc wrapper returns -1. But
std.os.linux.ioctl is a RAW syscall: it returns the negated errno encoded
in a usize (never literally -1) and does NOT touch the C errno global.
So posix.errno(linux.ioctl(...)) always returns .SUCCESS under libc,
silently swallowing every ioctl failure. The correct, libc-independent
decode is std.os.linux.E.init(rc) (already proven+fixed for
hidraw.featureReport).

Fixed sites (all decode a raw linux.ioctl return):
- src/io/uinput.zig ioctlInt / ioctlPtr: UI_DEV_SETUP / UI_DEV_CREATE /
  UI_SET_* were silently succeeding; PERM and other failures are now
  surfaced.
- src/io/hidraw.zig EVIOCGRAB: a failed grab was swallowed and the
  un-grabbed evdev fd was still appended. Extracted evdevGrabErrno and
  decode via linux.E.init; the fd is now only kept on a real grab.
- src/cli/scan.zig HIDIOCGRAWNAME: warning-only path now logs the real
  errno via linux.E.init.

discoverWithRoot / discoverAllWithRoot already compared rc != 0 directly
and were correct; left unchanged.

Also dedupes src/io/hidraw.zig discoverWithRoot: the matching path string
was allocated twice (once for the loop, once for the return). Now returns
the single existing allocation via a keep_path guard.

Tests (Layer-0, falsifiable):
- uinput ioctlInt/ioctlPtr over fd=-1 (EBADF) must return error.Unexpected;
  under the old posix.errno decode they returned void (proven by reverting).
- hidraw evdevGrabErrno(-1) must return a non-SUCCESS errno (EBADF); under
  posix.errno it returned .SUCCESS (proven by reverting).
- Updated existing 'grabAssociatedEvdev: matches event by phys prefix':
  EVIOCGRAB on a regular file returns ENOTTY, now correctly surfaced, so
  the un-grabbed fd is no longer appended (count 0, was 1 only because the
  error was swallowed).
- scan HIDIOCGRAWNAME is warning-only (no error returned); behavior is
  covered by the uinput/hidraw tests sharing the identical decode pattern.

refs: codebase audit (systemic)
@BANANASJIM BANANASJIM force-pushed the fix/raw-syscall-errno branch from 9f73d42 to fbf6c65 Compare June 7, 2026 05:11
@BANANASJIM BANANASJIM merged commit 6bb3fcc into main Jun 7, 2026
36 checks passed
@BANANASJIM BANANASJIM deleted the fix/raw-syscall-errno branch June 7, 2026 05:16
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