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

Safe code triggering UB #34714

Closed
Stebalien opened this issue Jul 7, 2016 · 4 comments
Closed

Safe code triggering UB #34714

Stebalien opened this issue Jul 7, 2016 · 4 comments

Comments

@Stebalien
Copy link
Contributor

Stebalien commented Jul 7, 2016

When compiled with rust nightly (optimizations disabled), the following reliably segfaults:

use std::iter::FromIterator;
use std::iter::IntoIterator;

pub struct BoxedIterator<T> {
    iter: Box<Iterator<Item = T>>,
}

impl<T> Iterator for BoxedIterator<T> {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

impl<T> FromIterator<T> for BoxedIterator<T> {
    fn from_iter<I>(iter: I) -> Self
        where I: IntoIterator<Item = T>,
              I::IntoIter: 'static
    {
        BoxedIterator { iter: Box::new(iter.into_iter()) }
    }
}

fn main() {
    let iter: Result<BoxedIterator<&str>, ()> =
        vec![Ok("test1"), Ok("test2")].into_iter().collect();
    let mut iter = iter.unwrap();

    println!("{:?}", iter.next());
}

When compiled with -C opt-level=3, it prints None but should print "test1".


Report from stackoverflow: https://stackoverflow.com/questions/38254647/why-does-calling-next-on-an-iterator-trait-object-give-me-an-out-of-memory-at
Thanks to Apanatshka for finding this.

@Stebalien
Copy link
Contributor Author

Here's a variant that segfaults in debug mode (on play): https://is.gd/IROdsv
I couldn't get the above code to segfault on play because there's no way to compile with both optimizations and debug mode off.

@eefriedman
Copy link
Contributor

This looks like the same bug as #18937.

@arielb1
Copy link
Contributor

arielb1 commented Jul 8, 2016

Sure. Duplicate of #18937.

@arielb1 arielb1 closed this as completed Jul 8, 2016
@Stebalien
Copy link
Contributor Author

I see. For anyone else coming here, the I::IntoIter: 'static bound on the FromIterator impl doesn't exist on the trait definition.

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

3 participants