| 1 | use core::fmt; |
| 2 | use std::{ |
| 3 | ffi::{CStr, OsString}, |
| 4 | ops, |
| 5 | }; |
| 6 | |
| 7 | use crate::common::Error; |
| 8 | |
| 9 | const NULL_BYTE: char = '\0'; |
| 10 | const NULL_BYTE_UTF8_LEN: usize = NULL_BYTE.len_utf8(); |
| 11 | |
| 12 | /// A UTF-8 encoded string with no interior null bytes |
| 13 | /// |
| 14 | /// This type can be converted into a C (null-terminated) string at no cost |
| 15 | #[derive(Clone, PartialEq, Eq)] |
| 16 | pub struct SudoString { |
| 17 | inner: String, |
| 18 | } |
| 19 | |
| 20 | impl SudoString { |
| 21 | pub fn new(mut string: String) -> Result<Self, Error> { |
| 22 | if string.as_bytes().contains(&0) { |
| 23 | return Err(Error::StringValidation(string)); |
| 24 | } |
| 25 | |
| 26 | string.push(NULL_BYTE); |
| 27 | |
| 28 | Ok(Self { inner: string }) |
| 29 | } |
| 30 | |
| 31 | pub fn from_cli_string(cli_string: impl Into<String>) -> Self { |
| 32 | Self::new(cli_string.into()) |
| 33 | .expect("strings that come in from CLI should not have interior null bytes") |
| 34 | } |
| 35 | |
| 36 | pub fn as_cstr(&self) -> &CStr { |
| 37 | CStr::from_bytes_with_nul(self.inner.as_bytes()).unwrap() |
| 38 | } |
| 39 | |
| 40 | pub fn as_str(&self) -> &str { |
| 41 | self |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl Default for SudoString { |
| 46 | fn default() -> Self { |
| 47 | Self { |
| 48 | inner: NULL_BYTE.into(), |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[cfg(test)] |
| 54 | impl From<&'_ str> for SudoString { |
| 55 | fn from(value: &'_ str) -> Self { |
| 56 | SudoString::try_from(value.to_string()).unwrap() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl TryFrom<String> for SudoString { |
| 61 | type Error = Error; |
| 62 | |
| 63 | fn try_from(value: String) -> Result<Self, Self::Error> { |
| 64 | Self::new(value) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl From<SudoString> for String { |
| 69 | fn from(value: SudoString) -> Self { |
| 70 | let mut s = value.inner; |
| 71 | s.pop(); |
| 72 | s |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl From<SudoString> for OsString { |
| 77 | fn from(value: SudoString) -> Self { |
| 78 | let mut s = value.inner; |
| 79 | s.pop(); |
| 80 | OsString::from(s) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl AsRef<str> for SudoString { |
| 85 | fn as_ref(&self) -> &str { |
| 86 | self.as_str() |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl ops::Deref for SudoString { |
| 91 | type Target = str; |
| 92 | |
| 93 | fn deref(&self) -> &Self::Target { |
| 94 | let num_bytes = self.inner.len(); |
| 95 | &self.inner[..num_bytes - NULL_BYTE_UTF8_LEN] |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl fmt::Debug for SudoString { |
| 100 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 101 | let s: &str = self; |
| 102 | fmt::Debug::fmt(s, f) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl fmt::Display for SudoString { |
| 107 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 108 | f.write_str(self) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl PartialEq<str> for SudoString { |
| 113 | fn eq(&self, other: &str) -> bool { |
| 114 | let s: &str = self; |
| 115 | s == other |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl PartialEq<&'_ str> for SudoString { |
| 120 | fn eq(&self, other: &&str) -> bool { |
| 121 | let s: &str = self; |
| 122 | s == *other |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #[cfg(test)] |
| 127 | mod tests { |
| 128 | use std::ffi::CString; |
| 129 | |
| 130 | use super::*; |
| 131 | |
| 132 | #[test] |
| 133 | fn null_byte_is_utf8_encoded_as_a_single_byte() { |
| 134 | assert_eq!(1, NULL_BYTE_UTF8_LEN) |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn sanity_check() { |
| 139 | let expected = "hello"; |
| 140 | let s = SudoString::new("hello".to_string()).unwrap(); |
| 141 | assert_eq!(expected, &*s); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn cstr_conversion() { |
| 146 | let expected = "hello"; |
| 147 | let cstr = CString::from_vec_with_nul((expected.to_string() + "\0").into_bytes()).unwrap(); |
| 148 | let s = SudoString::new(expected.to_string()).unwrap(); |
| 149 | assert_eq!(&*cstr, s.as_cstr()); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn rejects_string_that_contains_interior_null() { |
| 154 | assert!(SudoString::new("he\0llo".to_string()).is_err()); |
| 155 | } |
| 156 | } |