@@ -43,7 +43,9 @@ use crate::sys;
4343/// `CString` implements a [`as_ptr`] method through the [`Deref`]
4444/// trait. This method will give you a `*const c_char` which you can
4545/// feed directly to extern functions that expect a nul-terminated
46- /// string, like C's `strdup()`.
46+ /// string, like C's `strdup()`. Notice that [`as_ptr`] returns a
47+ /// read-only pointer; if the C code writes to it, that causes
48+ /// undefined behavior.
4749///
4850/// # Extracting a slice of the whole C string
4951///
@@ -61,7 +63,7 @@ use crate::sys;
6163///
6264/// Once you have the kind of slice you need (with or without a nul
6365/// terminator), you can call the slice's own
64- /// [`as_ptr`][slice.as_ptr] method to get a raw pointer to pass to
66+ /// [`as_ptr`][slice.as_ptr] method to get a read-only raw pointer to pass to
6567/// extern functions. See the documentation for that function for a
6668/// discussion on ensuring the lifetime of the raw pointer.
6769///
@@ -1043,6 +1045,9 @@ impl CStr {
10431045 ///
10441046 /// **WARNING**
10451047 ///
1048+ /// The returned pointer is read-only; writing to it (including passing it
1049+ /// to C code that writes to it) causes undefined behavior.
1050+ ///
10461051 /// It is your responsibility to make sure that the underlying memory is not
10471052 /// freed too early. For example, the following code will cause undefined
10481053 /// behavior when `ptr` is used inside the `unsafe` block:
0 commit comments