{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/31567","repository_url":"https://api.github.com/repos/rust-lang/rust","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/31567/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/31567/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/31567/events","html_url":"https://github.com/rust-lang/rust/issues/31567","id":132916400,"number":31567,"title":"`Drop` and leaking `&mut` references","user":{"login":"cuviper","id":36186,"avatar_url":"https://avatars.githubusercontent.com/u/36186?v=3","gravatar_id":"","url":"https://api.github.com/users/cuviper","html_url":"https://github.com/cuviper","followers_url":"https://api.github.com/users/cuviper/followers","following_url":"https://api.github.com/users/cuviper/following{/other_user}","gists_url":"https://api.github.com/users/cuviper/gists{/gist_id}","starred_url":"https://api.github.com/users/cuviper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cuviper/subscriptions","organizations_url":"https://api.github.com/users/cuviper/orgs","repos_url":"https://api.github.com/users/cuviper/repos","events_url":"https://api.github.com/users/cuviper/events{/privacy}","received_events_url":"https://api.github.com/users/cuviper/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rust-lang/rust/labels/I-unsound","name":"I-unsound","color":"e11d21"},{"url":"https://api.github.com/repos/rust-lang/rust/labels/P-high","name":"P-high","color":"eb6420"},{"url":"https://api.github.com/repos/rust-lang/rust/labels/regression-from-stable-to-nightly","name":"regression-from-stable-to-nightly","color":"FF309F"},{"url":"https://api.github.com/repos/rust-lang/rust/labels/T-compiler","name":"T-compiler","color":"bfd4f2"}],"state":"open","locked":false,"assignee":{"login":"pnkfelix","id":173127,"avatar_url":"https://avatars.githubusercontent.com/u/173127?v=3","gravatar_id":"","url":"https://api.github.com/users/pnkfelix","html_url":"https://github.com/pnkfelix","followers_url":"https://api.github.com/users/pnkfelix/followers","following_url":"https://api.github.com/users/pnkfelix/following{/other_user}","gists_url":"https://api.github.com/users/pnkfelix/gists{/gist_id}","starred_url":"https://api.github.com/users/pnkfelix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pnkfelix/subscriptions","organizations_url":"https://api.github.com/users/pnkfelix/orgs","repos_url":"https://api.github.com/users/pnkfelix/repos","events_url":"https://api.github.com/users/pnkfelix/events{/privacy}","received_events_url":"https://api.github.com/users/pnkfelix/received_events","type":"User","site_admin":false},"milestone":null,"comments":16,"created_at":"2016-02-11T08:34:49Z","updated_at":"2016-06-23T17:05:16Z","closed_at":null,"body":"There's a way to leak a `&mut` member borrowed from a struct, and then manipulate that member from the `Drop` handler -- even while it is immutably borrowed!\r\n\r\n```rust\r\nstruct VecWrapper<'a>(&'a mut Vec<u8>);\r\n\r\nimpl<'a> IntoIterator for VecWrapper<'a> {\r\n    type Item = &'a u8;\r\n    type IntoIter = std::slice::Iter<'a, u8>;\r\n\r\n    fn into_iter(self) -> Self::IntoIter {\r\n        self.0.iter()\r\n    }\r\n}\r\n\r\nimpl<'a> Drop for VecWrapper<'a> {\r\n    fn drop(&mut self) {\r\n        // aggressively free memory\r\n        self.0.clear();\r\n        self.0.shrink_to_fit();\r\n    }\r\n}\r\n\r\nfn main() {\r\n    let mut v = vec![1, 2, 3];\r\n    for i in VecWrapper(&mut v) {\r\n        let s = \"foo\".to_owned(); // reused allocation\r\n        println!(\"{}!  {} {:#x} '{}'\", s, i, i, *i as char);\r\n    }\r\n}\r\n```\r\n\r\n[playground](http://is.gd/v4Qi95) output:\r\n\r\n```\r\nfoo!  102 0x66 'f'\r\nfoo!  111 0x6f 'o'\r\nfoo!  111 0x6f 'o'\r\n```\r\n\r\nSo this drop frees the vec's buffer, then an unrelated string allocation reuses the same memory, and those new values come out of the `vec::Iter`.\r\n\r\nI think if the member was immutable `&`, then this \"leak\" from `into_iter()` would be fine since they can both share that reference, and `Drop` can't mutate it.  But `&mut` must be exclusive, of course..."},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/228115998","html_url":"https://github.com/rust-lang/rust/issues/31567#issuecomment-228115998","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/31567","id":228115998,"user":{"login":"cuviper","id":36186,"avatar_url":"https://avatars.githubusercontent.com/u/36186?v=3","gravatar_id":"","url":"https://api.github.com/users/cuviper","html_url":"https://github.com/cuviper","followers_url":"https://api.github.com/users/cuviper/followers","following_url":"https://api.github.com/users/cuviper/following{/other_user}","gists_url":"https://api.github.com/users/cuviper/gists{/gist_id}","starred_url":"https://api.github.com/users/cuviper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cuviper/subscriptions","organizations_url":"https://api.github.com/users/cuviper/orgs","repos_url":"https://api.github.com/users/cuviper/repos","events_url":"https://api.github.com/users/cuviper/events{/privacy}","received_events_url":"https://api.github.com/users/cuviper/received_events","type":"User","site_admin":false},"created_at":"2016-06-23T17:05:16Z","updated_at":"2016-06-23T17:05:16Z","body":"I can reproduce this all the way back to 1.0.0, at least."}}
{"actor":{"display_login":"cuviper"}}