Reduce false positives in msys2 detection
This checks that: * the handle is a pipe * the pipe's file name starts with "msys-" or "cygwin-" rather than looking in the full path.
This commit is contained in:
@@ -120,6 +120,11 @@ unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
|
||||
}
|
||||
|
||||
unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
|
||||
// Early return if the handle is not a pipe.
|
||||
if c::GetFileType(handle) != c::FILE_TYPE_PIPE {
|
||||
return false;
|
||||
}
|
||||
|
||||
const SIZE: usize = size_of::<c::FILE_NAME_INFO>() + c::MAX_PATH * size_of::<c::WCHAR>();
|
||||
let mut name_info_bytes = Align8([0u8; SIZE]);
|
||||
let res = c::GetFileInformationByHandleEx(
|
||||
@@ -137,11 +142,13 @@ unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
|
||||
let name_ptr = name_info_bytes.0.as_ptr().offset(size_of::<c::DWORD>() as isize).cast::<u16>();
|
||||
let s = core::slice::from_raw_parts(name_ptr, name_len);
|
||||
let name = String::from_utf16_lossy(s);
|
||||
// Get the file name only.
|
||||
let name = name.rsplit('\\').next().unwrap_or(&name);
|
||||
// This checks whether 'pty' exists in the file name, which indicates that
|
||||
// a pseudo-terminal is attached. To mitigate against false positives
|
||||
// (e.g., an actual file name that contains 'pty'), we also require that
|
||||
// either the strings 'msys-' or 'cygwin-' are in the file name as well.)
|
||||
let is_msys = name.contains("msys-") || name.contains("cygwin-");
|
||||
// the file name begins with either the strings 'msys-' or 'cygwin-'.)
|
||||
let is_msys = name.starts_with("msys-") || name.starts_with("cygwin-");
|
||||
let is_pty = name.contains("-pty");
|
||||
is_msys && is_pty
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user