Skip to content

Commit 1caa90f

Browse files
authored
Address safety comments for dump_process_memory.
1 parent d8379d3 commit 1caa90f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

crates/rrg/src/action/dump_process_memory.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ mod windows {
438438
self.process.0,
439439
self.cur_addr,
440440
mem.as_mut_ptr(),
441-
std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
441+
std::mem::size_of_val(&mem),
442442
)
443443
};
444444
if status == 0 {
@@ -534,15 +534,20 @@ mod windows {
534534
let meta = file.metadata().unwrap();
535535
let length = meta.len() as usize;
536536

537+
let len_hi = u32::try_from(meta.len() >> u32::BITS)
538+
.expect("invalid length high bits");
539+
let len_lo = u32::try_from(meta.len() & u64::from(!0u32))
540+
.expect("invalid length low bits");
541+
537542
// SAFETY: the returned mapping will be dropped
538543
// by `OwnedHandle`'s `drop` impl.
539544
let mapping = unsafe {
540545
CreateFileMappingW(
541546
file.as_raw_handle(),
542547
std::ptr::null(), // default security
543548
PAGE_READWRITE, // read/write permission
544-
0, // size of mapping object, high
545-
length as u32, // size of mapping object, low
549+
len_hi, // size of mapping object, high
550+
len_lo, // size of mapping object, low
546551
std::ptr::null(),
547552
)
548553
};
@@ -563,6 +568,10 @@ mod windows {
563568
impl Drop for MappedView {
564569
fn drop(&mut self) {
565570
// SAFETY: we only need `unsafe` to call the FFI function here.
571+
//
572+
// The pointer is invalidated but it is not leaked outside
573+
// of this type, so it is no longer accessible after the
574+
// value is destroyed.
566575
unsafe {
567576
UnmapViewOfFile(self.addr);
568577
}

0 commit comments

Comments
 (0)