Skip to content

Commit 64f87d2

Browse files
authored
Unrolled build for #145150
Rollup merge of #145150 - ChrisDenton:inherit, r=Mark-Simulacrum Replace unsafe `security_attributes` function with safe `inherit_handle` alternative The `security_attributes` function is marked as safe despite taking a raw pointer which will later be used. Fortunately this function is only used internally and only in one place that has been basically the same for a decade now. However, we only ever set one bool so it's easy enough to replace with something that's actually safe. In the future we might want to expose the ability for users to set security attributes. But that should be properly designed (and safe!).
2 parents 29737cb + 89b3669 commit 64f87d2

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub struct OpenOptions {
8080
attributes: u32,
8181
share_mode: u32,
8282
security_qos_flags: u32,
83-
security_attributes: *mut c::SECURITY_ATTRIBUTES,
83+
inherit_handle: bool,
8484
}
8585

8686
#[derive(Clone, PartialEq, Eq, Debug)]
@@ -203,7 +203,7 @@ impl OpenOptions {
203203
share_mode: c::FILE_SHARE_READ | c::FILE_SHARE_WRITE | c::FILE_SHARE_DELETE,
204204
attributes: 0,
205205
security_qos_flags: 0,
206-
security_attributes: ptr::null_mut(),
206+
inherit_handle: false,
207207
}
208208
}
209209

@@ -243,8 +243,8 @@ impl OpenOptions {
243243
// receive is `SECURITY_ANONYMOUS = 0x0`, which we can't check for later on.
244244
self.security_qos_flags = flags | c::SECURITY_SQOS_PRESENT;
245245
}
246-
pub fn security_attributes(&mut self, attrs: *mut c::SECURITY_ATTRIBUTES) {
247-
self.security_attributes = attrs;
246+
pub fn inherit_handle(&mut self, inherit: bool) {
247+
self.inherit_handle = inherit;
248248
}
249249

250250
fn get_access_mode(&self) -> io::Result<u32> {
@@ -307,12 +307,17 @@ impl File {
307307

308308
fn open_native(path: &WCStr, opts: &OpenOptions) -> io::Result<File> {
309309
let creation = opts.get_creation_mode()?;
310+
let sa = c::SECURITY_ATTRIBUTES {
311+
nLength: size_of::<c::SECURITY_ATTRIBUTES>() as u32,
312+
lpSecurityDescriptor: ptr::null_mut(),
313+
bInheritHandle: opts.inherit_handle as c::BOOL,
314+
};
310315
let handle = unsafe {
311316
c::CreateFileW(
312317
path.as_ptr(),
313318
opts.get_access_mode()?,
314319
opts.share_mode,
315-
opts.security_attributes,
320+
if opts.inherit_handle { &sa } else { ptr::null() },
316321
creation,
317322
opts.get_flags_and_attributes(),
318323
ptr::null_mut(),

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,16 +623,10 @@ impl Stdio {
623623
// permissions as well as the ability to be inherited to child
624624
// processes (as this is about to be inherited).
625625
Stdio::Null => {
626-
let size = size_of::<c::SECURITY_ATTRIBUTES>();
627-
let mut sa = c::SECURITY_ATTRIBUTES {
628-
nLength: size as u32,
629-
lpSecurityDescriptor: ptr::null_mut(),
630-
bInheritHandle: 1,
631-
};
632626
let mut opts = OpenOptions::new();
633627
opts.read(stdio_id == c::STD_INPUT_HANDLE);
634628
opts.write(stdio_id != c::STD_INPUT_HANDLE);
635-
opts.security_attributes(&mut sa);
629+
opts.inherit_handle(true);
636630
File::open(Path::new(r"\\.\NUL"), &opts).map(|file| file.into_inner())
637631
}
638632
}

0 commit comments

Comments
 (0)