Skip to content

Commit 9813f32

Browse files
committed
fix unsafe scopes
Signed-off-by: Simon Davies <[email protected]>
1 parent 16e8e7c commit 9813f32

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/hyperlight_guest/src/memory.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,38 +121,42 @@ pub unsafe extern "C" fn free(ptr: *mut c_void) {
121121
/// # Safety
122122
/// `ptr` must be a pointer to a memory block previously allocated by `memory::malloc`, `memory::calloc`, or `memory::realloc`.
123123
#[unsafe(no_mangle)]
124-
pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void { unsafe {
124+
pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
125125
if ptr.is_null() {
126126
// If the pointer is null, treat as a malloc
127-
return malloc(size);
127+
return unsafe {malloc(size)};
128128
}
129129

130130
if size == 0 {
131131
// If the size is 0, treat as a free and return null
132-
free(ptr);
132+
unsafe {
133+
free(ptr);
134+
}
133135
return ptr::null_mut();
134136
}
135137

136-
unsafe {
138+
137139
let total_new_size = size
138140
.checked_add(size_of::<Layout>())
139141
.expect("data and layout size should not overflow in realloc");
140142

141-
let block_start = (ptr as *const Layout).sub(1);
142-
let old_layout = block_start.read();
143+
let block_start = unsafe { (ptr as *const Layout).sub(1) };
144+
let old_layout = unsafe { block_start.read() };
143145
let new_layout = Layout::from_size_align(total_new_size, MAX_ALIGN).unwrap();
144146

145147
let new_block_start =
146-
alloc::alloc::realloc(block_start as *mut u8, old_layout, total_new_size)
148+
unsafe {alloc::alloc::realloc(block_start as *mut u8, old_layout, total_new_size)}
147149
as *mut Layout;
148150

149151
if new_block_start.is_null() {
150152
// Realloc failed
151153
abort_with_code(&[ErrorCode::MallocFailed as u8]);
152154
} else {
153155
// Update the stored Layout, then return ptr to memory right after the Layout.
154-
new_block_start.write(new_layout);
155-
new_block_start.add(1) as *mut c_void
156+
unsafe {
157+
new_block_start.write(new_layout);
158+
new_block_start.add(1) as *mut c_void
159+
}
156160
}
157-
}
158-
}}
161+
162+
}

0 commit comments

Comments
 (0)