{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/comments/31334880","id":31334880,"diff_hunk":"@@ -200,6 +200,38 @@ impl CString {\n         CString { inner: v }\n     }\n \n+    /// Retakes ownership of a CString that was transferred to C.\n+    ///\n+    /// The only appropriate argument is a pointer obtained by calling\n+    /// `into_raw`.\n+    #[unstable(feature = \"c_str_memory\", reason = \"recently added\")]\n+    pub unsafe fn from_raw(ptr: *const libc::c_char) -> CString {\n+        let len = libc::strlen(ptr);\n+        let len_with_nul = len as usize + 1;\n+        let vec = Vec::from_raw_parts(ptr as *mut u8, len_with_nul, len_with_nul);\n+        CString::from_vec_unchecked(vec)\n+    }\n+\n+    /// Transfers ownership of the string to a C caller.\n+    ///\n+    /// The pointer must be returned to Rust and reconstituted using\n+    /// `from_raw` to be properly deallocated. Specifically, one\n+    /// should *not* use the standard C `free` function to deallocate\n+    /// this string.\n+    ///\n+    /// Failure to call `from_raw` will lead to a memory leak.\n+    #[unstable(feature = \"c_str_memory\", reason = \"recently added\")]\n+    pub fn into_raw(self) -> *const libc::c_char {\n+        // Resize the vector to fit - we need the capacity to be\n+        // determinable from the string length, and shrinking to fit\n+        // is the only way to be sure.\n+        let mut vec = self.inner;\n+        vec.shrink_to_fit();","path":"src/libstd/ffi/c_str.rs","position":30,"original_position":30,"commit_id":"12151bc4d48dafe6b71c235cb94ef4b4f5c057b6","original_commit_id":"12151bc4d48dafe6b71c235cb94ef4b4f5c057b6","user":{"login":"cristicbz","id":801328,"avatar_url":"https://avatars.githubusercontent.com/u/801328?v=3","gravatar_id":"","url":"https://api.github.com/users/cristicbz","html_url":"https://github.com/cristicbz","followers_url":"https://api.github.com/users/cristicbz/followers","following_url":"https://api.github.com/users/cristicbz/following{/other_user}","gists_url":"https://api.github.com/users/cristicbz/gists{/gist_id}","starred_url":"https://api.github.com/users/cristicbz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cristicbz/subscriptions","organizations_url":"https://api.github.com/users/cristicbz/orgs","repos_url":"https://api.github.com/users/cristicbz/repos","events_url":"https://api.github.com/users/cristicbz/events{/privacy}","received_events_url":"https://api.github.com/users/cristicbz/received_events","type":"User","site_admin":false},"body":"Just curious: would it not be possible to support\r\n```rust\r\nimpl<T> Vec<T> {\r\n    unsafe fn drop_from_ptr_len(ptr: *mut T, len: usize)\r\n            where T: Copy {      // Or !Drop, I guess.\r\n        Vec::from_raw_parts(ptr, len, len);\r\n    }\r\n}\r\n```\r\nIs there any realistic implementation of Vec for which that would be UB? I've stumbled upon a similar problem with FFI code where I could only pass a ```void*``` in a callback and didn't want to do an allocation (this way I could cast len as ```void*```).","created_at":"2015-05-29T15:27:37Z","updated_at":"2015-05-29T15:27:37Z","html_url":"https://github.com/rust-lang/rust/pull/25777#discussion_r31334880","pull_request_url":"https://api.github.com/repos/rust-lang/rust/pulls/25777","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/comments/31334880"},"html":{"href":"https://github.com/rust-lang/rust/pull/25777#discussion_r31334880"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/25777"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/25777","id":36149176,"html_url":"https://github.com/rust-lang/rust/pull/25777","diff_url":"https://github.com/rust-lang/rust/pull/25777.diff","patch_url":"https://github.com/rust-lang/rust/pull/25777.patch","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/25777","number":25777,"state":"open","locked":false,"title":"Add a way to return allocated strings to C and then free them later","user":{"login":"shepmaster","id":174509,"avatar_url":"https://avatars.githubusercontent.com/u/174509?v=3","gravatar_id":"","url":"https://api.github.com/users/shepmaster","html_url":"https://github.com/shepmaster","followers_url":"https://api.github.com/users/shepmaster/followers","following_url":"https://api.github.com/users/shepmaster/following{/other_user}","gists_url":"https://api.github.com/users/shepmaster/gists{/gist_id}","starred_url":"https://api.github.com/users/shepmaster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shepmaster/subscriptions","organizations_url":"https://api.github.com/users/shepmaster/orgs","repos_url":"https://api.github.com/users/shepmaster/repos","events_url":"https://api.github.com/users/shepmaster/events{/privacy}","received_events_url":"https://api.github.com/users/shepmaster/received_events","type":"User","site_admin":false},"body":"As far as I was able to determine, it's currently *impossible* to allocate a C NUL-terminated string in Rust and then return it to C (transferring ownership), without leaking memory. There is support for passing the string to C (borrowing).\r\n\r\nTo complicate matters, it's not possible for the C code to just call `free` on the allocated string, due to the different allocators in use.\r\n\r\n`CString` has no way to recreate itself from a pointer. This commit adds one. This is complicated a bit because Rust `Vec`s want the pointer, size, and capacity.\r\n\r\nTo deal with that, another method to shrink and \"leak\" the `CString` to a `char *` is also provided. \r\n\r\nWe can then use `strlen` to determine the length of the string, which must match the capacity. \r\n\r\n**TODO**\r\n\r\n- [x] Improve documentation\r\n- [x] Add stability markers","created_at":"2015-05-25T16:48:03Z","updated_at":"2015-05-29T15:27:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e1a1edae6e4302d5a4f5245148656b656eff9554","assignee":{"login":"huonw","id":1203825,"avatar_url":"https://avatars.githubusercontent.com/u/1203825?v=3","gravatar_id":"","url":"https://api.github.com/users/huonw","html_url":"https://github.com/huonw","followers_url":"https://api.github.com/users/huonw/followers","following_url":"https://api.github.com/users/huonw/following{/other_user}","gists_url":"https://api.github.com/users/huonw/gists{/gist_id}","starred_url":"https://api.github.com/users/huonw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/huonw/subscriptions","organizations_url":"https://api.github.com/users/huonw/orgs","repos_url":"https://api.github.com/users/huonw/repos","events_url":"https://api.github.com/users/huonw/events{/privacy}","received_events_url":"https://api.github.com/users/huonw/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/rust/pulls/25777/commits","review_comments_url":"https://api.github.com/repos/rust-lang/rust/pulls/25777/comments","review_comment_url":"https://api.github.com/repos/rust-lang/rust/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/25777/comments","statuses_url":"https://api.github.com/repos/rust-lang/rust/statuses/12151bc4d48dafe6b71c235cb94ef4b4f5c057b6","head":{"label":"shepmaster:cstring-return-to-c","ref":"cstring-return-to-c","sha":"12151bc4d48dafe6b71c235cb94ef4b4f5c057b6","user":{"login":"shepmaster","id":174509,"avatar_url":"https://avatars.githubusercontent.com/u/174509?v=3","gravatar_id":"","url":"https://api.github.com/users/shepmaster","html_url":"https://github.com/shepmaster","followers_url":"https://api.github.com/users/shepmaster/followers","following_url":"https://api.github.com/users/shepmaster/following{/other_user}","gists_url":"https://api.github.com/users/shepmaster/gists{/gist_id}","starred_url":"https://api.github.com/users/shepmaster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shepmaster/subscriptions","organizations_url":"https://api.github.com/users/shepmaster/orgs","repos_url":"https://api.github.com/users/shepmaster/repos","events_url":"https://api.github.com/users/shepmaster/events{/privacy}","received_events_url":"https://api.github.com/users/shepmaster/received_events","type":"User","site_admin":false},"repo":{"id":23265650,"name":"rust","full_name":"shepmaster/rust","owner":{"login":"shepmaster","id":174509,"avatar_url":"https://avatars.githubusercontent.com/u/174509?v=3","gravatar_id":"","url":"https://api.github.com/users/shepmaster","html_url":"https://github.com/shepmaster","followers_url":"https://api.github.com/users/shepmaster/followers","following_url":"https://api.github.com/users/shepmaster/following{/other_user}","gists_url":"https://api.github.com/users/shepmaster/gists{/gist_id}","starred_url":"https://api.github.com/users/shepmaster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shepmaster/subscriptions","organizations_url":"https://api.github.com/users/shepmaster/orgs","repos_url":"https://api.github.com/users/shepmaster/repos","events_url":"https://api.github.com/users/shepmaster/events{/privacy}","received_events_url":"https://api.github.com/users/shepmaster/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shepmaster/rust","description":"a safe, concurrent, practical language","fork":true,"url":"https://api.github.com/repos/shepmaster/rust","forks_url":"https://api.github.com/repos/shepmaster/rust/forks","keys_url":"https://api.github.com/repos/shepmaster/rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shepmaster/rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shepmaster/rust/teams","hooks_url":"https://api.github.com/repos/shepmaster/rust/hooks","issue_events_url":"https://api.github.com/repos/shepmaster/rust/issues/events{/number}","events_url":"https://api.github.com/repos/shepmaster/rust/events","assignees_url":"https://api.github.com/repos/shepmaster/rust/assignees{/user}","branches_url":"https://api.github.com/repos/shepmaster/rust/branches{/branch}","tags_url":"https://api.github.com/repos/shepmaster/rust/tags","blobs_url":"https://api.github.com/repos/shepmaster/rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shepmaster/rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shepmaster/rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/shepmaster/rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shepmaster/rust/statuses/{sha}","languages_url":"https://api.github.com/repos/shepmaster/rust/languages","stargazers_url":"https://api.github.com/repos/shepmaster/rust/stargazers","contributors_url":"https://api.github.com/repos/shepmaster/rust/contributors","subscribers_url":"https://api.github.com/repos/shepmaster/rust/subscribers","subscription_url":"https://api.github.com/repos/shepmaster/rust/subscription","commits_url":"https://api.github.com/repos/shepmaster/rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/shepmaster/rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/shepmaster/rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/shepmaster/rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/shepmaster/rust/contents/{+path}","compare_url":"https://api.github.com/repos/shepmaster/rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shepmaster/rust/merges","archive_url":"https://api.github.com/repos/shepmaster/rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shepmaster/rust/downloads","issues_url":"https://api.github.com/repos/shepmaster/rust/issues{/number}","pulls_url":"https://api.github.com/repos/shepmaster/rust/pulls{/number}","milestones_url":"https://api.github.com/repos/shepmaster/rust/milestones{/number}","notifications_url":"https://api.github.com/repos/shepmaster/rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shepmaster/rust/labels{/name}","releases_url":"https://api.github.com/repos/shepmaster/rust/releases{/id}","created_at":"2014-08-23T20:30:53Z","updated_at":"2015-05-08T03:03:30Z","pushed_at":"2015-05-28T16:09:51Z","git_url":"git://github.com/shepmaster/rust.git","ssh_url":"git@github.com:shepmaster/rust.git","clone_url":"https://github.com/shepmaster/rust.git","svn_url":"https://github.com/shepmaster/rust","homepage":"http://www.rust-lang.org","size":292050,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"1742a01f8d5717f1ab2f445c4d4656633b4c88b9","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":724712,"name":"rust","full_name":"rust-lang/rust","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/rust","description":"A safe, concurrent, practical language.","fork":false,"url":"https://api.github.com/repos/rust-lang/rust","forks_url":"https://api.github.com/repos/rust-lang/rust/forks","keys_url":"https://api.github.com/repos/rust-lang/rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/rust/teams","hooks_url":"https://api.github.com/repos/rust-lang/rust/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/rust/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/rust/events","assignees_url":"https://api.github.com/repos/rust-lang/rust/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/rust/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/rust/tags","blobs_url":"https://api.github.com/repos/rust-lang/rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/rust/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/rust/languages","stargazers_url":"https://api.github.com/repos/rust-lang/rust/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/rust/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/rust/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/rust/subscription","commits_url":"https://api.github.com/repos/rust-lang/rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/rust/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/rust/merges","archive_url":"https://api.github.com/repos/rust-lang/rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/rust/downloads","issues_url":"https://api.github.com/repos/rust-lang/rust/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/rust/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/rust/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/rust/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/rust/releases{/id}","created_at":"2010-06-16T20:39:03Z","updated_at":"2015-05-29T15:24:54Z","pushed_at":"2015-05-29T14:54:00Z","git_url":"git://github.com/rust-lang/rust.git","ssh_url":"git@github.com:rust-lang/rust.git","clone_url":"https://github.com/rust-lang/rust.git","svn_url":"https://github.com/rust-lang/rust","homepage":"http://www.rust-lang.org","size":667347,"stargazers_count":11872,"watchers_count":11872,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2275,"mirror_url":null,"open_issues_count":2069,"forks":2275,"open_issues":2069,"watchers":11872,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/25777"},"html":{"href":"https://github.com/rust-lang/rust/pull/25777"},"issue":{"href":"https://api.github.com/repos/rust-lang/rust/issues/25777"},"comments":{"href":"https://api.github.com/repos/rust-lang/rust/issues/25777/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/25777/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/25777/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/rust/statuses/12151bc4d48dafe6b71c235cb94ef4b4f5c057b6"}}}}