Skip to content

Commit

Permalink
Make PathBuf less Ok with adding UTF-16 then into_string
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Jun 12, 2024
1 parent 76c7382 commit 263a7bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/std/src/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ impl Wtf8Buf {
/// 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<u8> {
// FIXME: this function should not even exist, as it implies violating Wtf8Buf invariants
// For now, simply assume that is about to happen.
self.is_known_utf8 = false;
&mut self.bytes
}
}
Expand Down
13 changes: 13 additions & 0 deletions library/std/tests/windows.rs
Original file line number Diff line number Diff line change
@@ -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<String, OsString> {
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();
}

0 comments on commit 263a7bf

Please sign in to comment.