Skip to content
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

Change FD_SETSIZE to c_int #3356

Merged
merged 1 commit into from Jan 8, 2024
Merged

Change FD_SETSIZE to c_int #3356

merged 1 commit into from Jan 8, 2024

Conversation

magicant
Copy link
Contributor

FD_SETSIZE defines an upper bound for file descriptors that can be added to fd_set. It is common to check if a file descriptor is less than FD_SETSIZE before adding it to an fd_set. See the example below:

fn main() {
    let path = std::ffi::CStr::from_bytes_with_nul(b"/dev/null\0").unwrap();
    let fd = unsafe { libc::open(path.as_ptr(), libc::O_RDONLY) };
    if fd < 0 {
        eprint!("open failed\n");
        return;
    }

    let mut set = std::mem::MaybeUninit::<libc::fd_set>::uninit();
    unsafe { libc::FD_ZERO(set.as_mut_ptr()) }
    if fd < libc::FD_SETSIZE { // <- type mismatch!
        unsafe { libc::FD_SET(fd, set.as_mut_ptr()) }
    } else {
        eprint!("too large fd\n");
    }
}

Unfortunately, this example does not compile because of a type mismatch. fd is c_int while FD_SETSIZE is usize (or size_t).
Since FD_SETSIZE represents the max file descriptor + 1, I think it should have the same type as file descriptors. This pull request modifies the type to c_int and adds casts where needed.

@rustbot
Copy link
Collaborator

rustbot commented Sep 18, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @JohnTitor (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot
Copy link
Collaborator

rustbot commented Sep 18, 2023

Some changes occurred in solarish module

cc @jclulow, @pfmooney

Some changes occurred in OpenBSD module

cc @semarie

@bors
Copy link
Contributor

bors commented Sep 21, 2023

☔ The latest upstream changes (presumably #3348) made this pull request unmergeable. Please resolve the merge conflicts.

@sthibaul
Copy link
Contributor

sthibaul commented Nov 9, 2023

Please also update src/unix/hurd/mod.rs, introduced in the meanwhile, thanks!

@magicant
Copy link
Contributor Author

magicant commented Nov 9, 2023

Rebased and updated my commit.

I also changed _POSIX_OPEN_MAX as well as _POSIX_FD_SETSIZE to c_int since the latter is defined as an alias of the former.

@bors
Copy link
Contributor

bors commented Nov 9, 2023

☔ The latest upstream changes (presumably #3428) made this pull request unmergeable. Please resolve the merge conflicts.

@sthibaul
Copy link
Contributor

sthibaul commented Nov 9, 2023

Ah, sorry, my commit indeed modified a lot, up to conflicting yours.

@magicant
Copy link
Contributor Author

No problem, my diff is so small that the conflict can be easily resolved 🙂

@JohnTitor
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jan 8, 2024

📌 Commit 500365e has been approved by JohnTitor

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 8, 2024

⌛ Testing commit 500365e with merge 7412a8b...

@bors
Copy link
Contributor

bors commented Jan 8, 2024

☀️ Test successful - checks-actions, checks-cirrus-freebsd-13, checks-cirrus-freebsd-14
Approved by: JohnTitor
Pushing 7412a8b to main...

@bors bors merged commit 7412a8b into rust-lang:main Jan 8, 2024
11 checks passed
@magicant magicant deleted the fd_setsize_type branch March 24, 2024 04:01
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.

None yet

5 participants