Skip to content

Commit 382ed52

Browse files
Make PathBuf less Ok with adding UTF-16 then into_string
1 parent f72e4a9 commit 382ed52

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

std/src/sys_common/wtf8.rs

+3
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ impl Wtf8Buf {
477477
/// Part of a hack to make PathBuf::push/pop more efficient.
478478
#[inline]
479479
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
480+
// FIXME: this function should not even exist, as it implies violating Wtf8Buf invariants
481+
// For now, simply assume that is about to happen.
482+
self.is_known_utf8 = false;
480483
&mut self.bytes
481484
}
482485
}

std/tests/windows.rs

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

0 commit comments

Comments
 (0)