Skip to content

Commit 170a8de

Browse files
committed
Use Align8 to avoid misalignment if the allocator or Vec doesn't align allocations
1 parent aa42c4d commit 170a8de

File tree

1 file changed

+7
-7
lines changed
  • library/std/src/sys/windows

1 file changed

+7
-7
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::marker::PhantomData;
22
use crate::mem::size_of;
33
use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle};
44
use crate::slice;
5-
use crate::sys::c;
5+
use crate::sys::{c, Align8};
66
use core;
77
use libc;
88

@@ -120,21 +120,21 @@ unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
120120
}
121121

122122
unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
123-
let size = size_of::<c::FILE_NAME_INFO>() + c::MAX_PATH * size_of::<c::WCHAR>();
124-
let mut name_info_bytes = vec![0u8; size];
123+
const SIZE: usize = size_of::<c::FILE_NAME_INFO>() + c::MAX_PATH * size_of::<c::WCHAR>();
124+
let mut name_info_bytes = Align8([0u8; SIZE]);
125125
let res = c::GetFileInformationByHandleEx(
126126
handle,
127127
c::FileNameInfo,
128-
name_info_bytes.as_mut_ptr() as *mut libc::c_void,
129-
size as u32,
128+
name_info_bytes.0.as_mut_ptr() as *mut libc::c_void,
129+
SIZE as u32,
130130
);
131131
if res == 0 {
132132
return false;
133133
}
134-
let name_info: &c::FILE_NAME_INFO = &*(name_info_bytes.as_ptr() as *const c::FILE_NAME_INFO);
134+
let name_info: &c::FILE_NAME_INFO = &*(name_info_bytes.0.as_ptr() as *const c::FILE_NAME_INFO);
135135
let name_len = name_info.FileNameLength as usize / 2;
136136
// Offset to get the `FileName` field.
137-
let name_ptr = name_info_bytes.as_ptr().offset(size_of::<c::DWORD>() as isize).cast::<u16>();
137+
let name_ptr = name_info_bytes.0.as_ptr().offset(size_of::<c::DWORD>() as isize).cast::<u16>();
138138
let s = core::slice::from_raw_parts(name_ptr, name_len);
139139
let name = String::from_utf16_lossy(s);
140140
// This checks whether 'pty' exists in the file name, which indicates that

0 commit comments

Comments
 (0)