Skip to content

pipe() & fork() code quality improvements#1919

Open
Explorer09 wants to merge 2 commits intohtop-dev:mainfrom
Explorer09:fdpair-init
Open

pipe() & fork() code quality improvements#1919
Explorer09 wants to merge 2 commits intohtop-dev:mainfrom
Explorer09:fdpair-init

Conversation

@Explorer09
Copy link
Contributor

  • The 'fdpairs' for pipe() are better initialized with values {-1, -1} as FD 0 is a valid FD (stdin). This change mainly helps debuggers of htop as they won't see junk values when pipe() fails.
  • Make the return value checks of pipe() and fork() consistent throughout the codebase.

Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

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

Split initialization change and failure checks into separate commits.

int fdpair[2];
if (pipe(fdpair) < 0)
int fdpair[2] = {-1, -1};
if (pipe(fdpair) != 0)
Copy link
Member

Choose a reason for hiding this comment

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

Check < 0 for failure; consistent with usual kernel error checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just wondering, is there a document about POSIX's convention of return values with these system calls? Such as whether the standard does reserve all nonnegative return values for successful operations.

@BenBE BenBE added enhancement Extension or improvement to existing feature code quality ♻️ Code quality enhancement labels Mar 16, 2026
Avoid file descriptor 0 (stdin) as initial values of 'fdpair' arrays.
When pipe(2) fails, the 'fdpair' would be left unchanged. This helps
debuggers of htop by avoiding junk values in 'fdpair'.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Make the return value checks of pipe() and fork() consistent throughout
the codebase.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code quality ♻️ Code quality enhancement enhancement Extension or improvement to existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants