We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 276bce1 commit 666b677Copy full SHA for 666b677
src/rust/bitbox02/src/util.rs
@@ -105,15 +105,9 @@ pub fn truncate_str(s: &str, len: usize) -> &str {
105
/// Converts a Rust string to a null terminated C string by appending a null
106
/// terminator. Returns `Err(())` if the input already contians a null byte.
107
pub fn str_to_cstr_vec(input: &str) -> Result<Vec<u8>, ()> {
108
- let bytes = input.as_bytes();
109
- if bytes.contains(&0) {
110
- Err(())
111
- } else {
112
- let mut out = Vec::with_capacity(input.len() + 1);
113
- out.extend_from_slice(bytes);
114
- out.push(0); // null terminator
115
- Ok(out)
116
- }
+ Ok(alloc::ffi::CString::new(input)
+ .or(Err(()))?
+ .into_bytes_with_nul())
117
}
118
119
#[cfg(test)]
0 commit comments