Skip to content

Commit 9ae1f84

Browse files
Make PathBuf less Ok with adding UTF-16 then into_string
1 parent 76c7382 commit 9ae1f84

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

library/std/src/sys/os_str/wtf8.rs

+3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ impl Buf {
167167
/// Part of a hack to make PathBuf::push/pop more efficient.
168168
#[inline]
169169
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
170+
// FIXME: this function should not even exist, as it implies violating Wtf8Buf invariants
171+
// For now, simply assume that is about to happen.
172+
self.inner.is_known_utf8 = false;
170173
self.inner.as_mut_vec_for_path_buf()
171174
}
172175
}

library/std/tests/windows.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![cfg(windows)]
2+
//! An external tests
3+
4+
use std::{ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf};
5+
6+
#[test]
7+
fn os_string_must_know_it_isnt_utf8_issue_126291() -> Result<String, OsString> {
8+
let mut utf8 = PathBuf::from(OsString::from("utf8".to_owned()));
9+
let non_utf8: OsString =
10+
OsStringExt::from_wide(&[0x6e, 0x6f, 0x6e, 0xd800, 0x75, 0x74, 0x66, 0x38]);
11+
utf8.set_extension(&non_utf8);
12+
let result = utf8.into_os_string().into_string();
13+
}

0 commit comments

Comments
 (0)