Skip to content

Potential saftey issue #8

@bbatha

Description

@bbatha

Using the following code I am able to reliably corrupt data in foo in rust nightly rustc 1.5.0-nightly (b2f379cdc 2015-09-23)

extern crate scoped_threadpool;
use scoped_threadpool::{Pool, Scope};

#[derive(Debug)]
pub struct RefOwner<'a>(&'a str);

pub struct ScopeRef<'a, 'pool, 'scope> where 'pool: 'scope, 'scope: 'a {
    scope: &'a Scope<'pool, 'scope>,
}

impl<'a, 'pool, 'scope> ScopeRef<'a, 'pool, 'scope> {
    pub fn new(scope: &'a Scope<'pool, 'scope>) -> ScopeRef<'a, 'pool, 'scope> {
        ScopeRef { scope: scope }
    }

    pub fn execute<'b: 'pool>(&self, ref_owner: &'b RefOwner) -> String {
        self.scope.execute(move || {
            println!("Got: {}", ref_owner.0);
        });
        ref_owner.0.to_owned()
    }
}

#[test]
fn it_works() {
    let foo = "foo".to_owned();
    let mut pool = Pool::new(2);

    pool.scoped(|scope| {
        let ref_owner= RefOwner(&foo);
        let scope_ref = ScopeRef::new(scope);
        let result = scope_ref.execute(&ref_owner);
        assert_eq!(foo, result);
    });
}
     Running target/debug/pool_problems-5eb09be270cfb456

running 1 test
Got: H
test it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests pool_problems

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions