From d2cceb78db6311711abd0770275b8fa9588d477e Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 24 Aug 2022 15:32:16 +0200 Subject: [PATCH] Rewrite FILE_NAME_INFO handling to avoid enlarging slice reference Rather than referencing a slice's pointer and then creating a new slice with a longer length, offset from the base structure pointer instead. This makes some choices of Rust semantics happier. --- library/std/src/sys/windows/io.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/std/src/sys/windows/io.rs b/library/std/src/sys/windows/io.rs index 0ec619315633e..7e7518b6d8905 100644 --- a/library/std/src/sys/windows/io.rs +++ b/library/std/src/sys/windows/io.rs @@ -132,10 +132,10 @@ unsafe fn msys_tty_on(handle: c::HANDLE) -> bool { return false; } let name_info: &c::FILE_NAME_INFO = &*(name_info_bytes.as_ptr() as *const c::FILE_NAME_INFO); - let s = core::slice::from_raw_parts( - name_info.FileName.as_ptr(), - name_info.FileNameLength as usize / 2, - ); + let name_len = name_info.FileNameLength as usize / 2; + // Offset to get the `FileName` field. + let name_ptr = name_info_bytes.as_ptr().offset(size_of::() as isize).cast::(); + let s = core::slice::from_raw_parts(name_ptr, name_len); let name = String::from_utf16_lossy(s); // This checks whether 'pty' exists in the file name, which indicates that // a pseudo-terminal is attached. To mitigate against false positives