diff options
| author | bors <bors@rust-lang.org> | 2016-01-28 09:03:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-01-28 09:03:00 +0000 |
| commit | a891c72976824993ecb4a8749d9f16dbeceaeeed (patch) | |
| tree | 4b2bda4f1a1de2aff5aa5ff4dcdbb37c7bab968c /src/libstd | |
| parent | 035f4cca00d20fcf889c8c588125cd07b632a476 (diff) | |
| parent | 9cfa1916fa06fa5fa809d6d7807a6f978e35c67d (diff) | |
| download | rust-a891c72976824993ecb4a8749d9f16dbeceaeeed.tar.gz rust-a891c72976824993ecb4a8749d9f16dbeceaeeed.zip | |
Auto merge of #31171 - dirk:dirk/safety-section-in-cstring-docs, r=steveklabnik
Also a minor language tweak to the documentation of the `ffi::CString::from_raw` function.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 9d505607a60..ee744b868dd 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -60,6 +60,18 @@ use vec::Vec; /// } /// # } /// ``` +/// +/// # Safety +/// +/// `CString` is intended for working with traditional C-style strings +/// (a sequence of non-null bytes terminated by a single null byte); the +/// primary use case for these kinds of strings is interoperating with C-like +/// code. Often you will need to transfer ownership to/from that external +/// code. It is strongly recommended that you thoroughly read through the +/// documentation of `CString` before use, as improper ownership management +/// of `CString` instances can lead to invalid memory accesses, memory leaks, +/// and other memory errors. + #[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)] #[stable(feature = "rust1", since = "1.0.0")] pub struct CString { @@ -207,11 +219,11 @@ impl CString { CString { inner: v.into_boxed_slice() } } - /// Retakes ownership of a CString that was transferred to C. + /// Retakes ownership of a `CString` that was transferred to C. /// - /// The only appropriate argument is a pointer obtained by calling - /// `into_raw`. The length of the string will be recalculated - /// using the pointer. + /// This should only ever be called with a pointer that was earlier + /// obtained by calling `into_raw` on a `CString`. Additionally, the length + /// of the string will be recalculated from the pointer. #[stable(feature = "cstr_memory", since = "1.4.0")] pub unsafe fn from_raw(ptr: *mut c_char) -> CString { let len = libc::strlen(ptr) + 1; // Including the NUL byte |
