Skip to content

refactor: redefine ForkptyResult to avoid uninited master field #2315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

SteveLauC
Copy link
Member

What does this PR do

Before this PR, our ForkptrResult was defined like this:

pub struct ForkptyResult {
    pub master: OwnedFd,
    pub fork_result: ForkResult,
}

The problem with this definition is that the master field won't be initialized (actually, it won't be accessed at all) within the forked child process, which means that our forkpty() was calling OwnedFd::from_raw_fd(an uninitialized memory), if the dirty value stored in that uninitialized memory is not a valid value for a file descriptor (say -1), then this function will panic, making the child process panic.

This PR redefines the ForkptyResult type so that the master field is only present in the parent process.

cc @Xeom

related to #2312

Checklist:

  • I have read CONTRIBUTING.md
  • I have written necessary tests and rustdoc comments
  • A change log has been added if this PR modifies nix's API

@SteveLauC SteveLauC requested a review from asomers February 16, 2024 04:44
@Xeom
Copy link

Xeom commented Feb 16, 2024

This looks perfect - thank you so much!

I've tried out your branch in the original codebase where I was experiencing a crash, and it works great.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

Yes, I think this is exactly right.

@SteveLauC SteveLauC added this pull request to the merge queue Feb 25, 2024
Merged via the queue into nix-rust:master with commit 358fe9f Feb 25, 2024
@SteveLauC SteveLauC deleted the refactor/uninitialized_fd_forkpty_child branch February 25, 2024 23:52
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.

3 participants