diff options
| author | Ralf Jung <post@ralfj.de> | 2019-05-01 17:59:48 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2019-05-01 17:59:48 +0200 |
| commit | 1e47250540bc97f921d1b7e2982f1904fa191dff (patch) | |
| tree | 30c0e7841d8190f846c568f6bf44ca83ae75f70e /src/libstd | |
| parent | 6cc24f26036b28fb3366de86efe3da6c4464057a (diff) | |
| download | rust-1e47250540bc97f921d1b7e2982f1904fa191dff.tar.gz rust-1e47250540bc97f921d1b7e2982f1904fa191dff.zip | |
as_ptr returns a read-only pointer
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index f93583dff81..5c6c43017cf 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -43,7 +43,9 @@ use crate::sys; /// `CString` implements a [`as_ptr`] method through the [`Deref`] /// trait. This method will give you a `*const c_char` which you can /// feed directly to extern functions that expect a nul-terminated -/// string, like C's `strdup()`. +/// string, like C's `strdup()`. Notice that [`as_ptr`] returns a +/// read-only pointer; if the C code writes to it, that causes +/// undefined behavior. /// /// # Extracting a slice of the whole C string /// @@ -61,7 +63,7 @@ use crate::sys; /// /// Once you have the kind of slice you need (with or without a nul /// terminator), you can call the slice's own -/// [`as_ptr`][slice.as_ptr] method to get a raw pointer to pass to +/// [`as_ptr`][slice.as_ptr] method to get a read-only raw pointer to pass to /// extern functions. See the documentation for that function for a /// discussion on ensuring the lifetime of the raw pointer. /// @@ -1043,6 +1045,9 @@ impl CStr { /// /// **WARNING** /// + /// The returned pointer is read-only; writing to it (including passing it + /// to C code that writes to it) causes undefined behavior. + /// /// It is your responsibility to make sure that the underlying memory is not /// freed too early. For example, the following code will cause undefined /// behavior when `ptr` is used inside the `unsafe` block: |
