{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/27878","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/27878/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/27878/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/27878/events","html_url":"https://github.com/rust-lang/rust/issues/27878","id":101630136,"number":27878,"title":"Segfault in je_rallocx after pushing new value to Vec<u8> causing it to resize","user":{"login":"ashleysommer","id":402468,"avatar_url":"https://avatars.githubusercontent.com/u/402468?v=3","gravatar_id":"","url":"https://api.github.com/users/ashleysommer","html_url":"https://github.com/ashleysommer","followers_url":"https://api.github.com/users/ashleysommer/followers","following_url":"https://api.github.com/users/ashleysommer/following{/other_user}","gists_url":"https://api.github.com/users/ashleysommer/gists{/gist_id}","starred_url":"https://api.github.com/users/ashleysommer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashleysommer/subscriptions","organizations_url":"https://api.github.com/users/ashleysommer/orgs","repos_url":"https://api.github.com/users/ashleysommer/repos","events_url":"https://api.github.com/users/ashleysommer/events{/privacy}","received_events_url":"https://api.github.com/users/ashleysommer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-18T10:58:52Z","updated_at":"2015-08-18T12:56:57Z","closed_at":null,"body":"Im getting this very interesting segfault when using with CStrings in a project Im working on.\r\n\r\n```\r\nProgram received signal SIGSEGV, Segmentation fault.\r\n0x0000555555574551 in je_rallocx ()\r\nMissing separate debuginfos, use: dnf debuginfo-install fcgi-2.4.0-28.fc23.x86_64\r\n(gdb) bt\r\n#0  0x0000555555574551 in je_rallocx ()\r\n#1  0x000055555555dfc3 in fcgi::heap::reallocate (ptr=0x5555557af9fc \"REQUEST_METHOD\", old_size=14, size=28, align=1) at ../src/liballoc/heap.rs:71\r\n#2  0x000055555555de5f in fcgi::raw_vec::RawVec<T>::double (self=0x7fffffffd188) at ../src/liballoc/raw_vec.rs:202\r\n#3  0x000055555555dbf1 in fcgi::vec::Vec<T>::push (self=0x7fffffffd188, value=0 '\\000') at ../src/libcollections/vec.rs:577\r\n#4  0x000055555555d8dc in fcgi::DefaultRequest.Request::get_param (self=0x7fffffffd868, name=...) at /home/flubba86/workspace/rust-fcgi/src/lib.rs:153\r\n#5  0x0000555555559c60 in testcase:main () at src/main.rs:19\r\n#6  0x000055555556abc5 in rt::unwind::try::try_fn::h5614954417008942540 ()\r\n#7  0x0000555555568829 in __rust_try ()\r\n#8  0x000055555556a8b2 in rt::lang_start::ha172a3ce74bb453aK5w ()\r\n#9  0x000055555555d0f7 in main ()\r\n```\r\nI am passing a static `&str` with the contents \"REQUEST_METHOD\" to `get_param()` in the fcgi lib. The `get_param()` method converts that to a CString using `CString::new()`. To do that, the rust CString `new()` constructor method first converts the &str into a `Vec<u8>` using `into()`. It then does a `.push(0)` to append a null char, then finally returns a new CString using `into_boxed_slice` on the Vec.\r\n\r\nThe error occurs on the `.push(0)` line [here](https://github.com/rust-lang/rust/blob/master/src/libstd/ffi/c_str.rs#L199). The `Vec<u8>` is created with an initial size being the length of the string, and to add the new char it must call `double()` on the Vec to make it bigger. When it does that, the allocator segfaults during the jemalloc reallocation call.\r\n\r\nI isolated the issue further by first converting the `&str` to a `Vec<u8>` myself then doing `.push(0)` on it before passing it to the CString constructor. The push() still crashes when reallocating, as seen in the pasted trace above.\r\n\r\nI cannot however, create a minimal reproduction of the error. It only does it in this one library. I have tried multiple basic test cases and they all seem to work fine.\r\n\r\nI was using a Nightly from Saturday the 15th of August and I just updated to the Current nightly  (18th of August) and it is still the same.\r\n\r\nAny suggestions on how I can further debug this? Temporarily, is there any way of creating a CString from a &str without using an intermediate Vec<u8>?"},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/132197913","html_url":"https://github.com/rust-lang/rust/issues/27878#issuecomment-132197913","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/27878","id":132197913,"user":{"login":"bluss","id":3209739,"avatar_url":"https://avatars.githubusercontent.com/u/3209739?v=3","gravatar_id":"","url":"https://api.github.com/users/bluss","html_url":"https://github.com/bluss","followers_url":"https://api.github.com/users/bluss/followers","following_url":"https://api.github.com/users/bluss/following{/other_user}","gists_url":"https://api.github.com/users/bluss/gists{/gist_id}","starred_url":"https://api.github.com/users/bluss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluss/subscriptions","organizations_url":"https://api.github.com/users/bluss/orgs","repos_url":"https://api.github.com/users/bluss/repos","events_url":"https://api.github.com/users/bluss/events{/privacy}","received_events_url":"https://api.github.com/users/bluss/received_events","type":"User","site_admin":false},"created_at":"2015-08-18T12:56:57Z","updated_at":"2015-08-18T12:56:57Z","body":"Wow, this looks weird. `fcgi::heap::reallocate (ptr=0x5555557af9fc \"REQUEST_METHOD\",`  I think this is a hint.. somehow the Vec's internal pointer is a pointer to a string literal (?)."}}