alloc/ffi/c_str.rs
1//! [`CString`] and its related types.
2
3use core::borrow::Borrow;
4use core::ffi::{CStr, c_char};
5use core::num::NonZero;
6use core::slice::memchr;
7use core::str::{self, FromStr, Utf8Error};
8use core::{fmt, mem, ops, ptr, slice};
9
10use crate::borrow::{Cow, ToOwned};
11use crate::boxed::Box;
12use crate::rc::Rc;
13use crate::slice::hack::into_vec;
14use crate::string::String;
15#[cfg(target_has_atomic = "ptr")]
16use crate::sync::Arc;
17use crate::vec::Vec;
18
19/// A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the
20/// middle.
21///
22/// This type serves the purpose of being able to safely generate a
23/// C-compatible string from a Rust byte slice or vector. An instance of this
24/// type is a static guarantee that the underlying bytes contain no interior 0
25/// bytes ("nul characters") and that the final byte is 0 ("nul terminator").
26///
27/// `CString` is to <code>&[CStr]</code> as [`String`] is to <code>&[str]</code>: the former
28/// in each pair are owned strings; the latter are borrowed
29/// references.
30///
31/// # Creating a `CString`
32///
33/// A `CString` is created from either a byte slice or a byte vector,
34/// or anything that implements <code>[Into]<[Vec]<[u8]>></code> (for
35/// example, you can build a `CString` straight out of a [`String`] or
36/// a <code>&[str]</code>, since both implement that trait).
37/// You can create a `CString` from a literal with `CString::from(c"Text")`.
38///
39/// The [`CString::new`] method will actually check that the provided <code>&[[u8]]</code>
40/// does not have 0 bytes in the middle, and return an error if it
41/// finds one.
42///
43/// # Extracting a raw pointer to the whole C string
44///
45/// `CString` implements an [`as_ptr`][`CStr::as_ptr`] method through the [`Deref`]
46/// trait. This method will give you a `*const c_char` which you can
47/// feed directly to extern functions that expect a nul-terminated
48/// string, like C's `strdup()`. Notice that [`as_ptr`][`CStr::as_ptr`] returns a
49/// read-only pointer; if the C code writes to it, that causes
50/// undefined behavior.
51///
52/// # Extracting a slice of the whole C string
53///
54/// Alternatively, you can obtain a <code>&[[u8]]</code> slice from a
55/// `CString` with the [`CString::as_bytes`] method. Slices produced in this
56/// way do *not* contain the trailing nul terminator. This is useful
57/// when you will be calling an extern function that takes a `*const
58/// u8` argument which is not necessarily nul-terminated, plus another
59/// argument with the length of the string — like C's `strndup()`.
60/// You can of course get the slice's length with its
61/// [`len`][slice::len] method.
62///
63/// If you need a <code>&[[u8]]</code> slice *with* the nul terminator, you
64/// can use [`CString::as_bytes_with_nul`] instead.
65///
66/// Once you have the kind of slice you need (with or without a nul
67/// terminator), you can call the slice's own
68/// [`as_ptr`][slice::as_ptr] method to get a read-only raw pointer to pass to
69/// extern functions. See the documentation for that function for a
70/// discussion on ensuring the lifetime of the raw pointer.
71///
72/// [str]: prim@str "str"
73/// [`Deref`]: ops::Deref
74///
75/// # Examples
76///
77/// ```ignore (extern-declaration)
78/// # fn main() {
79/// use std::ffi::CString;
80/// use std::os::raw::c_char;
81///
82/// extern "C" {
83/// fn my_printer(s: *const c_char);
84/// }
85///
86/// // We are certain that our string doesn't have 0 bytes in the middle,
87/// // so we can .expect()
88/// let c_to_print = CString::new("Hello, world!").expect("CString::new failed");
89/// unsafe {
90/// my_printer(c_to_print.as_ptr());
91/// }
92/// # }
93/// ```
94///
95/// # Safety
96///
97/// `CString` is intended for working with traditional C-style strings
98/// (a sequence of non-nul bytes terminated by a single nul byte); the
99/// primary use case for these kinds of strings is interoperating with C-like
100/// code. Often you will need to transfer ownership to/from that external
101/// code. It is strongly recommended that you thoroughly read through the
102/// documentation of `CString` before use, as improper ownership management
103/// of `CString` instances can lead to invalid memory accesses, memory leaks,
104/// and other memory errors.
105#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)]
106#[cfg_attr(not(test), rustc_diagnostic_item = "cstring_type")]
107#[stable(feature = "alloc_c_string", since = "1.64.0")]
108pub struct CString {
109 // Invariant 1: the slice ends with a zero byte and has a length of at least one.
110 // Invariant 2: the slice contains only one zero byte.
111 // Improper usage of unsafe function can break Invariant 2, but not Invariant 1.
112 inner: Box<[u8]>,
113}
114
115/// An error indicating that an interior nul byte was found.
116///
117/// While Rust strings may contain nul bytes in the middle, C strings
118/// can't, as that byte would effectively truncate the string.
119///
120/// This error is created by the [`new`][`CString::new`] method on
121/// [`CString`]. See its documentation for more.
122///
123/// # Examples
124///
125/// ```
126/// use std::ffi::{CString, NulError};
127///
128/// let _: NulError = CString::new(b"f\0oo".to_vec()).unwrap_err();
129/// ```
130#[derive(Clone, PartialEq, Eq, Debug)]
131#[stable(feature = "alloc_c_string", since = "1.64.0")]
132pub struct NulError(usize, Vec<u8>);
133
134#[derive(Clone, PartialEq, Eq, Debug)]
135enum FromBytesWithNulErrorKind {
136 InteriorNul(usize),
137 NotNulTerminated,
138}
139
140/// An error indicating that a nul byte was not in the expected position.
141///
142/// The vector used to create a [`CString`] must have one and only one nul byte,
143/// positioned at the end.
144///
145/// This error is created by the [`CString::from_vec_with_nul`] method.
146/// See its documentation for more.
147///
148/// # Examples
149///
150/// ```
151/// use std::ffi::{CString, FromVecWithNulError};
152///
153/// let _: FromVecWithNulError = CString::from_vec_with_nul(b"f\0oo".to_vec()).unwrap_err();
154/// ```
155#[derive(Clone, PartialEq, Eq, Debug)]
156#[stable(feature = "alloc_c_string", since = "1.64.0")]
157pub struct FromVecWithNulError {
158 error_kind: FromBytesWithNulErrorKind,
159 bytes: Vec<u8>,
160}
161
162#[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
163impl FromVecWithNulError {
164 /// Returns a slice of [`u8`]s bytes that were attempted to convert to a [`CString`].
165 ///
166 /// # Examples
167 ///
168 /// Basic usage:
169 ///
170 /// ```
171 /// use std::ffi::CString;
172 ///
173 /// // Some invalid bytes in a vector
174 /// let bytes = b"f\0oo".to_vec();
175 ///
176 /// let value = CString::from_vec_with_nul(bytes.clone());
177 ///
178 /// assert_eq!(&bytes[..], value.unwrap_err().as_bytes());
179 /// ```
180 #[must_use]
181 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
182 pub fn as_bytes(&self) -> &[u8] {
183 &self.bytes[..]
184 }
185
186 /// Returns the bytes that were attempted to convert to a [`CString`].
187 ///
188 /// This method is carefully constructed to avoid allocation. It will
189 /// consume the error, moving out the bytes, so that a copy of the bytes
190 /// does not need to be made.
191 ///
192 /// # Examples
193 ///
194 /// Basic usage:
195 ///
196 /// ```
197 /// use std::ffi::CString;
198 ///
199 /// // Some invalid bytes in a vector
200 /// let bytes = b"f\0oo".to_vec();
201 ///
202 /// let value = CString::from_vec_with_nul(bytes.clone());
203 ///
204 /// assert_eq!(bytes, value.unwrap_err().into_bytes());
205 /// ```
206 #[must_use = "`self` will be dropped if the result is not used"]
207 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
208 pub fn into_bytes(self) -> Vec<u8> {
209 self.bytes
210 }
211}
212
213/// An error indicating invalid UTF-8 when converting a [`CString`] into a [`String`].
214///
215/// `CString` is just a wrapper over a buffer of bytes with a nul terminator;
216/// [`CString::into_string`] performs UTF-8 validation on those bytes and may
217/// return this error.
218///
219/// This `struct` is created by [`CString::into_string()`]. See
220/// its documentation for more.
221#[derive(Clone, PartialEq, Eq, Debug)]
222#[stable(feature = "alloc_c_string", since = "1.64.0")]
223pub struct IntoStringError {
224 inner: CString,
225 error: Utf8Error,
226}
227
228impl CString {
229 /// Creates a new C-compatible string from a container of bytes.
230 ///
231 /// This function will consume the provided data and use the
232 /// underlying bytes to construct a new string, ensuring that
233 /// there is a trailing 0 byte. This trailing 0 byte will be
234 /// appended by this function; the provided data should *not*
235 /// contain any 0 bytes in it.
236 ///
237 /// # Examples
238 ///
239 /// ```ignore (extern-declaration)
240 /// use std::ffi::CString;
241 /// use std::os::raw::c_char;
242 ///
243 /// extern "C" { fn puts(s: *const c_char); }
244 ///
245 /// let to_print = CString::new("Hello!").expect("CString::new failed");
246 /// unsafe {
247 /// puts(to_print.as_ptr());
248 /// }
249 /// ```
250 ///
251 /// # Errors
252 ///
253 /// This function will return an error if the supplied bytes contain an
254 /// internal 0 byte. The [`NulError`] returned will contain the bytes as well as
255 /// the position of the nul byte.
256 #[stable(feature = "rust1", since = "1.0.0")]
257 pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
258 trait SpecNewImpl {
259 fn spec_new_impl(self) -> Result<CString, NulError>;
260 }
261
262 impl<T: Into<Vec<u8>>> SpecNewImpl for T {
263 default fn spec_new_impl(self) -> Result<CString, NulError> {
264 let bytes: Vec<u8> = self.into();
265 match memchr::memchr(0, &bytes) {
266 Some(i) => Err(NulError(i, bytes)),
267 None => Ok(unsafe { CString::_from_vec_unchecked(bytes) }),
268 }
269 }
270 }
271
272 // Specialization for avoiding reallocation
273 #[inline(always)] // Without that it is not inlined into specializations
274 fn spec_new_impl_bytes(bytes: &[u8]) -> Result<CString, NulError> {
275 // We cannot have such large slice that we would overflow here
276 // but using `checked_add` allows LLVM to assume that capacity never overflows
277 // and generate twice shorter code.
278 // `saturating_add` doesn't help for some reason.
279 let capacity = bytes.len().checked_add(1).unwrap();
280
281 // Allocate before validation to avoid duplication of allocation code.
282 // We still need to allocate and copy memory even if we get an error.
283 let mut buffer = Vec::with_capacity(capacity);
284 buffer.extend(bytes);
285
286 // Check memory of self instead of new buffer.
287 // This allows better optimizations if lto enabled.
288 match memchr::memchr(0, bytes) {
289 Some(i) => Err(NulError(i, buffer)),
290 None => Ok(unsafe { CString::_from_vec_unchecked(buffer) }),
291 }
292 }
293
294 impl SpecNewImpl for &'_ [u8] {
295 fn spec_new_impl(self) -> Result<CString, NulError> {
296 spec_new_impl_bytes(self)
297 }
298 }
299
300 impl SpecNewImpl for &'_ str {
301 fn spec_new_impl(self) -> Result<CString, NulError> {
302 spec_new_impl_bytes(self.as_bytes())
303 }
304 }
305
306 impl SpecNewImpl for &'_ mut [u8] {
307 fn spec_new_impl(self) -> Result<CString, NulError> {
308 spec_new_impl_bytes(self)
309 }
310 }
311
312 t.spec_new_impl()
313 }
314
315 /// Creates a C-compatible string by consuming a byte vector,
316 /// without checking for interior 0 bytes.
317 ///
318 /// Trailing 0 byte will be appended by this function.
319 ///
320 /// This method is equivalent to [`CString::new`] except that no runtime
321 /// assertion is made that `v` contains no 0 bytes, and it requires an
322 /// actual byte vector, not anything that can be converted to one with Into.
323 ///
324 /// # Examples
325 ///
326 /// ```
327 /// use std::ffi::CString;
328 ///
329 /// let raw = b"foo".to_vec();
330 /// unsafe {
331 /// let c_string = CString::from_vec_unchecked(raw);
332 /// }
333 /// ```
334 #[must_use]
335 #[stable(feature = "rust1", since = "1.0.0")]
336 pub unsafe fn from_vec_unchecked(v: Vec<u8>) -> Self {
337 debug_assert!(memchr::memchr(0, &v).is_none());
338 unsafe { Self::_from_vec_unchecked(v) }
339 }
340
341 unsafe fn _from_vec_unchecked(mut v: Vec<u8>) -> Self {
342 v.reserve_exact(1);
343 v.push(0);
344 Self { inner: v.into_boxed_slice() }
345 }
346
347 /// Retakes ownership of a `CString` that was transferred to C via
348 /// [`CString::into_raw`].
349 ///
350 /// Additionally, the length of the string will be recalculated from the pointer.
351 ///
352 /// # Safety
353 ///
354 /// This should only ever be called with a pointer that was earlier
355 /// obtained by calling [`CString::into_raw`]. Other usage (e.g., trying to take
356 /// ownership of a string that was allocated by foreign code) is likely to lead
357 /// to undefined behavior or allocator corruption.
358 ///
359 /// It should be noted that the length isn't just "recomputed," but that
360 /// the recomputed length must match the original length from the
361 /// [`CString::into_raw`] call. This means the [`CString::into_raw`]/`from_raw`
362 /// methods should not be used when passing the string to C functions that can
363 /// modify the string's length.
364 ///
365 /// > **Note:** If you need to borrow a string that was allocated by
366 /// > foreign code, use [`CStr`]. If you need to take ownership of
367 /// > a string that was allocated by foreign code, you will need to
368 /// > make your own provisions for freeing it appropriately, likely
369 /// > with the foreign code's API to do that.
370 ///
371 /// # Examples
372 ///
373 /// Creates a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
374 /// ownership with `from_raw`:
375 ///
376 /// ```ignore (extern-declaration)
377 /// use std::ffi::CString;
378 /// use std::os::raw::c_char;
379 ///
380 /// extern "C" {
381 /// fn some_extern_function(s: *mut c_char);
382 /// }
383 ///
384 /// let c_string = CString::from(c"Hello!");
385 /// let raw = c_string.into_raw();
386 /// unsafe {
387 /// some_extern_function(raw);
388 /// let c_string = CString::from_raw(raw);
389 /// }
390 /// ```
391 #[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
392 #[stable(feature = "cstr_memory", since = "1.4.0")]
393 pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
394 // SAFETY: This is called with a pointer that was obtained from a call
395 // to `CString::into_raw` and the length has not been modified. As such,
396 // we know there is a NUL byte (and only one) at the end and that the
397 // information about the size of the allocation is correct on Rust's
398 // side.
399 unsafe {
400 unsafe extern "C" {
401 /// Provided by libc or compiler_builtins.
402 fn strlen(s: *const c_char) -> usize;
403 }
404 let len = strlen(ptr) + 1; // Including the NUL byte
405 let slice = slice::from_raw_parts_mut(ptr, len);
406 CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
407 }
408 }
409
410 /// Consumes the `CString` and transfers ownership of the string to a C caller.
411 ///
412 /// The pointer which this function returns must be returned to Rust and reconstituted using
413 /// [`CString::from_raw`] to be properly deallocated. Specifically, one
414 /// should *not* use the standard C `free()` function to deallocate
415 /// this string.
416 ///
417 /// Failure to call [`CString::from_raw`] will lead to a memory leak.
418 ///
419 /// The C side must **not** modify the length of the string (by writing a
420 /// nul byte somewhere inside the string or removing the final one) before
421 /// it makes it back into Rust using [`CString::from_raw`]. See the safety section
422 /// in [`CString::from_raw`].
423 ///
424 /// # Examples
425 ///
426 /// ```
427 /// use std::ffi::CString;
428 ///
429 /// let c_string = CString::from(c"foo");
430 ///
431 /// let ptr = c_string.into_raw();
432 ///
433 /// unsafe {
434 /// assert_eq!(b'f', *ptr as u8);
435 /// assert_eq!(b'o', *ptr.add(1) as u8);
436 /// assert_eq!(b'o', *ptr.add(2) as u8);
437 /// assert_eq!(b'\0', *ptr.add(3) as u8);
438 ///
439 /// // retake pointer to free memory
440 /// let _ = CString::from_raw(ptr);
441 /// }
442 /// ```
443 #[inline]
444 #[must_use = "`self` will be dropped if the result is not used"]
445 #[stable(feature = "cstr_memory", since = "1.4.0")]
446 pub fn into_raw(self) -> *mut c_char {
447 Box::into_raw(self.into_inner()) as *mut c_char
448 }
449
450 /// Converts the `CString` into a [`String`] if it contains valid UTF-8 data.
451 ///
452 /// On failure, ownership of the original `CString` is returned.
453 ///
454 /// # Examples
455 ///
456 /// ```
457 /// use std::ffi::CString;
458 ///
459 /// let valid_utf8 = vec![b'f', b'o', b'o'];
460 /// let cstring = CString::new(valid_utf8).expect("CString::new failed");
461 /// assert_eq!(cstring.into_string().expect("into_string() call failed"), "foo");
462 ///
463 /// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
464 /// let cstring = CString::new(invalid_utf8).expect("CString::new failed");
465 /// let err = cstring.into_string().err().expect("into_string().err() failed");
466 /// assert_eq!(err.utf8_error().valid_up_to(), 1);
467 /// ```
468 #[stable(feature = "cstring_into", since = "1.7.0")]
469 pub fn into_string(self) -> Result<String, IntoStringError> {
470 String::from_utf8(self.into_bytes()).map_err(|e| IntoStringError {
471 error: e.utf8_error(),
472 inner: unsafe { Self::_from_vec_unchecked(e.into_bytes()) },
473 })
474 }
475
476 /// Consumes the `CString` and returns the underlying byte buffer.
477 ///
478 /// The returned buffer does **not** contain the trailing nul
479 /// terminator, and it is guaranteed to not have any interior nul
480 /// bytes.
481 ///
482 /// # Examples
483 ///
484 /// ```
485 /// use std::ffi::CString;
486 ///
487 /// let c_string = CString::from(c"foo");
488 /// let bytes = c_string.into_bytes();
489 /// assert_eq!(bytes, vec![b'f', b'o', b'o']);
490 /// ```
491 #[must_use = "`self` will be dropped if the result is not used"]
492 #[stable(feature = "cstring_into", since = "1.7.0")]
493 pub fn into_bytes(self) -> Vec<u8> {
494 let mut vec = into_vec(self.into_inner());
495 let _nul = vec.pop();
496 debug_assert_eq!(_nul, Some(0u8));
497 vec
498 }
499
500 /// Equivalent to [`CString::into_bytes()`] except that the
501 /// returned vector includes the trailing nul terminator.
502 ///
503 /// # Examples
504 ///
505 /// ```
506 /// use std::ffi::CString;
507 ///
508 /// let c_string = CString::from(c"foo");
509 /// let bytes = c_string.into_bytes_with_nul();
510 /// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
511 /// ```
512 #[must_use = "`self` will be dropped if the result is not used"]
513 #[stable(feature = "cstring_into", since = "1.7.0")]
514 pub fn into_bytes_with_nul(self) -> Vec<u8> {
515 into_vec(self.into_inner())
516 }
517
518 /// Returns the contents of this `CString` as a slice of bytes.
519 ///
520 /// The returned slice does **not** contain the trailing nul
521 /// terminator, and it is guaranteed to not have any interior nul
522 /// bytes. If you need the nul terminator, use
523 /// [`CString::as_bytes_with_nul`] instead.
524 ///
525 /// # Examples
526 ///
527 /// ```
528 /// use std::ffi::CString;
529 ///
530 /// let c_string = CString::from(c"foo");
531 /// let bytes = c_string.as_bytes();
532 /// assert_eq!(bytes, &[b'f', b'o', b'o']);
533 /// ```
534 #[inline]
535 #[must_use]
536 #[stable(feature = "rust1", since = "1.0.0")]
537 pub fn as_bytes(&self) -> &[u8] {
538 // SAFETY: CString has a length at least 1
539 unsafe { self.inner.get_unchecked(..self.inner.len() - 1) }
540 }
541
542 /// Equivalent to [`CString::as_bytes()`] except that the
543 /// returned slice includes the trailing nul terminator.
544 ///
545 /// # Examples
546 ///
547 /// ```
548 /// use std::ffi::CString;
549 ///
550 /// let c_string = CString::from(c"foo");
551 /// let bytes = c_string.as_bytes_with_nul();
552 /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
553 /// ```
554 #[inline]
555 #[must_use]
556 #[stable(feature = "rust1", since = "1.0.0")]
557 pub fn as_bytes_with_nul(&self) -> &[u8] {
558 &self.inner
559 }
560
561 /// Extracts a [`CStr`] slice containing the entire string.
562 ///
563 /// # Examples
564 ///
565 /// ```
566 /// use std::ffi::{CString, CStr};
567 ///
568 /// let c_string = CString::from(c"foo");
569 /// let cstr = c_string.as_c_str();
570 /// assert_eq!(cstr,
571 /// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
572 /// ```
573 #[inline]
574 #[must_use]
575 #[stable(feature = "as_c_str", since = "1.20.0")]
576 #[cfg_attr(not(test), rustc_diagnostic_item = "cstring_as_c_str")]
577 pub fn as_c_str(&self) -> &CStr {
578 &*self
579 }
580
581 /// Converts this `CString` into a boxed [`CStr`].
582 ///
583 /// # Examples
584 ///
585 /// ```
586 /// let c_string = c"foo".to_owned();
587 /// let boxed = c_string.into_boxed_c_str();
588 /// assert_eq!(boxed.to_bytes_with_nul(), b"foo\0");
589 /// ```
590 #[must_use = "`self` will be dropped if the result is not used"]
591 #[stable(feature = "into_boxed_c_str", since = "1.20.0")]
592 pub fn into_boxed_c_str(self) -> Box<CStr> {
593 unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
594 }
595
596 /// Bypass "move out of struct which implements [`Drop`] trait" restriction.
597 #[inline]
598 fn into_inner(self) -> Box<[u8]> {
599 // Rationale: `mem::forget(self)` invalidates the previous call to `ptr::read(&self.inner)`
600 // so we use `ManuallyDrop` to ensure `self` is not dropped.
601 // Then we can return the box directly without invalidating it.
602 // See https://github.com/rust-lang/rust/issues/62553.
603 let this = mem::ManuallyDrop::new(self);
604 unsafe { ptr::read(&this.inner) }
605 }
606
607 /// Converts a <code>[Vec]<[u8]></code> to a [`CString`] without checking the
608 /// invariants on the given [`Vec`].
609 ///
610 /// # Safety
611 ///
612 /// The given [`Vec`] **must** have one nul byte as its last element.
613 /// This means it cannot be empty nor have any other nul byte anywhere else.
614 ///
615 /// # Example
616 ///
617 /// ```
618 /// use std::ffi::CString;
619 /// assert_eq!(
620 /// unsafe { CString::from_vec_with_nul_unchecked(b"abc\0".to_vec()) },
621 /// unsafe { CString::from_vec_unchecked(b"abc".to_vec()) }
622 /// );
623 /// ```
624 #[must_use]
625 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
626 pub unsafe fn from_vec_with_nul_unchecked(v: Vec<u8>) -> Self {
627 debug_assert!(memchr::memchr(0, &v).unwrap() + 1 == v.len());
628 unsafe { Self::_from_vec_with_nul_unchecked(v) }
629 }
630
631 unsafe fn _from_vec_with_nul_unchecked(v: Vec<u8>) -> Self {
632 Self { inner: v.into_boxed_slice() }
633 }
634
635 /// Attempts to converts a <code>[Vec]<[u8]></code> to a [`CString`].
636 ///
637 /// Runtime checks are present to ensure there is only one nul byte in the
638 /// [`Vec`], its last element.
639 ///
640 /// # Errors
641 ///
642 /// If a nul byte is present and not the last element or no nul bytes
643 /// is present, an error will be returned.
644 ///
645 /// # Examples
646 ///
647 /// A successful conversion will produce the same result as [`CString::new`]
648 /// when called without the ending nul byte.
649 ///
650 /// ```
651 /// use std::ffi::CString;
652 /// assert_eq!(
653 /// CString::from_vec_with_nul(b"abc\0".to_vec())
654 /// .expect("CString::from_vec_with_nul failed"),
655 /// c"abc".to_owned()
656 /// );
657 /// ```
658 ///
659 /// An incorrectly formatted [`Vec`] will produce an error.
660 ///
661 /// ```
662 /// use std::ffi::{CString, FromVecWithNulError};
663 /// // Interior nul byte
664 /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"a\0bc".to_vec()).unwrap_err();
665 /// // No nul byte
666 /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"abc".to_vec()).unwrap_err();
667 /// ```
668 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
669 pub fn from_vec_with_nul(v: Vec<u8>) -> Result<Self, FromVecWithNulError> {
670 let nul_pos = memchr::memchr(0, &v);
671 match nul_pos {
672 Some(nul_pos) if nul_pos + 1 == v.len() => {
673 // SAFETY: We know there is only one nul byte, at the end
674 // of the vec.
675 Ok(unsafe { Self::_from_vec_with_nul_unchecked(v) })
676 }
677 Some(nul_pos) => Err(FromVecWithNulError {
678 error_kind: FromBytesWithNulErrorKind::InteriorNul(nul_pos),
679 bytes: v,
680 }),
681 None => Err(FromVecWithNulError {
682 error_kind: FromBytesWithNulErrorKind::NotNulTerminated,
683 bytes: v,
684 }),
685 }
686 }
687}
688
689// Turns this `CString` into an empty string to prevent
690// memory-unsafe code from working by accident. Inline
691// to prevent LLVM from optimizing it away in debug builds.
692#[stable(feature = "cstring_drop", since = "1.13.0")]
693#[rustc_insignificant_dtor]
694impl Drop for CString {
695 #[inline]
696 fn drop(&mut self) {
697 unsafe {
698 *self.inner.get_unchecked_mut(0) = 0;
699 }
700 }
701}
702
703#[stable(feature = "rust1", since = "1.0.0")]
704impl ops::Deref for CString {
705 type Target = CStr;
706
707 #[inline]
708 fn deref(&self) -> &CStr {
709 unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
710 }
711}
712
713#[stable(feature = "rust1", since = "1.0.0")]
714impl fmt::Debug for CString {
715 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
716 fmt::Debug::fmt(&**self, f)
717 }
718}
719
720#[stable(feature = "cstring_into", since = "1.7.0")]
721impl From<CString> for Vec<u8> {
722 /// Converts a [`CString`] into a <code>[Vec]<[u8]></code>.
723 ///
724 /// The conversion consumes the [`CString`], and removes the terminating NUL byte.
725 #[inline]
726 fn from(s: CString) -> Vec<u8> {
727 s.into_bytes()
728 }
729}
730
731#[stable(feature = "cstr_default", since = "1.10.0")]
732impl Default for CString {
733 /// Creates an empty `CString`.
734 fn default() -> CString {
735 let a: &CStr = Default::default();
736 a.to_owned()
737 }
738}
739
740#[stable(feature = "cstr_borrow", since = "1.3.0")]
741impl Borrow<CStr> for CString {
742 #[inline]
743 fn borrow(&self) -> &CStr {
744 self
745 }
746}
747
748#[stable(feature = "cstring_from_cow_cstr", since = "1.28.0")]
749impl<'a> From<Cow<'a, CStr>> for CString {
750 /// Converts a `Cow<'a, CStr>` into a `CString`, by copying the contents if they are
751 /// borrowed.
752 #[inline]
753 fn from(s: Cow<'a, CStr>) -> Self {
754 s.into_owned()
755 }
756}
757
758#[cfg(not(test))]
759#[stable(feature = "box_from_c_str", since = "1.17.0")]
760impl From<&CStr> for Box<CStr> {
761 /// Converts a `&CStr` into a `Box<CStr>`,
762 /// by copying the contents into a newly allocated [`Box`].
763 fn from(s: &CStr) -> Box<CStr> {
764 let boxed: Box<[u8]> = Box::from(s.to_bytes_with_nul());
765 unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
766 }
767}
768
769#[cfg(not(test))]
770#[stable(feature = "box_from_mut_slice", since = "1.84.0")]
771impl From<&mut CStr> for Box<CStr> {
772 /// Converts a `&mut CStr` into a `Box<CStr>`,
773 /// by copying the contents into a newly allocated [`Box`].
774 fn from(s: &mut CStr) -> Box<CStr> {
775 Self::from(&*s)
776 }
777}
778
779#[stable(feature = "box_from_cow", since = "1.45.0")]
780impl From<Cow<'_, CStr>> for Box<CStr> {
781 /// Converts a `Cow<'a, CStr>` into a `Box<CStr>`,
782 /// by copying the contents if they are borrowed.
783 #[inline]
784 fn from(cow: Cow<'_, CStr>) -> Box<CStr> {
785 match cow {
786 Cow::Borrowed(s) => Box::from(s),
787 Cow::Owned(s) => Box::from(s),
788 }
789 }
790}
791
792#[stable(feature = "c_string_from_box", since = "1.18.0")]
793impl From<Box<CStr>> for CString {
794 /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating.
795 #[inline]
796 fn from(s: Box<CStr>) -> CString {
797 let raw = Box::into_raw(s) as *mut [u8];
798 CString { inner: unsafe { Box::from_raw(raw) } }
799 }
800}
801
802#[stable(feature = "cstring_from_vec_of_nonzerou8", since = "1.43.0")]
803impl From<Vec<NonZero<u8>>> for CString {
804 /// Converts a <code>[Vec]<[NonZero]<[u8]>></code> into a [`CString`] without
805 /// copying nor checking for inner nul bytes.
806 #[inline]
807 fn from(v: Vec<NonZero<u8>>) -> CString {
808 unsafe {
809 // Transmute `Vec<NonZero<u8>>` to `Vec<u8>`.
810 let v: Vec<u8> = {
811 // SAFETY:
812 // - transmuting between `NonZero<u8>` and `u8` is sound;
813 // - `alloc::Layout<NonZero<u8>> == alloc::Layout<u8>`.
814 let (ptr, len, cap): (*mut NonZero<u8>, _, _) = Vec::into_raw_parts(v);
815 Vec::from_raw_parts(ptr.cast::<u8>(), len, cap)
816 };
817 // SAFETY: `v` cannot contain nul bytes, given the type-level
818 // invariant of `NonZero<u8>`.
819 Self::_from_vec_unchecked(v)
820 }
821 }
822}
823
824impl FromStr for CString {
825 type Err = NulError;
826
827 /// Converts a string `s` into a [`CString`].
828 ///
829 /// This method is equivalent to [`CString::new`].
830 #[inline]
831 fn from_str(s: &str) -> Result<Self, Self::Err> {
832 Self::new(s)
833 }
834}
835
836impl TryFrom<CString> for String {
837 type Error = IntoStringError;
838
839 /// Converts a [`CString`] into a [`String`] if it contains valid UTF-8 data.
840 ///
841 /// This method is equivalent to [`CString::into_string`].
842 #[inline]
843 fn try_from(value: CString) -> Result<Self, Self::Error> {
844 value.into_string()
845 }
846}
847
848#[cfg(not(test))]
849#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
850impl Clone for Box<CStr> {
851 #[inline]
852 fn clone(&self) -> Self {
853 (**self).into()
854 }
855}
856
857#[stable(feature = "box_from_c_string", since = "1.20.0")]
858impl From<CString> for Box<CStr> {
859 /// Converts a [`CString`] into a <code>[Box]<[CStr]></code> without copying or allocating.
860 #[inline]
861 fn from(s: CString) -> Box<CStr> {
862 s.into_boxed_c_str()
863 }
864}
865
866#[stable(feature = "cow_from_cstr", since = "1.28.0")]
867impl<'a> From<CString> for Cow<'a, CStr> {
868 /// Converts a [`CString`] into an owned [`Cow`] without copying or allocating.
869 #[inline]
870 fn from(s: CString) -> Cow<'a, CStr> {
871 Cow::Owned(s)
872 }
873}
874
875#[stable(feature = "cow_from_cstr", since = "1.28.0")]
876impl<'a> From<&'a CStr> for Cow<'a, CStr> {
877 /// Converts a [`CStr`] into a borrowed [`Cow`] without copying or allocating.
878 #[inline]
879 fn from(s: &'a CStr) -> Cow<'a, CStr> {
880 Cow::Borrowed(s)
881 }
882}
883
884#[stable(feature = "cow_from_cstr", since = "1.28.0")]
885impl<'a> From<&'a CString> for Cow<'a, CStr> {
886 /// Converts a `&`[`CString`] into a borrowed [`Cow`] without copying or allocating.
887 #[inline]
888 fn from(s: &'a CString) -> Cow<'a, CStr> {
889 Cow::Borrowed(s.as_c_str())
890 }
891}
892
893#[cfg(target_has_atomic = "ptr")]
894#[stable(feature = "shared_from_slice2", since = "1.24.0")]
895impl From<CString> for Arc<CStr> {
896 /// Converts a [`CString`] into an <code>[Arc]<[CStr]></code> by moving the [`CString`]
897 /// data into a new [`Arc`] buffer.
898 #[inline]
899 fn from(s: CString) -> Arc<CStr> {
900 let arc: Arc<[u8]> = Arc::from(s.into_inner());
901 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const CStr) }
902 }
903}
904
905#[cfg(target_has_atomic = "ptr")]
906#[stable(feature = "shared_from_slice2", since = "1.24.0")]
907impl From<&CStr> for Arc<CStr> {
908 /// Converts a `&CStr` into a `Arc<CStr>`,
909 /// by copying the contents into a newly allocated [`Arc`].
910 #[inline]
911 fn from(s: &CStr) -> Arc<CStr> {
912 let arc: Arc<[u8]> = Arc::from(s.to_bytes_with_nul());
913 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const CStr) }
914 }
915}
916
917#[cfg(target_has_atomic = "ptr")]
918#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
919impl From<&mut CStr> for Arc<CStr> {
920 /// Converts a `&mut CStr` into a `Arc<CStr>`,
921 /// by copying the contents into a newly allocated [`Arc`].
922 #[inline]
923 fn from(s: &mut CStr) -> Arc<CStr> {
924 Arc::from(&*s)
925 }
926}
927
928#[stable(feature = "shared_from_slice2", since = "1.24.0")]
929impl From<CString> for Rc<CStr> {
930 /// Converts a [`CString`] into an <code>[Rc]<[CStr]></code> by moving the [`CString`]
931 /// data into a new [`Rc`] buffer.
932 #[inline]
933 fn from(s: CString) -> Rc<CStr> {
934 let rc: Rc<[u8]> = Rc::from(s.into_inner());
935 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
936 }
937}
938
939#[stable(feature = "shared_from_slice2", since = "1.24.0")]
940impl From<&CStr> for Rc<CStr> {
941 /// Converts a `&CStr` into a `Rc<CStr>`,
942 /// by copying the contents into a newly allocated [`Rc`].
943 #[inline]
944 fn from(s: &CStr) -> Rc<CStr> {
945 let rc: Rc<[u8]> = Rc::from(s.to_bytes_with_nul());
946 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
947 }
948}
949
950#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
951impl From<&mut CStr> for Rc<CStr> {
952 /// Converts a `&mut CStr` into a `Rc<CStr>`,
953 /// by copying the contents into a newly allocated [`Rc`].
954 #[inline]
955 fn from(s: &mut CStr) -> Rc<CStr> {
956 Rc::from(&*s)
957 }
958}
959
960#[cfg(not(no_global_oom_handling))]
961#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
962impl Default for Rc<CStr> {
963 /// Creates an empty CStr inside an Rc
964 ///
965 /// This may or may not share an allocation with other Rcs on the same thread.
966 #[inline]
967 fn default() -> Self {
968 let rc = Rc::<[u8]>::from(*b"\0");
969 // `[u8]` has the same layout as `CStr`, and it is `NUL` terminated.
970 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
971 }
972}
973
974#[cfg(not(test))]
975#[stable(feature = "default_box_extra", since = "1.17.0")]
976impl Default for Box<CStr> {
977 fn default() -> Box<CStr> {
978 let boxed: Box<[u8]> = Box::from([0]);
979 unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
980 }
981}
982
983impl NulError {
984 /// Returns the position of the nul byte in the slice that caused
985 /// [`CString::new`] to fail.
986 ///
987 /// # Examples
988 ///
989 /// ```
990 /// use std::ffi::CString;
991 ///
992 /// let nul_error = CString::new("foo\0bar").unwrap_err();
993 /// assert_eq!(nul_error.nul_position(), 3);
994 ///
995 /// let nul_error = CString::new("foo bar\0").unwrap_err();
996 /// assert_eq!(nul_error.nul_position(), 7);
997 /// ```
998 #[must_use]
999 #[stable(feature = "rust1", since = "1.0.0")]
1000 pub fn nul_position(&self) -> usize {
1001 self.0
1002 }
1003
1004 /// Consumes this error, returning the underlying vector of bytes which
1005 /// generated the error in the first place.
1006 ///
1007 /// # Examples
1008 ///
1009 /// ```
1010 /// use std::ffi::CString;
1011 ///
1012 /// let nul_error = CString::new("foo\0bar").unwrap_err();
1013 /// assert_eq!(nul_error.into_vec(), b"foo\0bar");
1014 /// ```
1015 #[must_use = "`self` will be dropped if the result is not used"]
1016 #[stable(feature = "rust1", since = "1.0.0")]
1017 pub fn into_vec(self) -> Vec<u8> {
1018 self.1
1019 }
1020}
1021
1022#[stable(feature = "rust1", since = "1.0.0")]
1023impl fmt::Display for NulError {
1024 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1025 write!(f, "nul byte found in provided data at position: {}", self.0)
1026 }
1027}
1028
1029#[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
1030impl fmt::Display for FromVecWithNulError {
1031 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1032 match self.error_kind {
1033 FromBytesWithNulErrorKind::InteriorNul(pos) => {
1034 write!(f, "data provided contains an interior nul byte at pos {pos}")
1035 }
1036 FromBytesWithNulErrorKind::NotNulTerminated => {
1037 write!(f, "data provided is not nul terminated")
1038 }
1039 }
1040 }
1041}
1042
1043impl IntoStringError {
1044 /// Consumes this error, returning original [`CString`] which generated the
1045 /// error.
1046 #[must_use = "`self` will be dropped if the result is not used"]
1047 #[stable(feature = "cstring_into", since = "1.7.0")]
1048 pub fn into_cstring(self) -> CString {
1049 self.inner
1050 }
1051
1052 /// Access the underlying UTF-8 error that was the cause of this error.
1053 #[must_use]
1054 #[stable(feature = "cstring_into", since = "1.7.0")]
1055 pub fn utf8_error(&self) -> Utf8Error {
1056 self.error
1057 }
1058}
1059
1060impl IntoStringError {
1061 fn description(&self) -> &str {
1062 "C string contained non-utf8 bytes"
1063 }
1064}
1065
1066#[stable(feature = "cstring_into", since = "1.7.0")]
1067impl fmt::Display for IntoStringError {
1068 #[allow(deprecated, deprecated_in_future)]
1069 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1070 self.description().fmt(f)
1071 }
1072}
1073
1074#[stable(feature = "cstr_borrow", since = "1.3.0")]
1075impl ToOwned for CStr {
1076 type Owned = CString;
1077
1078 fn to_owned(&self) -> CString {
1079 CString { inner: self.to_bytes_with_nul().into() }
1080 }
1081
1082 fn clone_into(&self, target: &mut CString) {
1083 let mut b = into_vec(mem::take(&mut target.inner));
1084 self.to_bytes_with_nul().clone_into(&mut b);
1085 target.inner = b.into_boxed_slice();
1086 }
1087}
1088
1089#[stable(feature = "cstring_asref", since = "1.7.0")]
1090impl From<&CStr> for CString {
1091 /// Converts a <code>&[CStr]</code> into a [`CString`]
1092 /// by copying the contents into a new allocation.
1093 fn from(s: &CStr) -> CString {
1094 s.to_owned()
1095 }
1096}
1097
1098#[stable(feature = "cstring_asref", since = "1.7.0")]
1099impl ops::Index<ops::RangeFull> for CString {
1100 type Output = CStr;
1101
1102 #[inline]
1103 fn index(&self, _index: ops::RangeFull) -> &CStr {
1104 self
1105 }
1106}
1107
1108#[stable(feature = "cstring_asref", since = "1.7.0")]
1109impl AsRef<CStr> for CString {
1110 #[inline]
1111 fn as_ref(&self) -> &CStr {
1112 self
1113 }
1114}
1115
1116#[cfg(not(test))]
1117impl CStr {
1118 /// Converts a `CStr` into a <code>[Cow]<[str]></code>.
1119 ///
1120 /// If the contents of the `CStr` are valid UTF-8 data, this
1121 /// function will return a <code>[Cow]::[Borrowed]\(&[str])</code>
1122 /// with the corresponding <code>&[str]</code> slice. Otherwise, it will
1123 /// replace any invalid UTF-8 sequences with
1124 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD] and return a
1125 /// <code>[Cow]::[Owned]\(&[str])</code> with the result.
1126 ///
1127 /// [str]: prim@str "str"
1128 /// [Borrowed]: Cow::Borrowed
1129 /// [Owned]: Cow::Owned
1130 /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER "std::char::REPLACEMENT_CHARACTER"
1131 ///
1132 /// # Examples
1133 ///
1134 /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8. The leading
1135 /// `c` on the string literal denotes a `CStr`.
1136 ///
1137 /// ```
1138 /// use std::borrow::Cow;
1139 ///
1140 /// assert_eq!(c"Hello World".to_string_lossy(), Cow::Borrowed("Hello World"));
1141 /// ```
1142 ///
1143 /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
1144 ///
1145 /// ```
1146 /// use std::borrow::Cow;
1147 ///
1148 /// assert_eq!(
1149 /// c"Hello \xF0\x90\x80World".to_string_lossy(),
1150 /// Cow::Owned(String::from("Hello �World")) as Cow<'_, str>
1151 /// );
1152 /// ```
1153 #[rustc_allow_incoherent_impl]
1154 #[must_use = "this returns the result of the operation, \
1155 without modifying the original"]
1156 #[stable(feature = "cstr_to_str", since = "1.4.0")]
1157 pub fn to_string_lossy(&self) -> Cow<'_, str> {
1158 String::from_utf8_lossy(self.to_bytes())
1159 }
1160
1161 /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating.
1162 ///
1163 /// # Examples
1164 ///
1165 /// ```
1166 /// use std::ffi::{CStr, CString};
1167 ///
1168 /// let boxed: Box<CStr> = Box::from(c"foo");
1169 /// let c_string: CString = c"foo".to_owned();
1170 ///
1171 /// assert_eq!(boxed.into_c_string(), c_string);
1172 /// ```
1173 #[rustc_allow_incoherent_impl]
1174 #[must_use = "`self` will be dropped if the result is not used"]
1175 #[stable(feature = "into_boxed_c_str", since = "1.20.0")]
1176 pub fn into_c_string(self: Box<Self>) -> CString {
1177 CString::from(self)
1178 }
1179}
1180
1181#[stable(feature = "rust1", since = "1.0.0")]
1182impl core::error::Error for NulError {
1183 #[allow(deprecated)]
1184 fn description(&self) -> &str {
1185 "nul byte found in data"
1186 }
1187}
1188
1189#[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
1190impl core::error::Error for FromVecWithNulError {}
1191
1192#[stable(feature = "cstring_into", since = "1.7.0")]
1193impl core::error::Error for IntoStringError {
1194 #[allow(deprecated)]
1195 fn description(&self) -> &str {
1196 "C string contained non-utf8 bytes"
1197 }
1198
1199 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
1200 Some(&self.error)
1201 }
1202}