diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/util.rs | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/util.rs b/src/util.rs index b9b1a68..a0d0d1b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -76,9 +76,15 @@ pub fn get_last_error() -> Error { } pub fn generate_password(length: usize) -> Result<CString, Error> { - let mut data = vec![0u8; length]; - OsRng.fill_bytes(&mut data[..]); - get_cstring(data) + loop { + // Randomly generate a password until we get a string *without* null bytes. Otherwise + // the string would be cut off prematurely due to null-termination in C. + let mut data = vec![0u8; length]; + OsRng.fill_bytes(&mut data[..]); + if let Ok(s) = CString::new(data) { + return Ok(s); + } + } } pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, Error> { |