Skip to content

Commit

Permalink
Avoid cast pointers to usize in windows::NamedPipe
Browse files Browse the repository at this point in the history
Changes the Inner::ptr_from_* methods to use ptr::wrapping_sub rather
then casting to usize.
  • Loading branch information
Thomasdezeeuw committed May 13, 2021
1 parent e316b21 commit 27a6a3c
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/sys/windows/named_pipe.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::ffi::OsStr;
use std::io::{self, Read, Write};
use std::mem::{self, size_of};
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
use std::sync::atomic::Ordering::{Relaxed, SeqCst};
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::{Arc, Mutex};
use std::{fmt, slice};
use std::{fmt, mem, slice};

use miow::iocp::{CompletionPort, CompletionStatus};
use miow::pipe;
Expand Down Expand Up @@ -96,13 +95,13 @@ impl Inner {
/// Same as [`ptr_from_conn_overlapped`] but for `Inner.read`.
unsafe fn ptr_from_read_overlapped(ptr: *mut OVERLAPPED) -> *const Inner {
// `read` is after `connect: Overlapped`.
(ptr as usize + size_of::<Overlapped>()) as *const Inner
(ptr as *mut Overlapped).wrapping_sub(1) as *const Inner
}

/// Same as [`ptr_from_conn_overlapped`] but for `Inner.write`.
unsafe fn ptr_from_write_overlapped(ptr: *mut OVERLAPPED) -> *const Inner {
// `read` is after `connect: Overlapped` and `read: Overlapped`.
(ptr as usize + (2 * size_of::<Overlapped>())) as *const Inner
(ptr as *mut Overlapped).wrapping_sub(2) as *const Inner
}
}

Expand Down

0 comments on commit 27a6a3c

Please sign in to comment.