From 9ae1f8434fcd864a518a8cd939d53ada6ecf4a65 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 11 Jun 2024 22:55:55 -0700 Subject: [PATCH] Make PathBuf less Ok with adding UTF-16 then `into_string` --- library/std/src/sys/os_str/wtf8.rs | 3 +++ library/std/tests/windows.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 library/std/tests/windows.rs diff --git a/library/std/src/sys/os_str/wtf8.rs b/library/std/src/sys/os_str/wtf8.rs index dfff4dd4fb023..e8590fb1fe1e0 100644 --- a/library/std/src/sys/os_str/wtf8.rs +++ b/library/std/src/sys/os_str/wtf8.rs @@ -167,6 +167,9 @@ impl Buf { /// Part of a hack to make PathBuf::push/pop more efficient. #[inline] pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec { + // FIXME: this function should not even exist, as it implies violating Wtf8Buf invariants + // For now, simply assume that is about to happen. + self.inner.is_known_utf8 = false; self.inner.as_mut_vec_for_path_buf() } } diff --git a/library/std/tests/windows.rs b/library/std/tests/windows.rs new file mode 100644 index 0000000000000..0e70479434f0f --- /dev/null +++ b/library/std/tests/windows.rs @@ -0,0 +1,13 @@ +#![cfg(windows)] +//! An external tests + +use std::{ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf}; + +#[test] +fn os_string_must_know_it_isnt_utf8_issue_126291() -> Result { + let mut utf8 = PathBuf::from(OsString::from("utf8".to_owned())); + let non_utf8: OsString = + OsStringExt::from_wide(&[0x6e, 0x6f, 0x6e, 0xd800, 0x75, 0x74, 0x66, 0x38]); + utf8.set_extension(&non_utf8); + let result = utf8.into_os_string().into_string(); +}