From ed0983c4460f7297578e6d930fbdb85600a11454 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 29 Dec 2020 22:42:03 +0100 Subject: [PATCH] utils: Do not strip trailing nul from wide strings DXC (all the Windows string types using WCHAR under the hood, really) require wide strings to be terminated with a NUL character. If not, they'd use a BSTR that has a sneaky length prefix (#11), otherwise the length or ending of the string is unknown. This has not caused any trouble before because into_vec performs the exact same conversion as into_vec_with_nul, *except* that it pops off the last character before returning the Vec. Only the size of it is reduced, the underlying memory is not reallocated and the final NUL byte is left in place. However, in the event that a vector is shuffled around or modified we cannot permit ourselves to loose that trailing NUL. --- src/utils.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index a24f7f4..0ea8f07 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,7 +8,9 @@ use thiserror::Error; use winapi::um::oleauto::SysStringLen; pub(crate) fn to_wide(msg: &str) -> Vec { - widestring::WideCString::from_str(msg).unwrap().into_vec() + widestring::WideCString::from_str(msg) + .unwrap() + .into_vec_with_nul() } pub(crate) fn from_wide(wide: LPWSTR) -> String {