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

How to pass a null ptr to some functions? #480

Closed
hicqu opened this issue Dec 1, 2016 · 2 comments
Closed

How to pass a null ptr to some functions? #480

hicqu opened this issue Dec 1, 2016 · 2 comments

Comments

@hicqu
Copy link
Contributor

hicqu commented Dec 1, 2016

look at this function:

fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &mut EpollEvent)

if EpollOp == EpollDel, the event could be a NULL in C. But how to pass a NULL ptr to epoll_ctl in rust?

@fiveop
Copy link
Contributor

fiveop commented Dec 2, 2016

This is a missing feature of our interface. We had a similar problem with kill (see #445). Feel free to create a PR to fix this. We will incorporate it, even if it is a breaking change. We should probably check in the new implementation of epoll_ctl, whether event may be None (or NULL in C) depending on the other arguments.

@hicqu
Copy link
Contributor Author

hicqu commented Dec 3, 2016

OK, I think we can define these two functions in mio. Here is a simple test:

/// maybe we could accept these two functions into mio?
fn null_as_mut<T>() -> &'static mut T {
    unsafe { &mut *::std::ptr::null_mut::<T>() }
}

fn null_as_ref<T>() -> &'static T {
    unsafe { & *::std::ptr::null::<T>() }
}

/// simple test below

#[derive(Debug)]
struct T { f: i32, }

fn ref_t(t: &T) {
    // if pass nullptr, segment fault as expected.
    println!("{:?}", *t);   
}

fn mut_t(t: &mut T) {
    // if pass nullptr, segment fault as expected.
    println!("{:?}", *t);   
}

fn main() {
    let mut t: T = T { f: 100 };
    ref_t(&t);  // it's OK.

    mut_t(null_as_mut::<T>());  // segment fault.
    ref_t(null_as_ref::<T>());  // segment fault.
}

What do you think?

homu added a commit that referenced this issue Jan 11, 2017
fix #480 and add simple test cases for that.

r? @fiveop
@homu homu closed this as completed in bff660f Jan 11, 2017
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

No branches or pull requests

2 participants