Skip to content

Conversation

@txy-21
Copy link
Contributor

@txy-21 txy-21 commented Jan 21, 2026

Summary

This PR fixes a file descriptor leak in the popen() implementation when the read and write file descriptors are duplicated (when newfd[0] equals newfd[1]).

The bug was in the file descriptor cleanup logic where the condition if (rw) alone was insufficient to handle the edge case where both pipe ends point to the same descriptor.

Changes

  • File: system/popen/popen.c
  • Change: Added additional condition check newfd[0] != newfd[1] before closing the write-side file descriptor
  • Impact: Prevents file descriptor leaks when pipes have duplicated file descriptors

Testing

Tested with:

  • NuttX popen() basic functionality
  • Edge case: pipe with duplicated file descriptors (newfd[0] == newfd[1])
  • No file descriptor leaks observed

Verification Checklist

  • Code compiles without warnings
  • Existing popen tests pass
  • File descriptor properly closed in edge case
  • No breaking changes to popen() API

Fixes a bug where newfd is not properly closed when newfd[0] equals newfd[1].
This can cause file descriptor leaks in certain edge cases where the read
and write sides of the pipe are duplicated.

Signed-off-by: hongfengchen <hongfengchen@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants