Skip to content

Commit a6bb6d7

Browse files
committed
Address review comments
1 parent c5304ff commit a6bb6d7

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN};
77
#[repr(C)]
88
struct Header(*mut u8);
99

10+
/// # Safety
11+
///
12+
/// There must be a `Header` at `ptr.offset(-1)`.
1013
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
1114
// SAFETY: the safety contract must be upheld by the caller
1215
unsafe { &mut *(ptr as *mut Header).offset(-1) }
1316
}
1417

18+
/// # Safety
19+
///
20+
/// `ptr`, once aligned, must have space for a Header at `ptr.offset(-1)`.
1521
unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
1622
// SAFETY: the safety contract must be upheld by the caller
1723
unsafe {
@@ -30,7 +36,7 @@ unsafe fn allocate_with_flags(layout: Layout, flags: c::DWORD) -> *mut u8 {
3036

3137
let ptr = unsafe {
3238
// SAFETY: The caller must ensure that
33-
// `layout.size()` + `layout.size()` does not overflow.
39+
// `layout.size()` + `layout.align()` does not overflow.
3440
let size = layout.size() + layout.align();
3541
c::HeapAlloc(c::GetProcessHeap(), flags, size)
3642
};
@@ -71,17 +77,18 @@ unsafe impl GlobalAlloc for System {
7177
c::HeapFree(c::GetProcessHeap(), 0, header.0 as c::LPVOID)
7278
}
7379
};
80+
// SAFETY: `c::GetLastError()` cannot fail
7481
debug_assert!(err != 0, "Failed to free heap memory: {}", unsafe { c::GetLastError() });
7582
}
7683

7784
#[inline]
7885
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
86+
// SAFETY: HeapReAlloc/realloc_fallback is safe if ptr was allocated by this allocator
87+
// and new_size is not 0.
88+
debug_assert_ne!(new_size, 0);
7989
if layout.align() <= MIN_ALIGN {
80-
// SAFETY: HeapReAlloc is safe if ptr was allocated by this allocator
81-
// and new_size is not 0.
8290
unsafe { c::HeapReAlloc(c::GetProcessHeap(), 0, ptr as c::LPVOID, new_size) as *mut u8 }
8391
} else {
84-
// SAFETY: The safety contract for `realloc_fallback` must be upheld by the caller
8592
unsafe { realloc_fallback(self, ptr, layout, new_size) }
8693
}
8794
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ unsafe fn parse_lp_cmd_line<F: Fn() -> OsString>(
5454
const SPACE: u16 = ' ' as u16;
5555
let mut ret_val = Vec::new();
5656

57-
//SAFETY: the caller must supply a valid pointer
57+
// SAFETY: the caller must supply a pointer that is valid to dereference
5858
let mut cmd_line = unsafe {
5959
if lp_cmd_line.is_null() || *lp_cmd_line == 0 {
6060
ret_val.push(exe_name());

0 commit comments

Comments
 (0)