Skip to content

Bulletproof handling around the GPIO fd#260

Merged
fhunleth merged 1 commit into
mainfrom
bulletproof-fd-handling
Jul 12, 2026
Merged

Bulletproof handling around the GPIO fd#260
fhunleth merged 1 commit into
mainfrom
bulletproof-fd-handling

Conversation

@fhunleth

Copy link
Copy Markdown
Member

No description provided.

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

This PR hardens GPIO resource lifecycle handling by making “closed/not-open” state explicit via pin->fd = -1, aligning cleanup paths, and ensuring stub backend read/write fail cleanly when the pin isn’t open.

Changes:

  • Add pin->fd < 0 guards to the stub backend’s hal_read_gpio/hal_write_gpio to return -EBADF when not open.
  • Set pin->fd = -1 in the cdev backend’s hal_close_gpio after closing the fd.
  • Make NIF cleanup call hal_close_gpio unconditionally and normalize pin->env reset to NULL; document that hal_close_gpio is idempotent.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
c_src/hal_stub.c Prevents stub read/write operations on unopened/closed pins by returning -EBADF.
c_src/hal_cdev_gpio.c Marks the GPIO fd invalid after close to avoid reuse/accidental operations on a stale fd.
c_src/gpio_nif.h Documents that hal_close_gpio may be called more than once.
c_src/gpio_nif.c Simplifies release logic by always calling hal_close_gpio and using NULL for cleared env pointers.

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

@fhunleth
fhunleth merged commit 90771de into main Jul 12, 2026
10 checks passed
@fhunleth
fhunleth deleted the bulletproof-fd-handling branch July 12, 2026 15:45
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.

2 participants