Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b453694

Browse files
Rollup merge of #103360 - ChrisDenton:isterm-filetype, r=thomcc
Reduce false positives in msys2 detection Currently msys2 will be detected by getting the file path and looking to see if it contains the substrings "msys-" and "-ptr" (or "cygwin-" and "-pty"). This risks false positives, especially with filesystem files and if `GetFileInformationByHandleEx` returns a [full path](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntqueryinformationfile#remarks). This PR adds a check to see if the handle is a pipe before doing the substring search. Additionally, for "msys2-" or "cygwin-" it only checks if the file name starts with the substring rather than looking at the whole path.
2 parents b22559f + d7b0bcb commit b453694

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

‎library/std/src/sys/windows/c.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ pub const FIONBIO: c_ulong = 0x8004667e;
129129

130130
pub const MAX_PATH: usize = 260;
131131

132+
pub const FILE_TYPE_PIPE: u32 = 3;
133+
132134
#[repr(C)]
133135
#[derive(Copy)]
134136
pub struct WIN32_FIND_DATAW {
@@ -1114,6 +1116,7 @@ extern "system" {
11141116
lpFileInformation: LPVOID,
11151117
dwBufferSize: DWORD,
11161118
) -> BOOL;
1119+
pub fn GetFileType(hfile: HANDLE) -> DWORD;
11171120
pub fn SleepConditionVariableSRW(
11181121
ConditionVariable: PCONDITION_VARIABLE,
11191122
SRWLock: PSRWLOCK,

‎library/std/src/sys/windows/io.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
120120
}
121121

122122
unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
123+
// Early return if the handle is not a pipe.
124+
if c::GetFileType(handle) != c::FILE_TYPE_PIPE {
125+
return false;
126+
}
127+
123128
const SIZE: usize = size_of::<c::FILE_NAME_INFO>() + c::MAX_PATH * size_of::<c::WCHAR>();
124129
let mut name_info_bytes = Align8([0u8; SIZE]);
125130
let res = c::GetFileInformationByHandleEx(
@@ -137,11 +142,13 @@ unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
137142
let name_ptr = name_info_bytes.0.as_ptr().offset(size_of::<c::DWORD>() as isize).cast::<u16>();
138143
let s = core::slice::from_raw_parts(name_ptr, name_len);
139144
let name = String::from_utf16_lossy(s);
145+
// Get the file name only.
146+
let name = name.rsplit('\\').next().unwrap_or(&name);
140147
// This checks whether 'pty' exists in the file name, which indicates that
141148
// a pseudo-terminal is attached. To mitigate against false positives
142149
// (e.g., an actual file name that contains 'pty'), we also require that
143-
// either the strings 'msys-' or 'cygwin-' are in the file name as well.)
144-
let is_msys = name.contains("msys-") || name.contains("cygwin-");
150+
// the file name begins with either the strings 'msys-' or 'cygwin-'.)
151+
let is_msys = name.starts_with("msys-") || name.starts_with("cygwin-");
145152
let is_pty = name.contains("-pty");
146153
is_msys && is_pty
147154
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /