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

Can implement coerce by packing a borrowed pointer inside a @Trait #7247

Closed
bblum opened this issue Jun 19, 2013 · 1 comment
Closed

Can implement coerce by packing a borrowed pointer inside a @Trait #7247

bblum opened this issue Jun 19, 2013 · 1 comment
Labels
A-traits Area: Trait system A-typesystem Area: The type system I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

Comments

@bblum
Copy link
Contributor

bblum commented Jun 19, 2013

The gist is that the kind-checker permits putting borrowed pointers into traits, which will cause the compiler to forget about the lifetime. The following program uses this to return a borrowed pointer outside of its stack frame, and then collides another stack frame into it to implement type coercion.

This will be fixed by #3569.

trait Foo<T> {
    fn get(@self) -> T;
}

struct Bar<'self, T> {
    x: &'self Option<T>,
}

impl <'self, T: Copy> Foo<T> for Bar<'self, T> {
    #[inline(always)] // Needed to avoid clobbering too early!
    fn get(@self) -> T {
        copy *self.x.get_ref()
    }
}

fn foo<T: 'static + Copy>(val: Option<T>) -> @Foo<T> {
    let x = val;
    let bar = Bar { x: &x };
    return @bar as @Foo<T>; // this is the line that should be illegal
}

fn coerce<A: 'static + Copy, B: 'static + Copy>(val: A) -> B {
    // Contains a borrowed pointer past the end of the stack.
    let x = foo(None::<B>);
    // The value passed in collides with the stack location pointed to inside x.
    // Need to call it "_z", not "_", because the latter will immediately get freed.
    let _z = foo(Some(val));
    x.get()
}
fn main() {
    let y: ~str = coerce(~[0x31u8, 0x33, 0x33, 0x37, 0]);
    println(fmt!("%?", y));
}
@ghost ghost assigned bblum Jun 19, 2013
@nikomatsakis
Copy link
Contributor

Dup of #5723 I believe

@bblum bblum removed their assignment Jun 16, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 3, 2021
Fix missing_docs_in_private_items false negative

changelog: Fix [`missing_docs_in_private_items`] false negative when the item has any `#[name = "value"]` attribute

Closes rust-lang#7247 (decided not to use the rustc method since it calls `Session::check_name`, which is for rustc only)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system A-typesystem Area: The type system I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Projects
None yet
Development

No branches or pull requests

2 participants