Skip to content

Commit 79df5f4

Browse files
authored
Fix unsafe_op_in_unsafe_fn for Windows (#16058)
Windows builds are generating a bunch of warnings about unsafe_op_in_unsafe_fn. This fixes those warnings by making sure the relevant parts are wrapped in unsafe.
2 parents 5c8f865 + 3fc3cf2 commit 79df5f4

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

crates/cargo-util/src/read2.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,21 @@ mod imp {
141141

142142
impl<'a> Pipe<'a> {
143143
unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> {
144+
// SAFETY: Handle must be owned, open, and closeable with CloseHandle.
145+
let pipe = unsafe { NamedPipe::from_raw_handle(p.into_raw_handle()) };
144146
Pipe {
145147
dst,
146-
pipe: NamedPipe::from_raw_handle(p.into_raw_handle()),
148+
pipe,
147149
overlapped: Overlapped::zero(),
148150
done: false,
149151
}
150152
}
151153

152154
unsafe fn read(&mut self) -> io::Result<()> {
153-
let dst = slice_to_end(self.dst);
154-
match self.pipe.read_overlapped(dst, self.overlapped.raw()) {
155+
let dst = unsafe { slice_to_end(self.dst) };
156+
// SAFETY: The buffer must be valid until the end of the I/O,
157+
// which is handled in `read2`.
158+
match unsafe { self.pipe.read_overlapped(dst, self.overlapped.raw()) } {
155159
Ok(_) => Ok(()),
156160
Err(e) => {
157161
if e.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) {
@@ -166,7 +170,7 @@ mod imp {
166170

167171
unsafe fn complete(&mut self, status: &CompletionStatus) {
168172
let prev = self.dst.len();
169-
self.dst.set_len(prev + status.bytes_transferred() as usize);
173+
unsafe { self.dst.set_len(prev + status.bytes_transferred() as usize) };
170174
if status.bytes_transferred() == 0 {
171175
self.done = true;
172176
}
@@ -180,6 +184,6 @@ mod imp {
180184
if v.capacity() == v.len() {
181185
v.reserve(1);
182186
}
183-
slice::from_raw_parts_mut(v.as_mut_ptr().add(v.len()), v.capacity() - v.len())
187+
unsafe { slice::from_raw_parts_mut(v.as_mut_ptr().add(v.len()), v.capacity() - v.len()) }
184188
}
185189
}

src/cargo/util/job.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ mod imp {
8787
// use job objects, so we instead just ignore errors and assume that
8888
// we're otherwise part of someone else's job object in this case.
8989

90-
let job = CreateJobObjectW(ptr::null_mut(), ptr::null());
90+
let job = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null()) };
9191
if job == INVALID_HANDLE_VALUE {
9292
return None;
9393
}
@@ -98,22 +98,24 @@ mod imp {
9898
// entire process tree by default because we've added ourselves and
9999
// our children will reside in the job once we spawn a process.
100100
let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION;
101-
info = mem::zeroed();
101+
info = unsafe { mem::zeroed() };
102102
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
103-
let r = SetInformationJobObject(
104-
job.inner,
105-
JobObjectExtendedLimitInformation,
106-
addr_of!(info) as *const _,
107-
mem::size_of_val(&info) as u32,
108-
);
103+
let r = unsafe {
104+
SetInformationJobObject(
105+
job.inner,
106+
JobObjectExtendedLimitInformation,
107+
addr_of!(info) as *const _,
108+
mem::size_of_val(&info) as u32,
109+
)
110+
};
109111
if r == 0 {
110112
return None;
111113
}
112114

113115
// Assign our process to this job object, meaning that our children will
114116
// now live or die based on our existence.
115-
let me = GetCurrentProcess();
116-
let r = AssignProcessToJobObject(job.inner, me);
117+
let me = unsafe { GetCurrentProcess() };
118+
let r = unsafe { AssignProcessToJobObject(job.inner, me) };
117119
if r == 0 {
118120
return None;
119121
}

0 commit comments

Comments
 (0)