| 189 | #[ignore] |
| 190 | #[test] |
| 191 | fn zeroize_c_string() { |
| 192 | let mut cstring = CString::new("Hello, world!").expect("CString::new failed"); |
| 193 | let orig_len = cstring.as_bytes().len(); |
| 194 | let orig_ptr = cstring.as_bytes().as_ptr(); |
| 195 | cstring.zeroize(); |
| 196 | // This doesn't quite test that the original memory has been cleared, but only that |
| 197 | // cstring now owns an empty buffer |
| 198 | assert!(cstring.as_bytes().is_empty()); |
| 199 | for i in 0..orig_len { |
| 200 | unsafe { |
| 201 | // Using a simple deref, only one iteration of the loop is performed |
| 202 | // presumably because after zeroize, the internal buffer has a length of one/ |
| 203 | // `read_volatile` seems to "fix" this |
| 204 | // Note that this is very likely UB |
| 205 | assert_eq!(orig_ptr.add(i).read_volatile(), 0); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #[cfg(feature = "alloc")] |
| 211 | #[test] |